• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/core/client_channel/subchannel_pool_interface.h"
20 
21 #include <grpc/support/port_platform.h>
22 #include <string.h>
23 
24 #include "absl/status/status.h"
25 #include "absl/status/statusor.h"
26 #include "absl/strings/str_cat.h"
27 #include "src/core/lib/address_utils/sockaddr_utils.h"
28 #include "src/core/lib/channel/channel_args.h"
29 
30 // The subchannel pool to reuse subchannels.
31 #define GRPC_ARG_SUBCHANNEL_POOL "grpc.internal.subchannel_pool"
32 // The subchannel key ID that is only used in test to make each key unique.
33 #define GRPC_ARG_SUBCHANNEL_KEY_TEST_ONLY_ID "grpc.subchannel_key_test_only_id"
34 
35 namespace grpc_core {
36 
SubchannelKey(const grpc_resolved_address & address,const ChannelArgs & args)37 SubchannelKey::SubchannelKey(const grpc_resolved_address& address,
38                              const ChannelArgs& args)
39     : address_(address), args_(args) {}
40 
operator <(const SubchannelKey & other) const41 bool SubchannelKey::operator<(const SubchannelKey& other) const {
42   if (address_.len < other.address_.len) return true;
43   if (address_.len > other.address_.len) return false;
44   int r = memcmp(address_.addr, other.address_.addr, address_.len);
45   if (r < 0) return true;
46   if (r > 0) return false;
47   return args_ < other.args();
48 }
49 
ToString() const50 std::string SubchannelKey::ToString() const {
51   auto addr_uri = grpc_sockaddr_to_uri(&address_);
52   return absl::StrCat(
53       "{address=",
54       addr_uri.ok() ? addr_uri.value() : addr_uri.status().ToString(),
55       ", args=", args_.ToString(), "}");
56 }
57 
ChannelArgName()58 absl::string_view SubchannelPoolInterface::ChannelArgName() {
59   return GRPC_ARG_SUBCHANNEL_POOL;
60 }
61 
62 }  // namespace grpc_core
63