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