subchannel_pool_interface.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <string.h>
24 
25 #include "absl/status/status.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/str_cat.h"
28 
32 
33 // The subchannel pool to reuse subchannels.
34 #define GRPC_ARG_SUBCHANNEL_POOL "grpc.internal.subchannel_pool"
35 // The subchannel key ID that is only used in test to make each key unique.
36 #define GRPC_ARG_SUBCHANNEL_KEY_TEST_ONLY_ID "grpc.subchannel_key_test_only_id"
37 
38 namespace grpc_core {
39 
40 TraceFlag grpc_subchannel_pool_trace(false, "subchannel_pool");
41 
43  const grpc_channel_args* args) {
45 }
46 
49 }
50 
53 }
54 
56  if (&other == this) {
57  return *this;
58  }
61  return *this;
62 }
63 
65  address_ = other.address_;
66  args_ = other.args_;
67  other.args_ = nullptr;
68 }
69 
71  address_ = other.address_;
72  args_ = other.args_;
73  other.args_ = nullptr;
74  return *this;
75 }
76 
77 bool SubchannelKey::operator<(const SubchannelKey& other) const {
78  if (address_.len < other.address_.len) return true;
79  if (address_.len > other.address_.len) return false;
80  int r = memcmp(address_.addr, other.address_.addr, address_.len);
81  if (r < 0) return true;
82  if (r > 0) return false;
83  return grpc_channel_args_compare(args_, other.args_) < 0;
84 }
85 
87  const grpc_resolved_address& address, const grpc_channel_args* args,
88  grpc_channel_args* (*copy_channel_args)(const grpc_channel_args* args)) {
89  address_ = address;
90  args_ = copy_channel_args(args);
91 }
92 
94  auto addr_uri = grpc_sockaddr_to_uri(&address_);
95  return absl::StrCat(
96  "{address=",
97  addr_uri.ok() ? addr_uri.value() : addr_uri.status().ToString(),
98  ", args=", grpc_channel_args_string(args_), "}");
99 }
100 
101 namespace {
102 
103 void* arg_copy(void* p) {
104  auto* subchannel_pool = static_cast<SubchannelPoolInterface*>(p);
105  subchannel_pool->Ref().release();
106  return p;
107 }
108 
109 void arg_destroy(void* p) {
110  auto* subchannel_pool = static_cast<SubchannelPoolInterface*>(p);
111  subchannel_pool->Unref();
112 }
113 
114 int arg_cmp(void* a, void* b) { return QsortCompare(a, b); }
115 
116 const grpc_arg_pointer_vtable subchannel_pool_arg_vtable = {
117  arg_copy, arg_destroy, arg_cmp};
118 
119 } // namespace
120 
122  SubchannelPoolInterface* subchannel_pool) {
124  const_cast<char*>(GRPC_ARG_SUBCHANNEL_POOL), subchannel_pool,
125  &subchannel_pool_arg_vtable);
126 }
127 
130  const grpc_channel_args* args) {
132  if (arg == nullptr || arg->type != GRPC_ARG_POINTER) return nullptr;
133  return static_cast<SubchannelPoolInterface*>(arg->value.pointer.p);
134 }
135 
136 } // namespace grpc_core
grpc_arg
Definition: grpc_types.h:103
grpc_core::SubchannelKey::~SubchannelKey
~SubchannelKey()
Definition: subchannel_pool_interface.cc:47
sockaddr_utils.h
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc_core::SubchannelPoolInterface::CreateChannelArg
static grpc_arg CreateChannelArg(SubchannelPoolInterface *subchannel_pool)
Definition: subchannel_pool_interface.cc:121
grpc_core
Definition: call_metric_recorder.h:31
string.h
useful.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
address_
ServerAddress address_
Definition: ring_hash.cc:194
arg::value
void * value
Definition: cmdline.cc:44
grpc_resolved_address
Definition: resolved_address.h:34
subchannel_pool_interface.h
args_
grpc_channel_args * args_
Definition: grpclb.cc:513
grpc_arg_pointer_vtable
Definition: grpc_types.h:85
grpc_channel_args
Definition: grpc_types.h:132
GRPC_ARG_SUBCHANNEL_POOL
#define GRPC_ARG_SUBCHANNEL_POOL
Definition: subchannel_pool_interface.cc:34
grpc_core::grpc_subchannel_pool_trace
TraceFlag grpc_subchannel_pool_trace(false, "subchannel_pool")
Definition: subchannel_pool_interface.h:35
arg::type
argtype type
Definition: cmdline.cc:43
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::SubchannelKey::operator=
SubchannelKey & operator=(const SubchannelKey &other)
Definition: subchannel_pool_interface.cc:55
grpc_channel_args_compare
int grpc_channel_args_compare(const grpc_channel_args *a, const grpc_channel_args *b)
Definition: channel_args.cc:380
grpc_core::SubchannelKey::Init
void Init(const grpc_resolved_address &address, const grpc_channel_args *args, grpc_channel_args *(*copy_channel_args)(const grpc_channel_args *args))
Definition: subchannel_pool_interface.cc:86
grpc_core::SubchannelKey::args_
const grpc_channel_args * args_
Definition: subchannel_pool_interface.h:69
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
arg
Definition: cmdline.cc:40
grpc_channel_args_copy
grpc_channel_args * grpc_channel_args_copy(const grpc_channel_args *src)
Definition: channel_args.cc:285
grpc_resolved_address::len
socklen_t len
Definition: resolved_address.h:36
grpc_core::SubchannelKey::SubchannelKey
SubchannelKey(const grpc_resolved_address &address, const grpc_channel_args *args)
Definition: subchannel_pool_interface.cc:42
grpc_core::SubchannelKey::ToString
std::string ToString() const
Definition: subchannel_pool_interface.cc:93
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc_core::SubchannelPoolInterface::GetSubchannelPoolFromChannelArgs
static SubchannelPoolInterface * GetSubchannelPoolFromChannelArgs(const grpc_channel_args *args)
Definition: subchannel_pool_interface.cc:129
grpc_core::SubchannelKey
Definition: subchannel_pool_interface.h:40
grpc_core::SubchannelPoolInterface
Definition: subchannel_pool_interface.h:76
grpc_core::SubchannelKey::address
const grpc_resolved_address & address() const
Definition: subchannel_pool_interface.h:55
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc_core::SubchannelKey::operator<
bool operator<(const SubchannelKey &other) const
Definition: subchannel_pool_interface.cc:77
grpc_channel_args_normalize
grpc_channel_args * grpc_channel_args_normalize(const grpc_channel_args *src)
Definition: channel_args.cc:338
channel_args.h
grpc_core::ServerAddress::address_
grpc_resolved_address address_
Definition: server_address.h:111
GRPC_ARG_POINTER
@ GRPC_ARG_POINTER
Definition: grpc_types.h:82
grpc_core::SubchannelKey::args
const grpc_channel_args * args() const
Definition: subchannel_pool_interface.h:56
grpc_channel_arg_pointer_create
grpc_arg grpc_channel_arg_pointer_create(char *name, void *value, const grpc_arg_pointer_vtable *vtable)
Definition: channel_args.cc:492
grpc_sockaddr_to_uri
absl::StatusOr< std::string > grpc_sockaddr_to_uri(const grpc_resolved_address *resolved_addr)
Definition: sockaddr_utils.cc:260
grpc_channel_args_find
const grpc_arg * grpc_channel_args_find(const grpc_channel_args *args, const char *name)
Definition: channel_args.cc:393
grpc_resolved_address::addr
char addr[GRPC_MAX_SOCKADDR_SIZE]
Definition: resolved_address.h:35
grpc_core::SubchannelKey::address_
grpc_resolved_address address_
Definition: subchannel_pool_interface.h:68
grpc_channel_args_string
std::string grpc_channel_args_string(const grpc_channel_args *args)
Definition: channel_args.cc:502
grpc_core::RefCounted::Ref
RefCountedPtr< Child > Ref() GRPC_MUST_USE_RESULT
Definition: ref_counted.h:287
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:27