-
Notifications
You must be signed in to change notification settings - Fork 791
/
Copy pathGrpcChannelOptions.cs
338 lines (311 loc) · 14.3 KB
/
GrpcChannelOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#region Copyright notice and license
// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using Grpc.Core;
using Grpc.Net.Client.Configuration;
using Grpc.Net.Compression;
using Microsoft.Extensions.Logging;
namespace Grpc.Net.Client;
/// <summary>
/// An options class for configuring a <see cref="GrpcChannel"/>.
/// </summary>
public sealed class GrpcChannelOptions
{
#if SUPPORT_LOAD_BALANCING
private TimeSpan _initialReconnectBackoff;
private TimeSpan? _maxReconnectBackoff;
#endif
/// <summary>
/// Gets or sets the credentials for the channel. This setting is used to set <see cref="ChannelCredentials"/> for
/// a channel. Connection transport layer security (TLS) is determined by the address used to create the channel.
/// </summary>
/// <remarks>
/// <para>
/// The channel credentials you use must match the address TLS setting. Use <see cref="ChannelCredentials.Insecure"/>
/// for an "http" address and <see cref="ChannelCredentials.SecureSsl"/> for "https".
/// </para>
/// <para>
/// The underlying <see cref="System.Net.Http.HttpClient"/> used by the channel automatically loads root certificates
/// from the operating system certificate store.
/// Client certificates should be configured on HttpClient. See <see href="https://aka.ms/aspnet/grpc/certauth"/> for details.
/// </para>
/// </remarks>
public ChannelCredentials? Credentials { get; set; }
/// <summary>
/// Gets or sets the maximum message size in bytes that can be sent from the client. Attempting to send a message
/// that exceeds the configured maximum message size results in an exception.
/// <para>
/// A <c>null</c> value removes the maximum message size limit. Defaults to <c>null</c>.
/// </para>
/// </summary>
public int? MaxSendMessageSize { get; set; }
/// <summary>
/// Gets or sets the maximum message size in bytes that can be received by the client. If the client receives a
/// message that exceeds this limit, it throws an exception.
/// <para>
/// A <c>null</c> value removes the maximum message size limit. Defaults to 4,194,304 (4 MB).
/// </para>
/// </summary>
public int? MaxReceiveMessageSize { get; set; }
/// <summary>
/// Gets or sets the maximum retry attempts. This value limits any retry and hedging attempt values specified in
/// the service config.
/// <para>
/// Setting this value alone doesn't enable retries. Retries are enabled in the service config, which can be done
/// using <see cref="ServiceConfig"/>.
/// </para>
/// <para>
/// A <c>null</c> value removes the maximum retry attempts limit. Defaults to 5.
/// </para>
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public int? MaxRetryAttempts { get; set; }
/// <summary>
/// Gets or sets the maximum buffer size in bytes that can be used to store sent messages when retrying
/// or hedging calls. If the buffer limit is exceeded, then no more retry attempts are made and all
/// hedging calls but one will be canceled. This limit is applied across all calls made using the channel.
/// <para>
/// Setting this value alone doesn't enable retries. Retries are enabled in the service config, which can be done
/// using <see cref="ServiceConfig"/>.
/// </para>
/// <para>
/// A <c>null</c> value removes the maximum retry buffer size limit. Defaults to 16,777,216 (16 MB).
/// </para>
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public long? MaxRetryBufferSize { get; set; }
/// <summary>
/// Gets or sets the maximum buffer size in bytes that can be used to store sent messages when retrying
/// or hedging calls. If the buffer limit is exceeded, then no more retry attempts are made and all
/// hedging calls but one will be canceled. This limit is applied to one call.
/// <para>
/// Setting this value alone doesn't enable retries. Retries are enabled in the service config, which can be done
/// using <see cref="ServiceConfig"/>.
/// </para>
/// <para>
/// A <c>null</c> value removes the maximum retry buffer size limit per call. Defaults to 1,048,576 (1 MB).
/// </para>
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public long? MaxRetryBufferPerCallSize { get; set; }
/// <summary>
/// Gets or sets a collection of compression providers.
/// </summary>
public IList<ICompressionProvider>? CompressionProviders { get; set; }
/// <summary>
/// Gets or sets the logger factory used by the channel. If no value is specified then the channel
/// attempts to resolve an <see cref="ILoggerFactory"/> from the <see cref="ServiceProvider"/>.
/// </summary>
public ILoggerFactory? LoggerFactory { get; set; }
/// <summary>
/// Gets or sets the <see cref="System.Net.Http.HttpClient"/> used by the channel to make HTTP calls.
/// </summary>
/// <remarks>
/// <para>
/// By default a <see cref="System.Net.Http.HttpClient"/> specified here will not be disposed with the channel.
/// To dispose the <see cref="System.Net.Http.HttpClient"/> with the channel you must set <see cref="DisposeHttpClient"/>
/// to <c>true</c>.
/// </para>
/// <para>
/// Only one HTTP caller can be specified for a channel. An error will be thrown if this is configured
/// together with <see cref="HttpHandler"/>.
/// </para>
/// </remarks>
public HttpClient? HttpClient { get; set; }
/// <summary>
/// Gets or sets the <see cref="HttpMessageHandler"/> used by the channel to make HTTP calls.
/// </summary>
/// <remarks>
/// <para>
/// By default a <see cref="HttpMessageHandler"/> specified here will not be disposed with the channel.
/// To dispose the <see cref="HttpMessageHandler"/> with the channel you must set <see cref="DisposeHttpClient"/>
/// to <c>true</c>.
/// </para>
/// <para>
/// Only one HTTP caller can be specified for a channel. An error will be thrown if this is configured
/// together with <see cref="HttpClient"/>.
/// </para>
/// </remarks>
public HttpMessageHandler? HttpHandler { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the underlying <see cref="System.Net.Http.HttpClient"/> or
/// <see cref="HttpMessageHandler"/> should be disposed when the <see cref="GrpcChannel"/> instance is disposed.
/// The default value is <c>false</c>.
/// </summary>
/// <remarks>
/// This setting is used when a <see cref="HttpClient"/> or <see cref="HttpHandler"/> value is specified.
/// If they are not specified then the channel will create an internal HTTP caller that is always disposed
/// when the channel is disposed.
/// </remarks>
public bool DisposeHttpClient { get; set; }
/// <summary>
/// Gets or sets a value indicating whether clients will throw <see cref="OperationCanceledException"/> for a call when its
/// <see cref="CallOptions.CancellationToken"/> is triggered or its <see cref="CallOptions.Deadline"/> is exceeded.
/// The default value is <c>false</c>.
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public bool ThrowOperationCanceledOnCancellation { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a gRPC call's <see cref="CallCredentials"/> are used by an insecure channel.
/// The default value is <c>false</c>.
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
/// <remarks>
/// <para>
/// The default value for this property is <c>false</c>, which causes an insecure channel to ignore a gRPC call's <see cref="CallCredentials"/>.
/// Sending authentication headers over an insecure connection has security implications and shouldn't be done in production environments.
/// </para>
/// <para>
/// If this property is set to <c>true</c>, call credentials are always used by a channel.
/// </para>
/// </remarks>
public bool UnsafeUseInsecureChannelCallCredentials { get; set; }
/// <summary>
/// Gets or sets the service config for a gRPC channel. A service config allows service owners to publish parameters
/// to be automatically used by all clients of their service. A service config can also be specified by a client
/// using this property.
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public ServiceConfig? ServiceConfig { get; set; }
#if SUPPORT_LOAD_BALANCING
/// <summary>
/// Gets or sets a value indicating whether resolving a service config from the <see cref="Balancer.Resolver"/>
/// is disabled.
/// The default value is <c>false</c>.
/// <para>
/// A hint is provided to the resolver that it shouldn't fetch a service config.
/// If a service config is returned by then resolver then it is ignored.
/// </para>
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public bool DisableResolverServiceConfig { get; set; }
/// <summary>
/// Gets or sets the the maximum time between subsequent connection attempts.
/// <para>
/// The reconnect backoff starts at an initial backoff and then exponentially increases between attempts, up to the maximum reconnect backoff.
/// Reconnect backoff adds a jitter to randomize the backoff. This is done to avoid spikes of connection attempts.
/// </para>
/// <para>
/// A <c>null</c> value removes the maximum reconnect backoff limit. The default value is 120 seconds.
/// </para>
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public TimeSpan? MaxReconnectBackoff
{
get => _maxReconnectBackoff;
set
{
if (value <= TimeSpan.Zero)
{
throw new ArgumentException("Maximum reconnect backoff must be greater than zero.");
}
_maxReconnectBackoff = value;
}
}
/// <summary>
/// Gets or sets the time between the first and second connection attempts.
/// <para>
/// The reconnect backoff starts at an initial backoff and then exponentially increases between attempts, up to the maximum reconnect backoff.
/// Reconnect backoff adds a jitter to randomize the backoff. This is done to avoid spikes of connection attempts.
/// </para>
/// <para>
/// Defaults to 1 second.
/// </para>
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public TimeSpan InitialReconnectBackoff
{
get => _initialReconnectBackoff;
set
{
if (value <= TimeSpan.Zero)
{
throw new ArgumentException("Initial reconnect backoff must be greater than zero.");
}
_initialReconnectBackoff = value;
}
}
#endif
/// <summary>
/// Gets or sets the <see cref="IServiceProvider"/> the channel uses to resolve types.
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public IServiceProvider? ServiceProvider { get; set; }
/// <summary>
/// Gets or sets the HTTP version to use when making gRPC calls.
/// <para>
/// When a <see cref="Version"/> is specified the value will be set on <see cref="HttpRequestMessage.Version"/>
/// as gRPC calls are made. Changing this property allows the HTTP version of gRPC calls to
/// be overridden.
/// </para>
/// <para>
/// A <c>null</c> value doesn't override the HTTP version of gRPC calls. Defaults to <c>2.0</c>.
/// </para>
/// </summary>
public Version? HttpVersion { get; set; }
#if NET5_0_OR_GREATER
/// <summary>
/// Gets or sets the HTTP policy to use when making gRPC calls.
/// <para>
/// When a <see cref="HttpVersionPolicy"/> is specified the value will be set on <see cref="HttpRequestMessage.VersionPolicy"/>
/// as gRPC calls are made. The policy determines how <see cref="Version"/> is interpreted when
/// the final HTTP version is negotiated with the server. Changing this property allows the HTTP
/// version of gRPC calls to be overridden.
/// </para>
/// <para>
/// A <c>null</c> value doesn't override the HTTP policy of gRPC calls. Defaults to <see cref="HttpVersionPolicy.RequestVersionExact"/>.
/// </para>
/// </summary>
public HttpVersionPolicy? HttpVersionPolicy { get; set; }
#endif
internal T ResolveService<T>(T defaultValue)
{
return (T?)ServiceProvider?.GetService(typeof(T)) ?? defaultValue;
}
/// <summary>
/// Initializes a new instance of the <see cref="GrpcChannelOptions"/> class.
/// </summary>
public GrpcChannelOptions()
{
MaxReceiveMessageSize = GrpcChannel.DefaultMaxReceiveMessageSize;
#if SUPPORT_LOAD_BALANCING
_maxReconnectBackoff = TimeSpan.FromTicks(GrpcChannel.DefaultMaxReconnectBackoffTicks);
_initialReconnectBackoff = TimeSpan.FromTicks(GrpcChannel.DefaultInitialReconnectBackoffTicks);
#endif
MaxRetryAttempts = GrpcChannel.DefaultMaxRetryAttempts;
MaxRetryBufferSize = GrpcChannel.DefaultMaxRetryBufferSize;
MaxRetryBufferPerCallSize = GrpcChannel.DefaultMaxRetryBufferPerCallSize;
}
}