3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVER_ADDRESS_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVER_ADDRESS_H
22 #include <grpc/support/port_platform.h>
27 #include "absl/container/inlined_vector.h"
28 #include "absl/strings/str_format.h"
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/useful.h"
32 #include "src/core/lib/iomgr/resolve_address.h"
40 // A server address is a grpc_resolved_address with an associated set of
41 // channel args. Any args present here will be merged into the channel
42 // args when a subchannel is created for this address.
45 // Base class for resolver-supplied attributes.
46 // Unlike channel args, these attributes don't affect subchannel
47 // uniqueness or behavior. They are for use by LB policies only.
49 // Attributes are keyed by a C string that is unique by address, not
50 // by value. All attributes added with the same key must be of the
52 class AttributeInterface {
54 virtual ~AttributeInterface() = default;
56 // Creates a copy of the attribute.
57 virtual std::unique_ptr<AttributeInterface> Copy() const = 0;
59 // Compares this attribute with another.
60 virtual int Cmp(const AttributeInterface* other) const = 0;
62 // Returns a human-readable representation of the attribute.
63 virtual std::string ToString() const = 0;
66 // Takes ownership of args.
67 ServerAddress(const grpc_resolved_address& address, grpc_channel_args* args,
68 std::map<const char*, std::unique_ptr<AttributeInterface>>
70 ServerAddress(const void* address, size_t address_len,
71 grpc_channel_args* args,
72 std::map<const char*, std::unique_ptr<AttributeInterface>>
75 ~ServerAddress() { grpc_channel_args_destroy(args_); }
78 ServerAddress(const ServerAddress& other);
79 ServerAddress& operator=(const ServerAddress& other);
82 ServerAddress(ServerAddress&& other) noexcept;
83 ServerAddress& operator=(ServerAddress&& other) noexcept;
85 bool operator==(const ServerAddress& other) const { return Cmp(other) == 0; }
87 int Cmp(const ServerAddress& other) const;
89 const grpc_resolved_address& address() const { return address_; }
90 const grpc_channel_args* args() const { return args_; }
92 const AttributeInterface* GetAttribute(const char* key) const;
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;
99 std::string ToString() const;
102 grpc_resolved_address address_;
103 grpc_channel_args* args_;
104 std::map<const char*, std::unique_ptr<AttributeInterface>> attributes_;
111 typedef absl::InlinedVector<ServerAddress, 1> ServerAddressList;
114 // ServerAddressWeightAttribute
116 class ServerAddressWeightAttribute : public ServerAddress::AttributeInterface {
118 static const char* kServerAddressWeightAttributeKey;
120 explicit ServerAddressWeightAttribute(uint32_t weight) : weight_(weight) {}
122 uint32_t weight() const { return weight_; }
124 std::unique_ptr<AttributeInterface> Copy() const override {
125 return absl::make_unique<ServerAddressWeightAttribute>(weight_);
128 int Cmp(const AttributeInterface* other) const override {
129 const auto* other_locality_attr =
130 static_cast<const ServerAddressWeightAttribute*>(other);
131 return GPR_ICMP(weight_, other_locality_attr->weight_);
134 std::string ToString() const override {
135 return absl::StrFormat("%d", weight_);
142 } // namespace grpc_core
144 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVER_ADDRESS_H */