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 <grpc/support/port_platform.h>
20
21 #include "src/core/ext/filters/client_channel/subchannel_pool_interface.h"
22
23 #include "src/core/lib/gpr/useful.h"
24
25 // The subchannel pool to reuse subchannels.
26 #define GRPC_ARG_SUBCHANNEL_POOL "grpc.subchannel_pool"
27 // The subchannel key ID that is only used in test to make each key unique.
28 #define GRPC_ARG_SUBCHANNEL_KEY_TEST_ONLY_ID "grpc.subchannel_key_test_only_id"
29
30 namespace grpc_core {
31
32 TraceFlag grpc_subchannel_pool_trace(false, "subchannel_pool");
33
SubchannelKey(const grpc_channel_args * args)34 SubchannelKey::SubchannelKey(const grpc_channel_args* args) {
35 Init(args, grpc_channel_args_normalize);
36 }
37
~SubchannelKey()38 SubchannelKey::~SubchannelKey() {
39 grpc_channel_args_destroy(const_cast<grpc_channel_args*>(args_));
40 }
41
SubchannelKey(const SubchannelKey & other)42 SubchannelKey::SubchannelKey(const SubchannelKey& other) {
43 Init(other.args_, grpc_channel_args_copy);
44 }
45
operator =(const SubchannelKey & other)46 SubchannelKey& SubchannelKey::operator=(const SubchannelKey& other) {
47 grpc_channel_args_destroy(const_cast<grpc_channel_args*>(args_));
48 Init(other.args_, grpc_channel_args_copy);
49 return *this;
50 }
51
Cmp(const SubchannelKey & other) const52 int SubchannelKey::Cmp(const SubchannelKey& other) const {
53 return grpc_channel_args_compare(args_, other.args_);
54 }
55
Init(const grpc_channel_args * args,grpc_channel_args * (* copy_channel_args)(const grpc_channel_args * args))56 void SubchannelKey::Init(
57 const grpc_channel_args* args,
58 grpc_channel_args* (*copy_channel_args)(const grpc_channel_args* args)) {
59 args_ = copy_channel_args(args);
60 }
61
62 namespace {
63
arg_copy(void * p)64 void* arg_copy(void* p) {
65 auto* subchannel_pool = static_cast<SubchannelPoolInterface*>(p);
66 subchannel_pool->Ref().release();
67 return p;
68 }
69
arg_destroy(void * p)70 void arg_destroy(void* p) {
71 auto* subchannel_pool = static_cast<SubchannelPoolInterface*>(p);
72 subchannel_pool->Unref();
73 }
74
arg_cmp(void * a,void * b)75 int arg_cmp(void* a, void* b) { return GPR_ICMP(a, b); }
76
77 const grpc_arg_pointer_vtable subchannel_pool_arg_vtable = {
78 arg_copy, arg_destroy, arg_cmp};
79
80 } // namespace
81
CreateChannelArg(SubchannelPoolInterface * subchannel_pool)82 grpc_arg SubchannelPoolInterface::CreateChannelArg(
83 SubchannelPoolInterface* subchannel_pool) {
84 return grpc_channel_arg_pointer_create(
85 const_cast<char*>(GRPC_ARG_SUBCHANNEL_POOL), subchannel_pool,
86 &subchannel_pool_arg_vtable);
87 }
88
89 SubchannelPoolInterface*
GetSubchannelPoolFromChannelArgs(const grpc_channel_args * args)90 SubchannelPoolInterface::GetSubchannelPoolFromChannelArgs(
91 const grpc_channel_args* args) {
92 const grpc_arg* arg = grpc_channel_args_find(args, GRPC_ARG_SUBCHANNEL_POOL);
93 if (arg == nullptr || arg->type != GRPC_ARG_POINTER) return nullptr;
94 return static_cast<SubchannelPoolInterface*>(arg->value.pointer.p);
95 }
96
97 } // namespace grpc_core
98