-
Notifications
You must be signed in to change notification settings - Fork 494
/
CosmosHttpClientCore.cs
537 lines (465 loc) · 24.4 KB
/
CosmosHttpClientCore.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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Cosmos.Resource.CosmosExceptions;
using Microsoft.Azure.Cosmos.Tracing.TraceData;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Collections;
internal sealed class CosmosHttpClientCore : CosmosHttpClient
{
private readonly HttpClient httpClient;
private readonly ICommunicationEventSource eventSource;
private bool disposedValue;
private CosmosHttpClientCore(
HttpClient httpClient,
HttpMessageHandler httpMessageHandler,
ICommunicationEventSource eventSource)
{
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
this.eventSource = eventSource ?? throw new ArgumentNullException(nameof(eventSource));
this.HttpMessageHandler = httpMessageHandler;
}
public override HttpMessageHandler HttpMessageHandler { get; }
public static CosmosHttpClient CreateWithConnectionPolicy(
ApiType apiType,
ICommunicationEventSource eventSource,
ConnectionPolicy connectionPolicy,
HttpMessageHandler httpMessageHandler,
EventHandler<SendingRequestEventArgs> sendingRequestEventArgs,
EventHandler<ReceivedResponseEventArgs> receivedResponseEventArgs)
{
if (connectionPolicy == null)
{
throw new ArgumentNullException(nameof(connectionPolicy));
}
Func<HttpClient> httpClientFactory = connectionPolicy.HttpClientFactory;
if (httpClientFactory != null)
{
if (sendingRequestEventArgs != null &&
receivedResponseEventArgs != null)
{
throw new InvalidOperationException($"{nameof(connectionPolicy.HttpClientFactory)} can not be set at the same time as {nameof(sendingRequestEventArgs)} or {nameof(ReceivedResponseEventArgs)}");
}
HttpClient userHttpClient = httpClientFactory.Invoke() ?? throw new ArgumentNullException($"{nameof(httpClientFactory)} returned null. {nameof(httpClientFactory)} must return a HttpClient instance.");
return CosmosHttpClientCore.CreateHelper(
httpClient: userHttpClient,
httpMessageHandler: httpMessageHandler,
requestTimeout: connectionPolicy.RequestTimeout,
userAgentContainer: connectionPolicy.UserAgentContainer,
apiType: apiType,
eventSource: eventSource);
}
if (httpMessageHandler == null)
{
httpMessageHandler = CosmosHttpClientCore.CreateHttpClientHandler(
gatewayModeMaxConnectionLimit: connectionPolicy.MaxConnectionLimit,
webProxy: null,
serverCertificateCustomValidationCallback: connectionPolicy.ServerCertificateCustomValidationCallback);
}
if (sendingRequestEventArgs != null ||
receivedResponseEventArgs != null)
{
httpMessageHandler = CosmosHttpClientCore.CreateHttpMessageHandler(
httpMessageHandler,
sendingRequestEventArgs,
receivedResponseEventArgs);
}
HttpClient httpClient = new HttpClient(httpMessageHandler);
return CosmosHttpClientCore.CreateHelper(
httpClient: httpClient,
httpMessageHandler: httpMessageHandler,
requestTimeout: connectionPolicy.RequestTimeout,
userAgentContainer: connectionPolicy.UserAgentContainer,
apiType: apiType,
eventSource: eventSource);
}
public static HttpMessageHandler CreateHttpClientHandler(
int gatewayModeMaxConnectionLimit,
IWebProxy webProxy,
Func<X509Certificate2, X509Chain, SslPolicyErrors, bool> serverCertificateCustomValidationCallback)
{
// TODO: Remove type check and use #if NET6_0_OR_GREATER when multitargetting is possible
Type socketHandlerType = Type.GetType("System.Net.Http.SocketsHttpHandler, System.Net.Http");
if (socketHandlerType != null)
{
try
{
return CosmosHttpClientCore.CreateSocketsHttpHandlerHelper(gatewayModeMaxConnectionLimit, webProxy, serverCertificateCustomValidationCallback);
}
catch (Exception e)
{
DefaultTrace.TraceError("Failed to create SocketsHttpHandler: {0}", e);
}
}
return CosmosHttpClientCore.CreateHttpClientHandlerHelper(gatewayModeMaxConnectionLimit, webProxy, serverCertificateCustomValidationCallback);
}
public static HttpMessageHandler CreateSocketsHttpHandlerHelper(
int gatewayModeMaxConnectionLimit,
IWebProxy webProxy,
Func<X509Certificate2, X509Chain, SslPolicyErrors, bool> serverCertificateCustomValidationCallback)
{
// TODO: Remove Reflection when multitargetting is possible
Type socketHandlerType = Type.GetType("System.Net.Http.SocketsHttpHandler, System.Net.Http");
object socketHttpHandler = Activator.CreateInstance(socketHandlerType);
PropertyInfo pooledConnectionLifetimeInfo = socketHandlerType.GetProperty("PooledConnectionLifetime");
//Sets the timeout for unused connections to a random time between 5 minutes and 5 minutes and 30 seconds.
//This is to avoid the issue where a large number of connections are closed at the same time.
TimeSpan connectionTimeSpan = TimeSpan.FromMinutes(5) + TimeSpan.FromSeconds(30 * CustomTypeExtensions.GetRandomNumber().NextDouble());
pooledConnectionLifetimeInfo.SetValue(socketHttpHandler, connectionTimeSpan);
// Proxy is only set by users and can cause not supported exception on some platforms
if (webProxy != null)
{
PropertyInfo webProxyInfo = socketHandlerType.GetProperty("Proxy");
webProxyInfo.SetValue(socketHttpHandler, webProxy);
}
// https://docs.microsoft.com/en-us/archive/blogs/timomta/controlling-the-number-of-outgoing-connections-from-httpclient-net-core-or-full-framework
try
{
PropertyInfo maxConnectionsPerServerInfo = socketHandlerType.GetProperty("MaxConnectionsPerServer");
maxConnectionsPerServerInfo.SetValue(socketHttpHandler, gatewayModeMaxConnectionLimit);
}
// MaxConnectionsPerServer is not supported on some platforms.
catch (PlatformNotSupportedException)
{
}
if (serverCertificateCustomValidationCallback != null)
{
//Get SslOptions Property
PropertyInfo sslOptionsInfo = socketHandlerType.GetProperty("SslOptions");
object sslOptions = sslOptionsInfo.GetValue(socketHttpHandler);
//Set SslOptions Property with custom certificate validation
PropertyInfo remoteCertificateValidationCallbackInfo = sslOptions.GetType().GetProperty("RemoteCertificateValidationCallback");
remoteCertificateValidationCallbackInfo.SetValue(
sslOptions,
new RemoteCertificateValidationCallback((object _, X509Certificate certificate, X509Chain x509Chain, SslPolicyErrors sslPolicyErrors) => serverCertificateCustomValidationCallback(
certificate is { } ? new X509Certificate2(certificate) : null,
x509Chain,
sslPolicyErrors)));
}
return (HttpMessageHandler)socketHttpHandler;
}
public static HttpMessageHandler CreateHttpClientHandlerHelper(
int gatewayModeMaxConnectionLimit,
IWebProxy webProxy,
Func<X509Certificate2, X509Chain, SslPolicyErrors, bool> serverCertificateCustomValidationCallback)
{
HttpClientHandler httpClientHandler = new HttpClientHandler();
// Proxy is only set by users and can cause not supported exception on some platforms
if (webProxy != null)
{
httpClientHandler.Proxy = webProxy;
}
// https://docs.microsoft.com/en-us/archive/blogs/timomta/controlling-the-number-of-outgoing-connections-from-httpclient-net-core-or-full-framework
try
{
httpClientHandler.MaxConnectionsPerServer = gatewayModeMaxConnectionLimit;
}
// MaxConnectionsPerServer is not supported on some platforms.
catch (PlatformNotSupportedException)
{
}
if (serverCertificateCustomValidationCallback != null)
{
httpClientHandler.ServerCertificateCustomValidationCallback = (_, certificate2, x509Chain, sslPolicyErrors) => serverCertificateCustomValidationCallback(certificate2, x509Chain, sslPolicyErrors);
}
return httpClientHandler;
}
private static HttpMessageHandler CreateHttpMessageHandler(
HttpMessageHandler innerHandler,
EventHandler<SendingRequestEventArgs> sendingRequestEventArgs,
EventHandler<ReceivedResponseEventArgs> receivedResponseEventArgs)
{
return new HttpRequestMessageHandler(
sendingRequestEventArgs,
receivedResponseEventArgs,
innerHandler);
}
private static CosmosHttpClient CreateHelper(
HttpClient httpClient,
HttpMessageHandler httpMessageHandler,
TimeSpan requestTimeout,
UserAgentContainer userAgentContainer,
ApiType apiType,
ICommunicationEventSource eventSource)
{
if (httpClient == null)
{
throw new ArgumentNullException(nameof(httpClient));
}
httpClient.Timeout = requestTimeout > CosmosHttpClientCore.GatewayRequestTimeout
? requestTimeout
: CosmosHttpClientCore.GatewayRequestTimeout;
httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true };
httpClient.AddUserAgentHeader(userAgentContainer);
httpClient.AddApiTypeHeader(apiType);
// Set requested API version header that can be used for
// version enforcement.
httpClient.DefaultRequestHeaders.Add(HttpConstants.HttpHeaders.Version,
HttpConstants.Versions.CurrentVersion);
httpClient.DefaultRequestHeaders.Add(HttpConstants.HttpHeaders.SDKSupportedCapabilities,
Headers.SDKSUPPORTEDCAPABILITIES);
httpClient.DefaultRequestHeaders.Add(HttpConstants.HttpHeaders.Accept, RuntimeConstants.MediaTypes.Json);
return new CosmosHttpClientCore(
httpClient,
httpMessageHandler,
eventSource);
}
public override Task<HttpResponseMessage> GetAsync(
Uri uri,
INameValueCollection additionalHeaders,
ResourceType resourceType,
HttpTimeoutPolicy timeoutPolicy,
IClientSideRequestStatistics clientSideRequestStatistics,
CancellationToken cancellationToken)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
// GetAsync doesn't let clients to pass in additional headers. So, we are
// internally using SendAsync and add the additional headers to requestMessage.
ValueTask<HttpRequestMessage> CreateRequestMessage()
{
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
if (additionalHeaders != null)
{
foreach (string header in additionalHeaders)
{
if (GatewayStoreClient.IsAllowedRequestHeader(header))
{
requestMessage.Headers.TryAddWithoutValidation(header, additionalHeaders[header]);
}
}
}
return new ValueTask<HttpRequestMessage>(requestMessage);
}
return this.SendHttpAsync(
CreateRequestMessage,
resourceType,
timeoutPolicy,
clientSideRequestStatistics,
cancellationToken);
}
public override Task<HttpResponseMessage> SendHttpAsync(
Func<ValueTask<HttpRequestMessage>> createRequestMessageAsync,
ResourceType resourceType,
HttpTimeoutPolicy timeoutPolicy,
IClientSideRequestStatistics clientSideRequestStatistics,
CancellationToken cancellationToken)
{
if (createRequestMessageAsync == null)
{
throw new ArgumentNullException(nameof(createRequestMessageAsync));
}
return this.SendHttpHelperAsync(
createRequestMessageAsync,
resourceType,
timeoutPolicy,
clientSideRequestStatistics,
cancellationToken);
}
private async Task<HttpResponseMessage> SendHttpHelperAsync(
Func<ValueTask<HttpRequestMessage>> createRequestMessageAsync,
ResourceType resourceType,
HttpTimeoutPolicy timeoutPolicy,
IClientSideRequestStatistics clientSideRequestStatistics,
CancellationToken cancellationToken)
{
DateTime startDateTimeUtc = DateTime.UtcNow;
IEnumerator<(TimeSpan requestTimeout, TimeSpan delayForNextRequest)> timeoutEnumerator = timeoutPolicy.GetTimeoutEnumerator();
timeoutEnumerator.MoveNext();
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
(TimeSpan requestTimeout, TimeSpan delayForNextRequest) = timeoutEnumerator.Current;
using (HttpRequestMessage requestMessage = await createRequestMessageAsync())
{
// If the default cancellation token is passed then use the timeout policy
using CancellationTokenSource cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cancellationTokenSource.CancelAfter(requestTimeout);
DateTime requestStartTime = DateTime.UtcNow;
try
{
HttpResponseMessage responseMessage = await this.ExecuteHttpHelperAsync(
requestMessage,
resourceType,
cancellationTokenSource.Token);
if (clientSideRequestStatistics is ClientSideRequestStatisticsTraceDatum datum)
{
datum.RecordHttpResponse(requestMessage, responseMessage, resourceType, requestStartTime);
}
if (!timeoutPolicy.ShouldRetryBasedOnResponse(requestMessage.Method, responseMessage))
{
return responseMessage;
}
bool isOutOfRetries = CosmosHttpClientCore.IsOutOfRetries(timeoutPolicy, startDateTimeUtc, timeoutEnumerator);
if (isOutOfRetries)
{
return responseMessage;
}
}
catch (Exception e)
{
if (clientSideRequestStatistics is ClientSideRequestStatisticsTraceDatum datum)
{
datum.RecordHttpException(requestMessage, e, resourceType, requestStartTime);
}
bool isOutOfRetries = CosmosHttpClientCore.IsOutOfRetries(timeoutPolicy, startDateTimeUtc, timeoutEnumerator);
switch (e)
{
case OperationCanceledException operationCanceledException:
// Throw if the user passed in cancellation was requested
if (cancellationToken.IsCancellationRequested)
{
throw;
}
// Convert OperationCanceledException to 408 when the HTTP client throws it. This makes it clear that the
// the request timed out and was not user canceled operation.
if (isOutOfRetries || !timeoutPolicy.IsSafeToRetry(requestMessage.Method))
{
// throw current exception (caught in transport handler)
string message =
$"GatewayStoreClient Request Timeout. Start Time UTC:{startDateTimeUtc}; Total Duration:{(DateTime.UtcNow - startDateTimeUtc).TotalMilliseconds} Ms; Request Timeout {requestTimeout.TotalMilliseconds} Ms; Http Client Timeout:{this.httpClient.Timeout.TotalMilliseconds} Ms; Activity id: {System.Diagnostics.Trace.CorrelationManager.ActivityId};";
e.Data.Add("Message", message);
if (timeoutPolicy.ShouldThrow503OnTimeout)
{
throw CosmosExceptionFactory.CreateServiceUnavailableException(
message: message,
headers: new Headers()
{
ActivityId = System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString(),
SubStatusCode = SubStatusCodes.TransportGenerated503
},
innerException: e);
}
throw;
}
break;
case WebException webException:
if (isOutOfRetries || (!timeoutPolicy.IsSafeToRetry(requestMessage.Method) && !WebExceptionUtility.IsWebExceptionRetriable(webException)))
{
throw;
}
break;
case HttpRequestException httpRequestException:
if (isOutOfRetries || !timeoutPolicy.IsSafeToRetry(requestMessage.Method))
{
throw;
}
break;
default:
throw;
}
}
}
if (delayForNextRequest != TimeSpan.Zero)
{
await Task.Delay(delayForNextRequest);
}
}
}
private static bool IsOutOfRetries(
HttpTimeoutPolicy timeoutPolicy,
DateTime startDateTimeUtc,
IEnumerator<(TimeSpan requestTimeout, TimeSpan delayForNextRequest)> timeoutEnumerator)
{
return (DateTime.UtcNow - startDateTimeUtc) > timeoutPolicy.MaximumRetryTimeLimit || // Maximum of time for all retries
!timeoutEnumerator.MoveNext(); // No more retries are configured
}
private async Task<HttpResponseMessage> ExecuteHttpHelperAsync(
HttpRequestMessage requestMessage,
ResourceType resourceType,
CancellationToken cancellationToken)
{
DateTime sendTimeUtc = DateTime.UtcNow;
Guid localGuid = Guid.NewGuid(); // For correlating HttpRequest and HttpResponse Traces
Guid requestedActivityId = System.Diagnostics.Trace.CorrelationManager.ActivityId;
this.eventSource.Request(
requestedActivityId,
localGuid,
requestMessage.RequestUri.ToString(),
resourceType.ToResourceTypeString(),
requestMessage.Headers);
// Only read the header initially. The content gets copied into a memory stream later
// if we read the content HTTP client will buffer the message and then it will get buffered
// again when it is copied to the memory stream.
HttpResponseMessage responseMessage = await this.httpClient.SendAsync(
requestMessage,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken);
// WebAssembly HttpClient does not set the RequestMessage property on SendAsync
if (responseMessage.RequestMessage == null)
{
responseMessage.RequestMessage = requestMessage;
}
DateTime receivedTimeUtc = DateTime.UtcNow;
TimeSpan durationTimeSpan = receivedTimeUtc - sendTimeUtc;
Guid activityId = Guid.Empty;
if (responseMessage.Headers.TryGetValues(
HttpConstants.HttpHeaders.ActivityId,
out IEnumerable<string> headerValues) && headerValues.Any())
{
activityId = new Guid(headerValues.First());
}
this.eventSource.Response(
activityId,
localGuid,
(short)responseMessage.StatusCode,
durationTimeSpan.TotalMilliseconds,
responseMessage.Headers);
return responseMessage;
}
protected override void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
this.httpClient.Dispose();
}
this.disposedValue = true;
}
}
public override void Dispose()
{
this.Dispose(true);
}
private class HttpRequestMessageHandler : DelegatingHandler
{
private readonly EventHandler<SendingRequestEventArgs> sendingRequest;
private readonly EventHandler<ReceivedResponseEventArgs> receivedResponse;
public HttpRequestMessageHandler(
EventHandler<SendingRequestEventArgs> sendingRequest,
EventHandler<ReceivedResponseEventArgs> receivedResponse,
HttpMessageHandler innerHandler)
{
this.sendingRequest = sendingRequest;
this.receivedResponse = receivedResponse;
this.InnerHandler = innerHandler ?? throw new ArgumentNullException(
$"innerHandler is null. This required for .NET core to limit the http connection. See {nameof(CreateHttpClientHandler)} ");
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
this.sendingRequest?.Invoke(this, new SendingRequestEventArgs(request));
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
this.receivedResponse?.Invoke(this, new ReceivedResponseEventArgs(request, response));
return response;
}
}
}
}