-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathGatewayAccountReader.cs
More file actions
98 lines (90 loc) · 4.79 KB
/
Copy pathGatewayAccountReader.cs
File metadata and controls
98 lines (90 loc) · 4.79 KB
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Resource.CosmosExceptions;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Cosmos.Tracing.TraceData;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Collections;
internal sealed class GatewayAccountReader
{
private readonly ConnectionPolicy connectionPolicy;
private readonly AuthorizationTokenProvider cosmosAuthorization;
private readonly CosmosHttpClient httpClient;
private readonly Uri serviceEndpoint;
private readonly CancellationToken cancellationToken;
// Backlog: Auth abstractions are spilling through. 4 arguments for this CTOR are result of it.
public GatewayAccountReader(Uri serviceEndpoint,
AuthorizationTokenProvider cosmosAuthorization,
ConnectionPolicy connectionPolicy,
CosmosHttpClient httpClient,
CancellationToken cancellationToken = default)
{
this.httpClient = httpClient;
this.serviceEndpoint = serviceEndpoint;
this.cosmosAuthorization = cosmosAuthorization ?? throw new ArgumentNullException(nameof(AuthorizationTokenProvider));
this.connectionPolicy = connectionPolicy;
this.cancellationToken = cancellationToken;
}
private async Task<AccountProperties> GetDatabaseAccountAsync(Uri serviceEndpoint)
{
INameValueCollection headers = new RequestNameValueCollection();
await this.cosmosAuthorization.AddAuthorizationHeaderAsync(
headersCollection: headers,
serviceEndpoint,
HttpConstants.HttpMethods.Get,
AuthorizationTokenType.PrimaryMasterKey);
using (ITrace trace = Trace.GetRootTrace("Account Read", TraceComponent.Transport, TraceLevel.Info))
{
IClientSideRequestStatistics stats = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, trace);
try
{
using (HttpResponseMessage responseMessage = await this.httpClient.GetAsync(
uri: serviceEndpoint,
additionalHeaders: headers,
resourceType: ResourceType.DatabaseAccount,
timeoutPolicy: HttpTimeoutPolicyControlPlaneRead.Instance,
clientSideRequestStatistics: stats,
cancellationToken: default))
using (DocumentServiceResponse documentServiceResponse = await ClientExtensions.ParseResponseAsync(responseMessage))
{
return CosmosResource.FromStream<AccountProperties>(documentServiceResponse);
}
}
catch (ObjectDisposedException) when (this.cancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException($"Client is being disposed for {serviceEndpoint} at {DateTime.UtcNow}, cancelling further operations.");
}
catch (OperationCanceledException ex)
{
trace.AddDatum("Client Side Request Stats", stats);
throw CosmosExceptionFactory.CreateRequestTimeoutException(
message: ex.Data?["Message"]?.ToString() ?? ex.Message,
headers: new Headers()
{
ActivityId = System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString()
},
innerException: ex,
trace: trace);
}
}
}
public async Task<AccountProperties> InitializeReaderAsync()
{
AccountProperties databaseAccount = await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
defaultEndpoint: this.serviceEndpoint,
locations: this.connectionPolicy.PreferredLocations,
accountInitializationCustomEndpoints: this.connectionPolicy.AccountInitializationCustomEndpoints,
getDatabaseAccountFn: this.GetDatabaseAccountAsync,
cancellationToken: this.cancellationToken);
return databaseAccount;
}
}
}