-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCosmosClientProvider.cs
More file actions
101 lines (85 loc) · 4.24 KB
/
Copy pathCosmosClientProvider.cs
File metadata and controls
101 lines (85 loc) · 4.24 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
99
100
101
using System;
using System.Collections.Concurrent;
using System.Linq;
using Atc.Cosmos.Serialization;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Options;
namespace Atc.Cosmos.Internal
{
public sealed class CosmosClientProvider(
IOptions<CosmosClientOptions> cosmosClientOptions,
IJsonCosmosSerializer serializer)
: IDisposable, ICosmosClientProvider
{
private readonly ConcurrentDictionary<CosmosOptions, CosmosClient> cosmosClientCache = new ();
private readonly ConcurrentDictionary<CosmosOptions, CosmosClient> cosmosBulkClientCache = new ();
public CosmosClient GetClient(CosmosOptions options)
=> cosmosClientCache.AddOrUpdate(options, CreateClient, (_, c) => c);
public CosmosClient GetBulkClient(CosmosOptions options)
=> cosmosBulkClientCache.AddOrUpdate(options, CreateBulkClient, (_, c) => c);
public void Dispose()
{
foreach (var client in cosmosClientCache.ToList())
{
client.Value.Dispose();
}
foreach (var client in cosmosBulkClientCache.ToList())
{
client.Value.Dispose();
}
}
private CosmosClient CreateBulkClient(CosmosOptions cosmosOptions) => CreateClient(cosmosOptions, true);
private CosmosClient CreateClient(CosmosOptions cosmosOptions) => CreateClient(cosmosOptions, false);
private CosmosClient CreateClient(CosmosOptions cosmosOptions, bool allowBulk)
{
var connectionString =
$"AccountEndpoint={cosmosOptions.AccountEndpoint};" +
$"AccountKey={cosmosOptions.AccountKey}";
var options = CreateCosmosClientOptions();
options.AllowBulkExecution = allowBulk;
options.Serializer = cosmosClientOptions.Value.Serializer ?? new CosmosSerializerAdapter(serializer);
options.CosmosClientTelemetryOptions = cosmosClientOptions.Value.CosmosClientTelemetryOptions;
return cosmosOptions.Credential is not null
? new CosmosClient(
cosmosOptions.AccountEndpoint,
cosmosOptions.Credential,
options)
: new CosmosClient(connectionString, options);
}
private CosmosClientOptions CreateCosmosClientOptions()
{
var result = new CosmosClientOptions();
if (cosmosClientOptions is { Value: { } o })
{
if (!string.IsNullOrEmpty(o.ApplicationName))
{
result.ApplicationName = o.ApplicationName;
}
result.ApplicationPreferredRegions = o.ApplicationPreferredRegions;
result.ApplicationRegion = o.ApplicationRegion;
result.ConnectionMode = o.ConnectionMode;
result.ConsistencyLevel = o.ConsistencyLevel;
foreach (var handler in o.CustomHandlers)
{
result.CustomHandlers.Add(handler);
}
result.HttpClientFactory = o.HttpClientFactory;
result.IdleTcpConnectionTimeout = o.IdleTcpConnectionTimeout;
result.LimitToEndpoint = o.LimitToEndpoint;
result.MaxRequestsPerTcpConnection = o.MaxRequestsPerTcpConnection;
result.MaxRetryWaitTimeOnRateLimitedRequests = o.MaxRetryWaitTimeOnRateLimitedRequests;
result.MaxTcpConnectionsPerEndpoint = o.MaxTcpConnectionsPerEndpoint;
result.OpenTcpConnectionTimeout = o.OpenTcpConnectionTimeout;
result.PortReuseMode = o.PortReuseMode;
result.RequestTimeout = o.RequestTimeout;
result.SerializerOptions = o.SerializerOptions;
result.WebProxy = o.WebProxy;
result.EnableTcpConnectionEndpointRediscovery = o.EnableTcpConnectionEndpointRediscovery;
result.GatewayModeMaxConnectionLimit = o.GatewayModeMaxConnectionLimit;
result.MaxRetryAttemptsOnRateLimitedRequests = o.MaxRetryAttemptsOnRateLimitedRequests;
result.CosmosClientTelemetryOptions = o.CosmosClientTelemetryOptions;
}
return result;
}
}
}