Debian Codegraph @

src/core/lib/resolver/server_address.h

raw
1 /*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPC_CORE_LIB_RESOLVER_SERVER_ADDRESS_H
20 #define GRPC_CORE_LIB_RESOLVER_SERVER_ADDRESS_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <stddef.h>
25 #include <stdint.h>
26
27 #include <map>
28 #include <memory>
29 #include <string>
30 #include <vector>
31
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/gpr/useful.h"
34 #include "src/core/lib/iomgr/resolved_address.h"
35
36 namespace grpc_core {
37
38 //
39 // ServerAddress
40 //
41
42 // A server address is a grpc_resolved_address with an associated set of
43 // channel args. Any args present here will be merged into the channel
44 // args when a subchannel is created for this address.
45 class ServerAddress {
46 public:
47 // Base class for resolver-supplied attributes.
48 // Unlike channel args, these attributes don't affect subchannel
49 // uniqueness or behavior. They are for use by LB policies only.
50 //
51 // Attributes are keyed by a C string that is unique by address, not
52 // by value. All attributes added with the same key must be of the
53 // same type.
54 class AttributeInterface {
55 public:
56 virtual ~AttributeInterface() = default;
57
58 // Creates a copy of the attribute.
59 virtual std::unique_ptr<AttributeInterface> Copy() const = 0;
60
61 // Compares this attribute with another.
62 virtual int Cmp(const AttributeInterface* other) const = 0;
63
64 // Returns a human-readable representation of the attribute.
65 virtual std::string ToString() const = 0;
66 };
67
68 // Takes ownership of args.
69 ServerAddress(const grpc_resolved_address& address, const ChannelArgs& args,
70 std::map<const char*, std::unique_ptr<AttributeInterface>>
71 attributes = {});
72 ServerAddress(const void* address, size_t address_len,
73 const ChannelArgs& args,
74 std::map<const char*, std::unique_ptr<AttributeInterface>>
75 attributes = {});
76
77 // Copyable.
78 ServerAddress(const ServerAddress& other);
79 ServerAddress& operator=(const ServerAddress& other);
80
81 // Movable.
82 ServerAddress(ServerAddress&& other) noexcept;
83 ServerAddress& operator=(ServerAddress&& other) noexcept;
84
85 bool operator==(const ServerAddress& other) const { return Cmp(other) == 0; }
86
87 int Cmp(const ServerAddress& other) const;
88
89 const grpc_resolved_address& address() const { return address_; }
90 const ChannelArgs& args() const { return args_; }
91
92 const AttributeInterface* GetAttribute(const char* key) const;
93
94 // Returns a copy of the address with a modified attribute.
95 // If the new value is null, the attribute is removed.
96 ServerAddress WithAttribute(const char* key,
97 std::unique_ptr<AttributeInterface> value) const;
98
99 // TODO(ctiller): Prior to making this a public API we should ensure that the
100 // channel args are not part of the generated string, lest we make that debug
101 // format load-bearing via Hyrum's law.
102 std::string ToString() const;
103
104 private:
105 grpc_resolved_address address_;
106 ChannelArgs args_;
107 std::map<const char*, std::unique_ptr<AttributeInterface>> attributes_;
108 };
109
110 //
111 // ServerAddressList
112 //
113
114 using ServerAddressList = std::vector<ServerAddress>;
115
116 //
117 // ServerAddressWeightAttribute
118 //
119 class ServerAddressWeightAttribute : public ServerAddress::AttributeInterface {
120 public:
121 static const char* kServerAddressWeightAttributeKey;
122
123 explicit ServerAddressWeightAttribute(uint32_t weight) : weight_(weight) {}
124
125 uint32_t weight() const { return weight_; }
126
127 std::unique_ptr<AttributeInterface> Copy() const override {
128 return std::make_unique<ServerAddressWeightAttribute>(weight_);
129 }
130
131 int Cmp(const AttributeInterface* other) const override {
132 const auto* other_locality_attr =
133 static_cast<const ServerAddressWeightAttribute*>(other);
134 return QsortCompare(weight_, other_locality_attr->weight_);
135 }
136
137 std::string ToString() const override;
138
139 private:
140 uint32_t weight_;
141 };
142
143 } // namespace grpc_core
144
145 #endif /* GRPC_CORE_LIB_RESOLVER_SERVER_ADDRESS_H */