Google Cloud C++ Client  2.7.0
C++ Client Library for Google Cloud Platform
grpc_options.h
Go to the documentation of this file.
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_GRPC_OPTIONS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_GRPC_OPTIONS_H
17 
18 #include "google/cloud/background_threads.h"
19 #include "google/cloud/completion_queue.h"
20 #include "google/cloud/options.h"
21 #include "google/cloud/tracing_options.h"
22 #include "google/cloud/version.h"
23 #include <grpcpp/grpcpp.h>
24 #include <map>
25 #include <string>
26 
27 namespace google {
28 namespace cloud {
30 
31 /**
32  * The gRPC credentials used by clients configured with this object.
33  *
34  * @ingroup options
35  */
37  using Type = std::shared_ptr<grpc::ChannelCredentials>;
38 };
39 
40 /**
41  * The number of transport channels to create.
42  *
43  * gRPC limits the number of simultaneous calls in progress on a channel to
44  * 100. Increasing the number of channels thus increases the number of
45  * operations that can be in progress in parallel.
46  *
47  * @note This option only applies when passed to the following functions:
48  * - `bigtable::MakeDataConnection()`
49  * - `pubsub::MakePublisherConnection()`
50  * - `pubsub::MakeSubscriberConnection()`
51  * - `spanner::MakeConnection()`
52  * - `storage_experimental::DefaultGrpcClient()`
53  *
54  * @ingroup options
55  */
57  using Type = int;
58 };
59 
60 /**
61  * A string-string map of arguments for `grpc::ChannelArguments::SetString`.
62  *
63  * This option gives users the ability to set various arguments for the
64  * underlying `grpc::ChannelArguments` objects that will be created. See the
65  * gRPC documentation for more details about available channel arguments.
66  *
67  * @note Our library will always start with the native object from
68  * `GrpcChannelArgumentsNativeOption`, then add the channel arguments from this
69  * option. Users are cautioned not to set the same channel argument to different
70  * values in different options as gRPC will use the first value set for some
71  * channel arguments, and the last value set for others.
72  *
73  * @see https://grpc.github.io/grpc/cpp/classgrpc_1_1_channel_arguments.html
74  * @see https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
75  *
76  * @ingroup options
77  */
79  using Type = std::map<std::string, std::string>;
80 };
81 
82 /**
83  * The native `grpc::ChannelArguments` object.
84  *
85  * This option gives users full control over the `grpc::ChannelArguments`
86  * objects that will be created. See the gRPC documentation for more details
87  * about available channel arguments.
88  *
89  * @note Our library will always start with the native object, then add in the
90  * channel arguments from `GrpcChannelArgumentsOption`, then add the user agent
91  * prefix from `UserAgentProductsOption`. Users are cautioned not to set the
92  * same channel argument to different values in different options as gRPC will
93  * use the first value set for some channel arguments, and the last value set
94  * for others.
95  *
96  * @see https://grpc.github.io/grpc/cpp/classgrpc_1_1_channel_arguments.html
97  * @see https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
98  *
99  * @ingroup options
100  */
102  using Type = grpc::ChannelArguments;
103 };
104 
105 /**
106  * The `TracingOptions` to use when printing grpc protocol buffer messages.
107  *
108  * @ingroup options
109  */
111  using Type = TracingOptions;
112 };
113 
114 /**
115  * The size of the background thread pool
116  *
117  * These threads are created by the client library to run a `CompletionQueue`
118  * which performs background work for gRPC.
119  *
120  * @note `GrpcBackgroundThreadPoolSizeOption`, `GrpcCompletionQueueOption`, and
121  * `GrpcBackgroundThreadsFactoryOption` are mutually exclusive. This option
122  * will be ignored if either `GrpcCompletionQueueOption` or
123  * `GrpcBackgroundThreadsFactoryOption` are set.
124  *
125  * @ingroup options
126  */
128  using Type = std::size_t;
129 };
130 
131 /**
132  * The `CompletionQueue` to use for background gRPC work.
133  *
134  * If this option is set, the library will use the supplied `CompletionQueue`
135  * instead of its own. The caller is responsible for making sure there are
136  * thread(s) servicing this `CompletionQueue`. The client library will not
137  * create any background threads or attempt to call `CompletionQueue::Run()`.
138  *
139  * @note `GrpcBackgroundThreadPoolSizeOption`, `GrpcCompletionQueueOption`, and
140  * `GrpcBackgroundThreadsFactoryOption` are mutually exclusive.
141  *
142  * @ingroup options
143  */
145  using Type = CompletionQueue;
146 };
147 
148 using BackgroundThreadsFactory =
149  std::function<std::unique_ptr<BackgroundThreads>()>;
150 
151 /**
152  * Changes the `BackgroundThreadsFactory`.
153  *
154  * Connections need to perform background work on behalf of the application.
155  * Normally they just create a background thread and a `CompletionQueue` for
156  * this work, but the application may need more fine-grained control of their
157  * threads.
158  *
159  * In this case the application can provide its own `BackgroundThreadsFactory`
160  * and it assumes responsibility for creating one or more threads blocked on its
161  * `CompletionQueue::Run()`.
162  *
163  * @note `GrpcBackgroundThreadPoolSizeOption`, `GrpcCompletionQueueOption`, and
164  * `GrpcBackgroundThreadsFactoryOption` are mutually exclusive. This option
165  * will be ignored if `GrpcCompletionQueueOption` is set.
166  *
167  * @ingroup options
168  */
170  using Type = BackgroundThreadsFactory;
171 };
172 
173 /**
174  * A list of all the gRPC options.
175  */
176 using GrpcOptionList =
181 
182 namespace internal {
183 
184 /**
185  * A generic function to directly configure a gRPC context.
186  *
187  * This function takes effect before the context is used to make any requests.
188  *
189  * @warning It is NOT recommended to call `set_auth_context()` or
190  * `set_credentials()` directly on the context. Instead, use the Google
191  * Unified Auth Credentials library, via
192  * #google::cloud::UnifiedCredentialsOption.
193  */
194 struct GrpcSetupOption {
195  using Type = std::function<void(grpc::ClientContext&)>;
196 };
197 
198 /**
199  * A generic function to directly configure a gRPC context for polling
200  * long-running operations.
201  *
202  * This function takes effect before the context is used to make any poll or
203  * cancel requests for long-running operations.
204  *
205  * @warning It is NOT recommended to call `set_auth_context()` or
206  * `set_credentials()` directly on the context. Instead, use the Google
207  * Unified Auth Credentials library, via
208  * #google::cloud::UnifiedCredentialsOption.
209  */
210 struct GrpcSetupPollOption {
211  using Type = std::function<void(grpc::ClientContext&)>;
212 };
213 
214 /// Configure the ClientContext using options.
215 void ConfigureContext(grpc::ClientContext& context, Options const& opts);
216 
217 /// Configure the ClientContext for polling operations using options.
218 void ConfigurePollContext(grpc::ClientContext& context, Options const& opts);
219 
220 /// Creates a new `grpc::ChannelArguments` configured with @p opts.
221 grpc::ChannelArguments MakeChannelArguments(Options const& opts);
222 
223 /// Helper function to extract the first instance of an integer channel argument
224 absl::optional<int> GetIntChannelArgument(grpc::ChannelArguments const& args,
225  std::string const& key);
226 
227 /// Helper function to extract the first instance of a string channel argument
228 absl::optional<std::string> GetStringChannelArgument(
229  grpc::ChannelArguments const& args, std::string const& key);
230 
231 /**
232  * Returns a factory for generating `BackgroundThreads`.
233  *
234  * If `GrpcBackgroundThreadsFactoryOption` is unset, it will return a thread
235  * pool of size `GrpcBackgroundThreadPoolSizeOption`.
236  */
237 BackgroundThreadsFactory MakeBackgroundThreadsFactory(Options const& opts = {});
238 
239 } // namespace internal
241 } // namespace cloud
242 } // namespace google
243 
244 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_GRPC_OPTIONS_H