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 if (&other == this) {
48 return *this;
49 }
50 grpc_channel_args_destroy(const_cast<grpc_channel_args*>(args_));
51 Init(other.args_, grpc_channel_args_copy);
52 return *this;
53 }
54
SubchannelKey(SubchannelKey && other)55 SubchannelKey::SubchannelKey(SubchannelKey&& other) noexcept {
56 args_ = other.args_;
57 other.args_ = nullptr;
58 }
59
operator =(SubchannelKey && other)60 SubchannelKey& SubchannelKey::operator=(SubchannelKey&& other) noexcept {
61 args_ = other.args_;
62 other.args_ = nullptr;
63 return *this;
64 }
65
operator <(const SubchannelKey & other) const66 bool SubchannelKey::operator<(const SubchannelKey& other) const {
67 return grpc_channel_args_compare(args_, other.args_) < 0;
68 }
69
Init(const grpc_channel_args * args,grpc_channel_args * (* copy_channel_args)(const grpc_channel_args * args))70 void SubchannelKey::Init(
71 const grpc_channel_args* args,
72 grpc_channel_args* (*copy_channel_args)(const grpc_channel_args* args)) {
73 args_ = copy_channel_args(args);
74 }
75
76 namespace {
77
arg_copy(void * p)78 void* arg_copy(void* p) {
79 auto* subchannel_pool = static_cast<SubchannelPoolInterface*>(p);
80 subchannel_pool->Ref().release();
81 return p;
82 }
83
arg_destroy(void * p)84 void arg_destroy(void* p) {
85 auto* subchannel_pool = static_cast<SubchannelPoolInterface*>(p);
86 subchannel_pool->Unref();
87 }
88
arg_cmp(void * a,void * b)89 int arg_cmp(void* a, void* b) { return GPR_ICMP(a, b); }
90
91 const grpc_arg_pointer_vtable subchannel_pool_arg_vtable = {
92 arg_copy, arg_destroy, arg_cmp};
93
94 } // namespace
95
CreateChannelArg(SubchannelPoolInterface * subchannel_pool)96 grpc_arg SubchannelPoolInterface::CreateChannelArg(
97 SubchannelPoolInterface* subchannel_pool) {
98 return grpc_channel_arg_pointer_create(
99 const_cast<char*>(GRPC_ARG_SUBCHANNEL_POOL), subchannel_pool,
100 &subchannel_pool_arg_vtable);
101 }
102
103 SubchannelPoolInterface*
GetSubchannelPoolFromChannelArgs(const grpc_channel_args * args)104 SubchannelPoolInterface::GetSubchannelPoolFromChannelArgs(
105 const grpc_channel_args* args) {
106 const grpc_arg* arg = grpc_channel_args_find(args, GRPC_ARG_SUBCHANNEL_POOL);
107 if (arg == nullptr || arg->type != GRPC_ARG_POINTER) return nullptr;
108 return static_cast<SubchannelPoolInterface*>(arg->value.pointer.p);
109 }
110
111 } // namespace grpc_core
112