xref: /aosp_15_r20/external/grpc-grpc/src/cpp/common/channel_arguments.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 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 #include <algorithm>
19 #include <list>
20 #include <string>
21 #include <vector>
22 
23 #include <grpc/impl/channel_arg_names.h>
24 #include <grpc/impl/compression_types.h>
25 #include <grpc/support/log.h>
26 #include <grpcpp/grpcpp.h>
27 #include <grpcpp/resource_quota.h>
28 #include <grpcpp/support/channel_arguments.h>
29 
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "src/core/lib/iomgr/socket_mutator.h"
32 
33 namespace grpc {
34 
ChannelArguments()35 ChannelArguments::ChannelArguments() {
36   // This will be ignored if used on the server side.
37   SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, "grpc-c++/" + grpc::Version());
38 }
39 
ChannelArguments(const ChannelArguments & other)40 ChannelArguments::ChannelArguments(const ChannelArguments& other)
41     : strings_(other.strings_) {
42   args_.reserve(other.args_.size());
43   auto list_it_dst = strings_.begin();
44   auto list_it_src = other.strings_.begin();
45   for (const auto& a : other.args_) {
46     grpc_arg ap;
47     ap.type = a.type;
48     GPR_ASSERT(list_it_src->c_str() == a.key);
49     ap.key = const_cast<char*>(list_it_dst->c_str());
50     ++list_it_src;
51     ++list_it_dst;
52     switch (a.type) {
53       case GRPC_ARG_INTEGER:
54         ap.value.integer = a.value.integer;
55         break;
56       case GRPC_ARG_STRING:
57         GPR_ASSERT(list_it_src->c_str() == a.value.string);
58         ap.value.string = const_cast<char*>(list_it_dst->c_str());
59         ++list_it_src;
60         ++list_it_dst;
61         break;
62       case GRPC_ARG_POINTER:
63         ap.value.pointer = a.value.pointer;
64         ap.value.pointer.p = a.value.pointer.vtable->copy(ap.value.pointer.p);
65         break;
66     }
67     args_.push_back(ap);
68   }
69 }
70 
~ChannelArguments()71 ChannelArguments::~ChannelArguments() {
72   for (auto& arg : args_) {
73     if (arg.type == GRPC_ARG_POINTER) {
74       grpc_core::ExecCtx exec_ctx;
75       arg.value.pointer.vtable->destroy(arg.value.pointer.p);
76     }
77   }
78 }
79 
Swap(ChannelArguments & other)80 void ChannelArguments::Swap(ChannelArguments& other) {
81   args_.swap(other.args_);
82   strings_.swap(other.strings_);
83 }
84 
SetCompressionAlgorithm(grpc_compression_algorithm algorithm)85 void ChannelArguments::SetCompressionAlgorithm(
86     grpc_compression_algorithm algorithm) {
87   SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, algorithm);
88 }
89 
SetGrpclbFallbackTimeout(int fallback_timeout)90 void ChannelArguments::SetGrpclbFallbackTimeout(int fallback_timeout) {
91   SetInt(GRPC_ARG_GRPCLB_FALLBACK_TIMEOUT_MS, fallback_timeout);
92 }
93 
SetSocketMutator(grpc_socket_mutator * mutator)94 void ChannelArguments::SetSocketMutator(grpc_socket_mutator* mutator) {
95   if (!mutator) {
96     return;
97   }
98   grpc_arg mutator_arg = grpc_socket_mutator_to_arg(mutator);
99   bool replaced = false;
100   grpc_core::ExecCtx exec_ctx;
101   for (auto& arg : args_) {
102     if (arg.type == mutator_arg.type &&
103         std::string(arg.key) == std::string(mutator_arg.key)) {
104       GPR_ASSERT(!replaced);
105       arg.value.pointer.vtable->destroy(arg.value.pointer.p);
106       arg.value.pointer = mutator_arg.value.pointer;
107       replaced = true;
108     }
109   }
110 
111   if (!replaced) {
112     strings_.push_back(std::string(mutator_arg.key));
113     args_.push_back(mutator_arg);
114     args_.back().key = const_cast<char*>(strings_.back().c_str());
115   }
116 }
117 
118 // Note: a second call to this will add in front the result of the first call.
119 // An example is calling this on a copy of ChannelArguments which already has a
120 // prefix. The user can build up a prefix string by calling this multiple times,
121 // each with more significant identifier.
SetUserAgentPrefix(const std::string & user_agent_prefix)122 void ChannelArguments::SetUserAgentPrefix(
123     const std::string& user_agent_prefix) {
124   if (user_agent_prefix.empty()) {
125     return;
126   }
127   bool replaced = false;
128   auto strings_it = strings_.begin();
129   for (auto& arg : args_) {
130     ++strings_it;
131     if (arg.type == GRPC_ARG_STRING) {
132       if (std::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) {
133         GPR_ASSERT(arg.value.string == strings_it->c_str());
134         *(strings_it) = user_agent_prefix + " " + arg.value.string;
135         arg.value.string = const_cast<char*>(strings_it->c_str());
136         replaced = true;
137         break;
138       }
139       ++strings_it;
140     }
141   }
142   if (!replaced) {
143     SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix);
144   }
145 }
146 
SetResourceQuota(const grpc::ResourceQuota & resource_quota)147 void ChannelArguments::SetResourceQuota(
148     const grpc::ResourceQuota& resource_quota) {
149   SetPointerWithVtable(GRPC_ARG_RESOURCE_QUOTA,
150                        resource_quota.c_resource_quota(),
151                        grpc_resource_quota_arg_vtable());
152 }
153 
SetMaxReceiveMessageSize(int size)154 void ChannelArguments::SetMaxReceiveMessageSize(int size) {
155   SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, size);
156 }
157 
SetMaxSendMessageSize(int size)158 void ChannelArguments::SetMaxSendMessageSize(int size) {
159   SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, size);
160 }
161 
SetLoadBalancingPolicyName(const std::string & lb_policy_name)162 void ChannelArguments::SetLoadBalancingPolicyName(
163     const std::string& lb_policy_name) {
164   SetString(GRPC_ARG_LB_POLICY_NAME, lb_policy_name);
165 }
166 
SetServiceConfigJSON(const std::string & service_config_json)167 void ChannelArguments::SetServiceConfigJSON(
168     const std::string& service_config_json) {
169   SetString(GRPC_ARG_SERVICE_CONFIG, service_config_json);
170 }
171 
SetInt(const std::string & key,int value)172 void ChannelArguments::SetInt(const std::string& key, int value) {
173   grpc_arg arg;
174   arg.type = GRPC_ARG_INTEGER;
175   strings_.push_back(key);
176   arg.key = const_cast<char*>(strings_.back().c_str());
177   arg.value.integer = value;
178 
179   args_.push_back(arg);
180 }
181 
SetPointer(const std::string & key,void * value)182 void ChannelArguments::SetPointer(const std::string& key, void* value) {
183   static const grpc_arg_pointer_vtable vtable = {
184       &PointerVtableMembers::Copy, &PointerVtableMembers::Destroy,
185       &PointerVtableMembers::Compare};
186   SetPointerWithVtable(key, value, &vtable);
187 }
188 
SetPointerWithVtable(const std::string & key,void * value,const grpc_arg_pointer_vtable * vtable)189 void ChannelArguments::SetPointerWithVtable(
190     const std::string& key, void* value,
191     const grpc_arg_pointer_vtable* vtable) {
192   grpc_arg arg;
193   arg.type = GRPC_ARG_POINTER;
194   strings_.push_back(key);
195   arg.key = const_cast<char*>(strings_.back().c_str());
196   arg.value.pointer.p = vtable->copy(value);
197   arg.value.pointer.vtable = vtable;
198   args_.push_back(arg);
199 }
200 
SetString(const std::string & key,const std::string & value)201 void ChannelArguments::SetString(const std::string& key,
202                                  const std::string& value) {
203   grpc_arg arg;
204   arg.type = GRPC_ARG_STRING;
205   strings_.push_back(key);
206   arg.key = const_cast<char*>(strings_.back().c_str());
207   strings_.push_back(value);
208   arg.value.string = const_cast<char*>(strings_.back().c_str());
209 
210   args_.push_back(arg);
211 }
212 
SetChannelArgs(grpc_channel_args * channel_args) const213 void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
214   channel_args->num_args = args_.size();
215   if (channel_args->num_args > 0) {
216     channel_args->args = const_cast<grpc_arg*>(&args_[0]);
217   }
218 }
219 
SetSslTargetNameOverride(const std::string & name)220 void ChannelArguments::SetSslTargetNameOverride(const std::string& name) {
221   SetString(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, name);
222 }
223 
GetSslTargetNameOverride() const224 std::string ChannelArguments::GetSslTargetNameOverride() const {
225   for (unsigned int i = 0; i < args_.size(); i++) {
226     if (std::string(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG) == args_[i].key) {
227       return args_[i].value.string;
228     }
229   }
230   return "";
231 }
232 
233 }  // namespace grpc
234