-
Notifications
You must be signed in to change notification settings - Fork 753
/
GrpcChannel.cs
866 lines (755 loc) · 35.4 KB
/
GrpcChannel.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
#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 System.Collections.Concurrent;
using Grpc.Core;
#if SUPPORT_LOAD_BALANCING
using Grpc.Net.Client.Balancer;
using Grpc.Net.Client.Balancer.Internal;
#endif
using Grpc.Net.Client.Configuration;
using Grpc.Net.Client.Internal;
using Grpc.Net.Client.Internal.Retry;
using Grpc.Net.Compression;
using Grpc.Shared;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Grpc.Net.Client;
/// <summary>
/// Represents a gRPC channel. Channels are an abstraction of long-lived connections to remote servers.
/// Client objects can reuse the same channel. Creating a channel is an expensive operation compared to invoking
/// a remote call so in general you should reuse a single channel for as many calls as possible.
/// </summary>
public sealed class GrpcChannel : ChannelBase, IDisposable
{
internal const int DefaultMaxReceiveMessageSize = 1024 * 1024 * 4; // 4 MB
#if SUPPORT_LOAD_BALANCING
internal const long DefaultInitialReconnectBackoffTicks = TimeSpan.TicksPerSecond * 1;
internal const long DefaultMaxReconnectBackoffTicks = TimeSpan.TicksPerSecond * 120;
#endif
internal const int DefaultMaxRetryAttempts = 5;
internal const long DefaultMaxRetryBufferSize = 1024 * 1024 * 16; // 16 MB
internal const long DefaultMaxRetryBufferPerCallSize = 1024 * 1024; // 1 MB
private readonly object _lock;
private readonly ConcurrentDictionary<IMethod, GrpcMethodInfo> _methodInfoCache;
private readonly Func<IMethod, GrpcMethodInfo> _createMethodInfoFunc;
private readonly Dictionary<MethodKey, MethodConfig>? _serviceConfigMethods;
private readonly bool _isSecure;
private readonly List<CallCredentials>? _callCredentials;
// Internal for testing
internal readonly HashSet<IDisposable> ActiveCalls;
internal Uri Address { get; }
internal HttpMessageInvoker HttpInvoker { get; }
internal TimeSpan? ConnectTimeout { get; }
internal HttpHandlerType HttpHandlerType { get; }
internal TimeSpan InitialReconnectBackoff { get; }
internal TimeSpan? MaxReconnectBackoff { get; }
internal int? SendMaxMessageSize { get; }
internal int? ReceiveMaxMessageSize { get; }
internal int? MaxRetryAttempts { get; }
internal long? MaxRetryBufferSize { get; }
internal long? MaxRetryBufferPerCallSize { get; }
internal ILoggerFactory LoggerFactory { get; }
internal ILogger Logger { get; }
internal bool ThrowOperationCanceledOnCancellation { get; }
internal bool UnsafeUseInsecureChannelCallCredentials { get; }
internal bool IsSecure => _isSecure;
internal List<CallCredentials>? CallCredentials => _callCredentials;
internal Dictionary<string, ICompressionProvider> CompressionProviders { get; }
internal string MessageAcceptEncoding { get; }
internal bool Disposed { get; private set; }
#if SUPPORT_LOAD_BALANCING
// Load balancing
internal ConnectionManager ConnectionManager { get; }
// Set in unit tests
internal ISubchannelTransportFactory SubchannelTransportFactory;
#endif
// Stateful
internal ChannelRetryThrottling? RetryThrottling { get; }
internal long CurrentRetryBufferSize;
// Options that are set in unit tests
internal ISystemClock Clock = SystemClock.Instance;
internal IOperatingSystem OperatingSystem;
internal IRandomGenerator RandomGenerator;
internal bool DisableClientDeadline;
internal long MaxTimerDueTime = uint.MaxValue - 1; // Max System.Threading.Timer due time
private readonly bool _shouldDisposeHttpClient;
internal GrpcChannel(Uri address, GrpcChannelOptions channelOptions) : base(address.Authority)
{
_lock = new object();
_methodInfoCache = new ConcurrentDictionary<IMethod, GrpcMethodInfo>();
// Dispose the HTTP client/handler if...
// 1. No client/handler was specified and so the channel created the client itself
// 2. User has specified a client/handler and set DisposeHttpClient to true
_shouldDisposeHttpClient = (channelOptions.HttpClient == null && channelOptions.HttpHandler == null)
|| channelOptions.DisposeHttpClient;
Address = address;
LoggerFactory = channelOptions.LoggerFactory ?? channelOptions.ResolveService<ILoggerFactory>(NullLoggerFactory.Instance);
OperatingSystem = channelOptions.ResolveService<IOperatingSystem>(Internal.OperatingSystem.Instance);
RandomGenerator = channelOptions.ResolveService<IRandomGenerator>(new RandomGenerator());
Logger = LoggerFactory.CreateLogger<GrpcChannel>();
#if SUPPORT_LOAD_BALANCING
InitialReconnectBackoff = channelOptions.InitialReconnectBackoff;
MaxReconnectBackoff = channelOptions.MaxReconnectBackoff;
var resolverFactory = GetResolverFactory(channelOptions);
ResolveCredentials(channelOptions, out _isSecure, out _callCredentials);
(HttpHandlerType, ConnectTimeout) = CalculateHandlerContext(Logger, address, _isSecure, channelOptions);
SubchannelTransportFactory = channelOptions.ResolveService<ISubchannelTransportFactory>(new SubChannelTransportFactory(this));
if (!IsHttpOrHttpsAddress(Address) || channelOptions.ServiceConfig?.LoadBalancingConfigs.Count > 0)
{
ValidateHttpHandlerSupportsConnectivity();
}
var defaultPort = IsSecure ? 443 : 80;
var resolver = resolverFactory.Create(new ResolverOptions(Address, defaultPort, LoggerFactory, channelOptions));
ConnectionManager = new ConnectionManager(
resolver,
channelOptions.DisableResolverServiceConfig,
LoggerFactory,
channelOptions.ResolveService<IBackoffPolicyFactory>(new ExponentialBackoffPolicyFactory(RandomGenerator, InitialReconnectBackoff, MaxReconnectBackoff)),
SubchannelTransportFactory,
ResolveLoadBalancerFactories(channelOptions));
ConnectionManager.ConfigureBalancer(c => new ChildHandlerLoadBalancer(
c,
channelOptions.ServiceConfig,
ConnectionManager));
#else
if (string.IsNullOrEmpty(address.Host))
{
throw new ArgumentException($"Address '{address.OriginalString}' doesn't have a host. Address should include a scheme, host, and optional port. For example, 'https://localhost:5001'.");
}
ResolveCredentials(channelOptions, out _isSecure, out _callCredentials);
(HttpHandlerType, ConnectTimeout) = CalculateHandlerContext(Logger, address, _isSecure, channelOptions);
#endif
HttpInvoker = channelOptions.HttpClient ?? CreateInternalHttpInvoker(channelOptions.HttpHandler);
SendMaxMessageSize = channelOptions.MaxSendMessageSize;
ReceiveMaxMessageSize = channelOptions.MaxReceiveMessageSize;
MaxRetryAttempts = channelOptions.MaxRetryAttempts;
MaxRetryBufferSize = channelOptions.MaxRetryBufferSize;
MaxRetryBufferPerCallSize = channelOptions.MaxRetryBufferPerCallSize;
CompressionProviders = ResolveCompressionProviders(channelOptions.CompressionProviders);
MessageAcceptEncoding = GrpcProtocolHelpers.GetMessageAcceptEncoding(CompressionProviders);
ThrowOperationCanceledOnCancellation = channelOptions.ThrowOperationCanceledOnCancellation;
UnsafeUseInsecureChannelCallCredentials = channelOptions.UnsafeUseInsecureChannelCallCredentials;
_createMethodInfoFunc = CreateMethodInfo;
ActiveCalls = new HashSet<IDisposable>();
if (channelOptions.ServiceConfig is { } serviceConfig)
{
RetryThrottling = serviceConfig.RetryThrottling != null ? CreateChannelRetryThrottling(serviceConfig.RetryThrottling) : null;
_serviceConfigMethods = CreateServiceConfigMethods(serviceConfig);
}
// Non-HTTP addresses (e.g. dns:///custom-hostname) usually specify a path instead of an authority.
// Only log about a path being present if HTTP or HTTPS.
if (!string.IsNullOrEmpty(Address.PathAndQuery) &&
Address.PathAndQuery != "/" &&
(Address.Scheme == Uri.UriSchemeHttps || Address.Scheme == Uri.UriSchemeHttp))
{
Log.AddressPathUnused(Logger, Address.OriginalString);
}
}
private void ResolveCredentials(GrpcChannelOptions channelOptions, out bool isSecure, out List<CallCredentials>? callCredentials)
{
if (channelOptions.Credentials != null)
{
var configurator = new DefaultChannelCredentialsConfigurator(channelOptions.UnsafeUseInsecureChannelCallCredentials);
channelOptions.Credentials.InternalPopulateConfiguration(configurator, channelOptions.Credentials);
isSecure = configurator.IsSecure ?? false;
callCredentials = configurator.CallCredentials;
ValidateChannelCredentials();
}
else
{
if (Address.Scheme == Uri.UriSchemeHttp)
{
isSecure = false;
}
else if (Address.Scheme == Uri.UriSchemeHttps)
{
isSecure = true;
}
else
{
throw new InvalidOperationException($"Unable to determine the TLS configuration of the channel from address '{Address}'. " +
$"{nameof(GrpcChannelOptions)}.{nameof(GrpcChannelOptions.Credentials)} must be specified when the address doesn't have a 'http' or 'https' scheme. " +
$"To call TLS endpoints, set credentials to '{nameof(ChannelCredentials)}.{nameof(ChannelCredentials.SecureSsl)}'. " +
$"To call non-TLS endpoints, set credentials to '{nameof(ChannelCredentials)}.{nameof(ChannelCredentials.Insecure)}'.");
}
callCredentials = null;
}
}
private static bool IsHttpOrHttpsAddress(Uri address)
{
return address.Scheme == Uri.UriSchemeHttps || address.Scheme == Uri.UriSchemeHttp;
}
private static HttpHandlerContext CalculateHandlerContext(ILogger logger, Uri address, bool isSecure, GrpcChannelOptions channelOptions)
{
if (channelOptions.HttpHandler == null)
{
// No way to know what handler a HttpClient is using so assume custom.
var type = channelOptions.HttpClient == null
? HttpHandlerType.SocketsHttpHandler
: HttpHandlerType.Custom;
return new HttpHandlerContext(type);
}
if (HttpRequestHelpers.HasHttpHandlerType(channelOptions.HttpHandler, "System.Net.Http.WinHttpHandler"))
{
return new HttpHandlerContext(HttpHandlerType.WinHttpHandler);
}
if (HttpRequestHelpers.HasHttpHandlerType(channelOptions.HttpHandler, "System.Net.Http.SocketsHttpHandler"))
{
HttpHandlerType type;
TimeSpan? connectTimeout;
#if NET5_0_OR_GREATER
var socketsHttpHandler = HttpRequestHelpers.GetHttpHandlerType<SocketsHttpHandler>(channelOptions.HttpHandler)!;
type = HttpHandlerType.SocketsHttpHandler;
connectTimeout = socketsHttpHandler.ConnectTimeout;
// Check if the SocketsHttpHandler is being shared by channels.
// It has already been setup by another channel (i.e. ConnectCallback is set) then
// additional channels can use advanced connectivity features.
if (!BalancerHttpHandler.IsSocketsHttpHandlerSetup(socketsHttpHandler))
{
// Someone has already configured the handler callback.
// This channel can't support advanced connectivity features.
if (socketsHttpHandler.ConnectCallback != null)
{
type = HttpHandlerType.Custom;
connectTimeout = null;
}
}
// If a proxy is specified then requests could be sent via an SSL tunnel.
// A CONNECT request is made to the proxy to establish the transport stream and then
// gRPC calls are sent via stream. This feature isn't supported by load balancer.
// Proxy can be specified via:
// - SocketsHttpHandler.Proxy. Set via app code.
// - HttpClient.DefaultProxy. Set via environment variables, e.g. HTTPS_PROXY.
if (type == HttpHandlerType.SocketsHttpHandler)
{
if (IsProxied(socketsHttpHandler, address, isSecure))
{
logger.LogInformation("Proxy configuration is detected. How the gRPC client creates connections can cause unexpected behavior when a proxy is configured. " +
"To ensure the client correctly uses a proxy, configure GrpcChannelOptions.HttpHandler to use HttpClientHandler. " +
"Note that HttpClientHandler isn't compatible with load balancing.");
}
}
#else
type = HttpHandlerType.SocketsHttpHandler;
connectTimeout = null;
#endif
return new HttpHandlerContext(type, connectTimeout);
}
if (HttpRequestHelpers.GetHttpHandlerType<HttpClientHandler>(channelOptions.HttpHandler) != null)
{
return new HttpHandlerContext(HttpHandlerType.HttpClientHandler);
}
return new HttpHandlerContext(HttpHandlerType.Custom);
}
#if NET5_0_OR_GREATER
private static readonly Uri HttpLoadBalancerTemporaryUri = new Uri("http://loadbalancer.temporary.invalid");
private static readonly Uri HttpsLoadBalancerTemporaryUri = new Uri("https://loadbalancer.temporary.invalid");
private static bool IsProxied(SocketsHttpHandler socketsHttpHandler, Uri address, bool isSecure)
{
// Check standard address directly.
// When load balancing the channel doesn't know the final addresses yet so use temporary address.
Uri resolvedAddress;
if (IsHttpOrHttpsAddress(address))
{
resolvedAddress = address;
}
else if (isSecure)
{
resolvedAddress = HttpsLoadBalancerTemporaryUri;
}
else
{
resolvedAddress = HttpLoadBalancerTemporaryUri;
}
var proxy = socketsHttpHandler.Proxy ?? HttpClient.DefaultProxy;
return proxy.GetProxy(resolvedAddress) != null;
}
#endif
#if SUPPORT_LOAD_BALANCING
private ResolverFactory GetResolverFactory(GrpcChannelOptions options)
{
// Special case http and https schemes. These schemes don't use a dynamic resolver. An http/https
// address is always just one address and that is enabled using the static resolver.
//
// Even with just one address we still want to use the load balancing infrastructure. This enables
// the connectivity APIs on channel like GrpcChannel.State and GrpcChannel.WaitForStateChanged.
if (IsHttpOrHttpsAddress(Address))
{
return new StaticResolverFactory(uri => new[] { new BalancerAddress(Address.Host, Address.Port) });
}
var factories = options.ResolveService<IEnumerable<ResolverFactory>>(Array.Empty<ResolverFactory>());
factories = factories.Union(ResolverFactory.KnownLoadResolverFactories);
foreach (var factory in factories)
{
if (string.Equals(factory.Name, Address.Scheme, StringComparison.OrdinalIgnoreCase))
{
return factory;
}
}
throw new InvalidOperationException($"No address resolver configured for the scheme '{Address.Scheme}'.");
}
private void ValidateHttpHandlerSupportsConnectivity()
{
if (HttpHandlerType == HttpHandlerType.SocketsHttpHandler)
{
// SocketsHttpHandler is being used.
return;
}
if (SubchannelTransportFactory is not SubChannelTransportFactory)
{
// Custom transport is configured. Probably in a unit test.
return;
}
// Either the HTTP transport was configured with HttpClient, or SocketsHttpHandler.ConnectCallback is set.
// We don't know how HTTP requests will be sent so we throw an error.
throw new InvalidOperationException(
$"Channel is configured with an HTTP transport doesn't support client-side load balancing or connectivity state tracking. " +
$"The underlying HTTP transport must be a {nameof(SocketsHttpHandler)} with no " +
$"{nameof(SocketsHttpHandler)}.{nameof(SocketsHttpHandler.ConnectCallback)} configured. " +
$"The HTTP transport must be configured on the channel using {nameof(GrpcChannelOptions)}.{nameof(GrpcChannelOptions.HttpHandler)}.");
}
private LoadBalancerFactory[] ResolveLoadBalancerFactories(GrpcChannelOptions channelOptions)
{
var serviceFactories = channelOptions.ResolveService<IEnumerable<LoadBalancerFactory>?>(defaultValue: null);
if (serviceFactories != null)
{
return serviceFactories.Union(LoadBalancerFactory.KnownLoadBalancerFactories).ToArray();
}
return LoadBalancerFactory.KnownLoadBalancerFactories;
}
#endif
private ChannelRetryThrottling CreateChannelRetryThrottling(RetryThrottlingPolicy retryThrottling)
{
if (retryThrottling.MaxTokens == null)
{
throw CreateException(RetryThrottlingPolicy.MaxTokensPropertyName);
}
if (retryThrottling.TokenRatio == null)
{
throw CreateException(RetryThrottlingPolicy.TokenRatioPropertyName);
}
return new ChannelRetryThrottling(retryThrottling.MaxTokens.Value, retryThrottling.TokenRatio.Value, LoggerFactory);
static InvalidOperationException CreateException(string propertyName)
{
return new InvalidOperationException($"Retry throttling missing required property '{propertyName}'.");
}
}
private static Dictionary<MethodKey, MethodConfig> CreateServiceConfigMethods(ServiceConfig serviceConfig)
{
var configs = new Dictionary<MethodKey, MethodConfig>();
for (var i = 0; i < serviceConfig.MethodConfigs.Count; i++)
{
var methodConfig = serviceConfig.MethodConfigs[i];
for (var j = 0; j < methodConfig.Names.Count; j++)
{
var name = methodConfig.Names[j];
var methodKey = new MethodKey(name.Service, name.Method);
if (configs.ContainsKey(methodKey))
{
throw new InvalidOperationException($"Duplicate method config found. Service: '{name.Service}', method: '{name.Method}'.");
}
configs[methodKey] = methodConfig;
}
}
return configs;
}
private HttpMessageInvoker CreateInternalHttpInvoker(HttpMessageHandler? handler)
{
// HttpMessageInvoker should always dispose handler if Disposed is called on it.
// Decision to dispose invoker is controlled by _shouldDisposeHttpClient.
if (handler == null)
{
handler = HttpHandlerFactory.CreatePrimaryHandler();
}
else
{
// Validate the user specified handler is compatible with this platform.
//
// Android's native handler doesn't fully support HTTP/2 and using it could cause hard to understand errors
// in advanced gRPC scenarios. We want Android to use SocketsHttpHandler. Throw an error if:
// 1. Client is running on Android.
// 2. Channel is created with HttpClientHandler.
// 3. Channel is not using GrpcWebHandler. grpc-web is compatible with the native handler.
// 4. UseNativeHttpHandler switch is true.
if (OperatingSystem.IsAndroid)
{
// GetHttpHandlerType recurses through DelegatingHandlers that may wrap the HttpClientHandler.
var httpClientHandler = HttpRequestHelpers.GetHttpHandlerType<HttpClientHandler>(handler);
var grpcWebHandler = HttpRequestHelpers.GetHttpHandlerType(handler, "Grpc.Net.Client.Web.GrpcWebHandler");
if (httpClientHandler != null && grpcWebHandler == null && RuntimeHelpers.QueryRuntimeSettingSwitch("System.Net.Http.UseNativeHttpHandler", defaultValue: false))
{
throw new InvalidOperationException("The channel configuration isn't valid on Android devices. " +
"The channel is configured to use HttpClientHandler and Android's native HTTP/2 library. " +
"gRPC isn't fully supported by Android's native HTTP/2 library and it can cause runtime errors. " +
"To fix this problem, either configure the channel to use SocketsHttpHandler, or add " +
"<UseNativeHttpHandler>false</UseNativeHttpHandler> to the app's project file. " +
"For more information, see https://aka.ms/aspnet/grpc/android.");
}
}
}
#if NET5_0
handler = HttpHandlerFactory.EnsureTelemetryHandler(handler);
#endif
#if SUPPORT_LOAD_BALANCING
BalancerHttpHandler balancerHttpHandler;
handler = balancerHttpHandler = new BalancerHttpHandler(handler, ConnectionManager);
if (HttpHandlerType == HttpHandlerType.SocketsHttpHandler)
{
var socketsHttpHandler = HttpRequestHelpers.GetHttpHandlerType<SocketsHttpHandler>(handler);
CompatibilityHelpers.Assert(socketsHttpHandler != null, "Should have handler with this handler type.");
BalancerHttpHandler.ConfigureSocketsHttpHandlerSetup(socketsHttpHandler, balancerHttpHandler.OnConnect);
}
#endif
// Use HttpMessageInvoker instead of HttpClient because it is faster
// and we don't need client's features.
var httpInvoker = new HttpMessageInvoker(handler, disposeHandler: true);
return httpInvoker;
}
internal void RegisterActiveCall(IDisposable grpcCall)
{
lock (_lock)
{
ActiveCalls.Add(grpcCall);
}
}
internal void FinishActiveCall(IDisposable grpcCall)
{
lock (_lock)
{
ActiveCalls.Remove(grpcCall);
}
}
internal GrpcMethodInfo GetCachedGrpcMethodInfo(IMethod method)
{
return _methodInfoCache.GetOrAdd(method, _createMethodInfoFunc);
}
private GrpcMethodInfo CreateMethodInfo(IMethod method)
{
var uri = new Uri(method.FullName, UriKind.Relative);
var scope = new GrpcCallScope(method.Type, uri);
var methodConfig = ResolveMethodConfig(method);
var uriBuilder = new UriBuilder(Address);
uriBuilder.Path = method.FullName;
// The Uri used to create HttpRequestMessage must have a http or https scheme.
uriBuilder.Scheme = IsSecure ? Uri.UriSchemeHttps : Uri.UriSchemeHttp;
// A Uri with a http or https scheme requires a host name.
// Triple slash URIs, e.g. dns:///custom-value, won't have a host and UriBuilder throws an error.
// Add a temp value as the host. The tempuri.org host may show up in some logging but it will
// get replaced in the final HTTP request address by the load balancer.
if (string.IsNullOrEmpty(uriBuilder.Host))
{
// .invalid is reserved for temporary host names.
// https://datatracker.ietf.org/doc/html/rfc2606#section-2
uriBuilder.Host = "loadbalancer.temporary.invalid";
}
return new GrpcMethodInfo(scope, uriBuilder.Uri, methodConfig);
}
private MethodConfig? ResolveMethodConfig(IMethod method)
{
if (_serviceConfigMethods != null)
{
MethodConfig? methodConfig;
if (_serviceConfigMethods.TryGetValue(new MethodKey(method.ServiceName, method.Name), out methodConfig))
{
return methodConfig;
}
if (_serviceConfigMethods.TryGetValue(new MethodKey(method.ServiceName, null), out methodConfig))
{
return methodConfig;
}
if (_serviceConfigMethods.TryGetValue(new MethodKey(null, null), out methodConfig))
{
return methodConfig;
}
}
return null;
}
private static Dictionary<string, ICompressionProvider> ResolveCompressionProviders(IList<ICompressionProvider>? compressionProviders)
{
if (compressionProviders == null)
{
return GrpcProtocolConstants.DefaultCompressionProviders;
}
var resolvedCompressionProviders = new Dictionary<string, ICompressionProvider>(StringComparer.Ordinal);
for (var i = 0; i < compressionProviders.Count; i++)
{
var compressionProvider = compressionProviders[i];
if (!resolvedCompressionProviders.ContainsKey(compressionProvider.EncodingName))
{
resolvedCompressionProviders.Add(compressionProvider.EncodingName, compressionProvider);
}
}
return resolvedCompressionProviders;
}
private void ValidateChannelCredentials()
{
if (IsSecure && Address.Scheme == Uri.UriSchemeHttp)
{
throw new InvalidOperationException($"Channel is configured with secure channel credentials and can't use a HttpClient with a '{Address.Scheme}' scheme.");
}
if (!IsSecure && Address.Scheme == Uri.UriSchemeHttps)
{
throw new InvalidOperationException($"Channel is configured with insecure channel credentials and can't use a HttpClient with a '{Address.Scheme}' scheme.");
}
}
/// <summary>
/// Create a new <see cref="CallInvoker"/> for the channel.
/// </summary>
/// <returns>A new <see cref="CallInvoker"/>.</returns>
public override CallInvoker CreateCallInvoker()
{
if (Disposed)
{
throw new ObjectDisposedException(nameof(GrpcChannel));
}
var invoker = new HttpClientCallInvoker(this);
return invoker;
}
/// <summary>
/// Creates a <see cref="GrpcChannel"/> for the specified address.
/// </summary>
/// <param name="address">The address the channel will use.</param>
/// <returns>A new instance of <see cref="GrpcChannel"/>.</returns>
public static GrpcChannel ForAddress(string address)
{
return ForAddress(address, new GrpcChannelOptions());
}
/// <summary>
/// Creates a <see cref="GrpcChannel"/> for the specified address and configuration options.
/// </summary>
/// <param name="address">The address the channel will use.</param>
/// <param name="channelOptions">The channel configuration options.</param>
/// <returns>A new instance of <see cref="GrpcChannel"/>.</returns>
public static GrpcChannel ForAddress(string address, GrpcChannelOptions channelOptions)
{
return ForAddress(new Uri(address), channelOptions);
}
/// <summary>
/// Creates a <see cref="GrpcChannel"/> for the specified address.
/// </summary>
/// <param name="address">The address the channel will use.</param>
/// <returns>A new instance of <see cref="GrpcChannel"/>.</returns>
public static GrpcChannel ForAddress(Uri address)
{
return ForAddress(address, new GrpcChannelOptions());
}
/// <summary>
/// Creates a <see cref="GrpcChannel"/> for the specified address and configuration options.
/// </summary>
/// <param name="address">The address the channel will use.</param>
/// <param name="channelOptions">The channel configuration options.</param>
/// <returns>A new instance of <see cref="GrpcChannel"/>.</returns>
public static GrpcChannel ForAddress(Uri address, GrpcChannelOptions channelOptions)
{
if (address == null)
{
throw new ArgumentNullException(nameof(address));
}
if (channelOptions == null)
{
throw new ArgumentNullException(nameof(channelOptions));
}
if (channelOptions.HttpClient != null && channelOptions.HttpHandler != null)
{
throw new ArgumentException($"{nameof(GrpcChannelOptions.HttpClient)} and {nameof(GrpcChannelOptions.HttpHandler)} have been configured. " +
$"Only one HTTP caller can be specified.");
}
return new GrpcChannel(address, channelOptions);
}
#if SUPPORT_LOAD_BALANCING
/// <summary>
/// Allows explicitly requesting channel to connect without starting an RPC.
/// Returned task completes once <see cref="State"/> Ready was seen.
/// There is no need to call this explicitly unless your use case requires that.
/// Starting an RPC on a new channel will request connection implicitly.
/// <para>
/// This API is only supported when the channel is configured with a
/// <see cref="SocketsHttpHandler"/> HTTP transport.
/// </para>
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public Task ConnectAsync(CancellationToken cancellationToken = default)
{
if (Disposed)
{
throw new ObjectDisposedException(nameof(GrpcChannel));
}
ValidateHttpHandlerSupportsConnectivity();
return ConnectionManager.ConnectAsync(waitForReady: true, cancellationToken);
}
/// <summary>
/// Gets current connectivity state of this channel.
/// After the channel has been shutdown, <see cref="ConnectivityState.Shutdown"/> is returned.
/// <para>
/// This API is only supported when the channel is configured with a
/// <see cref="SocketsHttpHandler"/> HTTP transport.
/// </para>
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
public ConnectivityState State
{
get
{
ValidateHttpHandlerSupportsConnectivity();
return ConnectionManager.State;
}
}
/// <summary>
/// Wait for channel's state to change. The task completes when <see cref="State"/> becomes
/// different from <paramref name="lastObservedState"/>.
/// <para>
/// This API is only supported when the channel is configured with a
/// <see cref="SocketsHttpHandler"/> HTTP transport.
/// </para>
/// <para>
/// Note: Experimental API that can change or be removed without any prior notice.
/// </para>
/// </summary>
/// <param name="lastObservedState">The last observed state. The task completes when <see cref="State"/> becomes different from this value.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public Task WaitForStateChangedAsync(ConnectivityState lastObservedState, CancellationToken cancellationToken = default)
{
if (Disposed)
{
throw new ObjectDisposedException(nameof(GrpcChannel));
}
ValidateHttpHandlerSupportsConnectivity();
return ConnectionManager.WaitForStateChangedAsync(lastObservedState, waitForState: null, cancellationToken);
}
#endif
/// <summary>
/// Releases the resources used by the <see cref="GrpcChannel"/> class.
/// Clients created with the channel can't be used after the channel is disposed.
/// </summary>
public void Dispose()
{
if (Disposed)
{
return;
}
lock (_lock)
{
if (ActiveCalls.Count > 0)
{
// Disposing a call will remove it from ActiveCalls. Need to take a copy
// to avoid enumeration from being modified
var activeCallsCopy = ActiveCalls.ToArray();
foreach (var activeCall in activeCallsCopy)
{
activeCall.Dispose();
}
}
}
if (_shouldDisposeHttpClient)
{
HttpInvoker.Dispose();
}
#if SUPPORT_LOAD_BALANCING
ConnectionManager.Dispose();
#endif
Disposed = true;
}
internal bool TryAddToRetryBuffer(long messageSize)
{
lock (_lock)
{
if (CurrentRetryBufferSize + messageSize > MaxRetryBufferSize)
{
return false;
}
CurrentRetryBufferSize += messageSize;
return true;
}
}
internal void RemoveFromRetryBuffer(long messageSize)
{
lock (_lock)
{
CurrentRetryBufferSize -= messageSize;
}
}
internal int GetRandomNumber(int minValue, int maxValue)
{
CompatibilityHelpers.Assert(RandomGenerator != null);
lock (_lock)
{
return RandomGenerator.Next(minValue, maxValue);
}
}
#if SUPPORT_LOAD_BALANCING
private sealed class SubChannelTransportFactory : ISubchannelTransportFactory
{
private readonly GrpcChannel _channel;
public SubChannelTransportFactory(GrpcChannel channel)
{
_channel = channel;
}
public ISubchannelTransport Create(Subchannel subchannel)
{
if (_channel.HttpHandlerType == HttpHandlerType.SocketsHttpHandler)
{
return new SocketConnectivitySubchannelTransport(
subchannel,
TimeSpan.FromSeconds(5),
_channel.ConnectTimeout,
_channel.LoggerFactory,
socketConnect: null);
}
return new PassiveSubchannelTransport(subchannel);
}
}
#endif
private readonly struct MethodKey : IEquatable<MethodKey>
{
public MethodKey(string? service, string? method)
{
Service = service;
Method = method;
}
public string? Service { get; }
public string? Method { get; }
public override bool Equals(object? obj) => obj is MethodKey n ? Equals(n) : false;
// Service and method names are case sensitive.
public bool Equals(MethodKey other) => other.Service == Service && other.Method == Method;
public override int GetHashCode() =>
(Service != null ? StringComparer.Ordinal.GetHashCode(Service) : 0) ^
(Method != null ? StringComparer.Ordinal.GetHashCode(Method) : 0);
}
private static class Log
{
private static readonly Action<ILogger, string, Exception?> _addressPathUnused =
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "AddressPathUnused"), "The path in the channel's address '{Address}' won't be used when making gRPC calls. A DelegatingHandler can be used to include a path with gRPC calls. See https://aka.ms/aspnet/grpc/subdir for details.");
public static void AddressPathUnused(ILogger logger, string address)
{
_addressPathUnused(logger, address, null);
}
}
private readonly record struct HttpHandlerContext(HttpHandlerType HttpHandlerType, TimeSpan? ConnectTimeout = null);
}
internal enum HttpHandlerType
{
SocketsHttpHandler,
HttpClientHandler,
WinHttpHandler,
Custom
}