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_SRC_CORE_RESOLVER_ENDPOINT_ADDRESSES_H 20 #define GRPC_SRC_CORE_RESOLVER_ENDPOINT_ADDRESSES_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <set> 25 #include <string> 26 #include <utility> 27 #include <vector> 28 29 #include "absl/functional/function_ref.h" 30 #include "src/core/lib/channel/channel_args.h" 31 #include "src/core/lib/iomgr/resolved_address.h" 32 33 // A channel arg key prefix used for args that are intended to be used 34 // only internally to resolvers and LB policies and should not be part 35 // of the subchannel key. The channel will automatically filter out any 36 // args with this prefix from the subchannel's args. 37 #define GRPC_ARG_NO_SUBCHANNEL_PREFIX "grpc.internal.no_subchannel." 38 39 // A channel arg indicating the weight of an address. 40 #define GRPC_ARG_ADDRESS_WEIGHT GRPC_ARG_NO_SUBCHANNEL_PREFIX "address.weight" 41 42 // Name associated with individual address, if available (e.g., DNS name). 43 #define GRPC_ARG_ADDRESS_NAME "grpc.address_name" 44 45 namespace grpc_core { 46 47 // A list of addresses for a given endpoint with an associated set of channel 48 // args. Any args present here will be merged into the channel args when a 49 // subchannel is created for each address. 50 class EndpointAddresses final { 51 public: 52 // For backward compatibility. 53 // TODO(roth): Remove when callers have been updated. 54 EndpointAddresses(const grpc_resolved_address& address, 55 const ChannelArgs& args); 56 57 // addresses must not be empty. 58 EndpointAddresses(std::vector<grpc_resolved_address> addresses, 59 const ChannelArgs& args); 60 61 // Copyable. 62 EndpointAddresses(const EndpointAddresses& other); 63 EndpointAddresses& operator=(const EndpointAddresses& other); 64 65 // Movable. 66 EndpointAddresses(EndpointAddresses&& other) noexcept; 67 EndpointAddresses& operator=(EndpointAddresses&& other) noexcept; 68 69 bool operator==(const EndpointAddresses& other) const { 70 return Cmp(other) == 0; 71 } 72 bool operator!=(const EndpointAddresses& other) const { 73 return Cmp(other) != 0; 74 } 75 bool operator<(const EndpointAddresses& other) const { 76 return Cmp(other) < 0; 77 } 78 79 int Cmp(const EndpointAddresses& other) const; 80 81 // For backward compatibility only. 82 // TODO(roth): Remove when all callers have been updated. address()83 const grpc_resolved_address& address() const { return addresses_[0]; } 84 addresses()85 const std::vector<grpc_resolved_address>& addresses() const { 86 return addresses_; 87 } args()88 const ChannelArgs& args() const { return args_; } 89 90 // TODO(ctiller): Prior to making this a public API we should ensure that the 91 // channel args are not part of the generated string, lest we make that debug 92 // format load-bearing via Hyrum's law. 93 std::string ToString() const; 94 95 private: 96 std::vector<grpc_resolved_address> addresses_; 97 ChannelArgs args_; 98 }; 99 100 using EndpointAddressesList = std::vector<EndpointAddresses>; 101 102 struct ResolvedAddressLessThan { 103 bool operator()(const grpc_resolved_address& addr1, 104 const grpc_resolved_address& addr2) const; 105 }; 106 107 class EndpointAddressSet final { 108 public: EndpointAddressSet(const std::vector<grpc_resolved_address> & addresses)109 explicit EndpointAddressSet( 110 const std::vector<grpc_resolved_address>& addresses) 111 : addresses_(addresses.begin(), addresses.end()) {} 112 113 bool operator==(const EndpointAddressSet& other) const; 114 bool operator<(const EndpointAddressSet& other) const; 115 116 std::string ToString() const; 117 118 private: 119 std::set<grpc_resolved_address, ResolvedAddressLessThan> addresses_; 120 }; 121 122 // An iterator interface for endpoints. 123 class EndpointAddressesIterator { 124 public: 125 virtual ~EndpointAddressesIterator() = default; 126 127 // Invokes callback once for each endpoint. 128 virtual void ForEach( 129 absl::FunctionRef<void(const EndpointAddresses&)> callback) const = 0; 130 }; 131 132 // Iterator over a fixed list of endpoints. 133 class EndpointAddressesListIterator final : public EndpointAddressesIterator { 134 public: EndpointAddressesListIterator(EndpointAddressesList endpoints)135 explicit EndpointAddressesListIterator(EndpointAddressesList endpoints) 136 : endpoints_(std::move(endpoints)) {} 137 ForEach(absl::FunctionRef<void (const EndpointAddresses &)> callback)138 void ForEach(absl::FunctionRef<void(const EndpointAddresses&)> callback) 139 const override { 140 for (const auto& endpoint : endpoints_) { 141 callback(endpoint); 142 } 143 } 144 145 private: 146 EndpointAddressesList endpoints_; 147 }; 148 149 // Iterator that returns only a single endpoint. 150 class SingleEndpointIterator final : public EndpointAddressesIterator { 151 public: SingleEndpointIterator(EndpointAddresses endpoint)152 explicit SingleEndpointIterator(EndpointAddresses endpoint) 153 : endpoint_(std::move(endpoint)) {} 154 ForEach(absl::FunctionRef<void (const EndpointAddresses &)> callback)155 void ForEach(absl::FunctionRef<void(const EndpointAddresses&)> callback) 156 const override { 157 callback(endpoint_); 158 } 159 160 private: 161 EndpointAddresses endpoint_; 162 }; 163 164 } // namespace grpc_core 165 166 #endif // GRPC_SRC_CORE_RESOLVER_ENDPOINT_ADDRESSES_H 167