-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathCosmosClientOptions.cs
1278 lines (1161 loc) · 59 KB
/
CosmosClientOptions.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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Common;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.Cosmos.FaultInjection;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json;
/// <summary>
/// Defines all the configurable options that the CosmosClient requires.
/// </summary>
/// <example>
/// An example on how to configure the serialization option to ignore null values.
/// <code language="c#">
/// <![CDATA[
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// SerializerOptions = new CosmosSerializationOptions(){
/// IgnoreNullValues = true
/// },
/// ConnectionMode = ConnectionMode.Gateway,
/// };
///
/// CosmosClient client = new CosmosClient("endpoint", "key", clientOptions);
/// ]]>
/// </code>
/// </example>
public class CosmosClientOptions
{
/// <summary>
/// Default connection mode
/// </summary>
private const ConnectionMode DefaultConnectionMode = ConnectionMode.Direct;
/// <summary>
/// Default Protocol mode
/// </summary>
private const Protocol DefaultProtocol = Protocol.Tcp;
private const string ConnectionStringAccountEndpoint = "AccountEndpoint";
private const string ConnectionStringAccountKey = "AccountKey";
private const string ConnectionStringDisableServerCertificateValidation = "DisableServerCertificateValidation";
private const ApiType DefaultApiType = ApiType.None;
/// <summary>
/// Default request timeout
/// </summary>
private int gatewayModeMaxConnectionLimit;
private CosmosSerializationOptions serializerOptions;
private CosmosSerializer serializerInternal;
private System.Text.Json.JsonSerializerOptions stjSerializerOptions;
private ConnectionMode connectionMode;
private Protocol connectionProtocol;
private TimeSpan? idleTcpConnectionTimeout;
private TimeSpan? openTcpConnectionTimeout;
private int? maxRequestsPerTcpConnection;
private int? maxTcpConnectionsPerEndpoint;
private PortReuseMode? portReuseMode;
private IWebProxy webProxy;
private Func<HttpClient> httpClientFactory;
private string applicationName;
private IFaultInjector faultInjector;
/// <summary>
/// Creates a new CosmosClientOptions
/// </summary>
public CosmosClientOptions()
{
this.GatewayModeMaxConnectionLimit = ConnectionPolicy.Default.MaxConnectionLimit;
this.RequestTimeout = ConnectionPolicy.Default.RequestTimeout;
this.TokenCredentialBackgroundRefreshInterval = null;
this.ConnectionMode = CosmosClientOptions.DefaultConnectionMode;
this.ConnectionProtocol = CosmosClientOptions.DefaultProtocol;
this.ApiType = CosmosClientOptions.DefaultApiType;
this.CustomHandlers = new Collection<RequestHandler>();
this.CosmosClientTelemetryOptions = new CosmosClientTelemetryOptions();
}
/// <summary>
/// Get or set user-agent suffix to include with every Azure Cosmos DB service interaction.
/// </summary>
/// <remarks>
/// Setting this property after sending any request won't have any effect.
/// </remarks>
public string ApplicationName
{
get => this.applicationName;
set
{
try
{
HttpRequestMessage dummyMessage = new HttpRequestMessage();
dummyMessage.Headers.Add(HttpConstants.HttpHeaders.UserAgent, value);
}
catch (FormatException fme)
{
throw new ArgumentException($"Application name '{value}' is invalid.", fme);
}
this.applicationName = value;
}
}
/// <summary>
/// Get or set session container for the client
/// </summary>
internal ISessionContainer SessionContainer { get; set; }
/// <summary>
/// Gets or sets the location where the application is running. This will influence the SDK's choice for the Azure Cosmos DB service interaction.
/// </summary>
/// <remarks>
/// <para>
/// During the CosmosClient initialization the account information, including the available regions, is obtained from the <see cref="CosmosClient.Endpoint"/>.
/// The CosmosClient will use the value of <see cref="ApplicationRegion"/> to populate the preferred list with the account available regions ordered by geographical proximity to the indicated region.
/// If the value of <see cref="ApplicationRegion"/> is not an available region in the account, the preferred list is still populated following the same mechanism but would not include the indicated region.
/// </para>
/// <para>
/// If during CosmosClient initialization, the <see cref="CosmosClient.Endpoint"/> is not reachable, the CosmosClient will attempt to recover and obtain the account information issuing requests to all <see cref="Regions"/> ordered by proximity to the <see cref="ApplicationRegion"/>.
/// For more granular control over the selected regions or to define a list based on a custom criteria, use <see cref="ApplicationPreferredRegions"/> instead of <see cref="ApplicationRegion"/>.
/// </para>
/// <para>
/// See also <seealso href="https://docs.microsoft.com/azure/cosmos-db/sql/troubleshoot-sdk-availability">Diagnose
/// and troubleshoot the availability of Cosmos SDKs</seealso> for more details.
/// </para>
/// <para>
/// This configuration is an alternative to <see cref="ApplicationPreferredRegions"/>, either one can be set but not both.
/// </para>
/// </remarks>
/// <example>
/// If an account is configured with multiple regions including West US, East US, and West Europe, configuring a client like the below example would result in the CosmosClient generating a sorted preferred regions based on proximity to East US.
/// The CosmosClient will send requests to East US, if that region becomes unavailable, it will fallback to West US (second in proximity), and finally to West Europe if West US becomes unavailable.
/// <code language="c#">
/// <![CDATA[
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// ApplicationRegion = Regions.EastUS
/// };
///
/// CosmosClient client = new CosmosClient("endpoint", "key", clientOptions);
/// ]]>
/// </code>
/// </example>
/// <seealso cref="CosmosClientBuilder.WithApplicationRegion(string)"/>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/high-availability#high-availability-with-cosmos-db-in-the-event-of-regional-outages">High availability on regional outages</seealso>
public string ApplicationRegion { get; set; }
/// <summary>
/// Gets and sets the preferred regions for geo-replicated database accounts in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// <para>
/// During the CosmosClient initialization the account information, including the available regions, is obtained from the <see cref="CosmosClient.Endpoint"/>.
/// The CosmosClient will use the value of <see cref="ApplicationPreferredRegions"/> to populate the preferred list with the account available regions that intersect with its value.
/// If the value of <see cref="ApplicationPreferredRegions"/> contains regions that are not an available region in the account, the values will be ignored. If the these invalid regions are added later to the account, the CosmosClient will use them if they are higher in the preference order.
/// </para>
/// <para>
/// If during CosmosClient initialization, the <see cref="CosmosClient.Endpoint"/> is not reachable, the CosmosClient will attempt to recover and obtain the account information issuing requests to the regions in <see cref="ApplicationPreferredRegions"/> in the order that they are listed.
/// </para>
/// <para>
/// See also <seealso href="https://docs.microsoft.com/azure/cosmos-db/sql/troubleshoot-sdk-availability">Diagnose
/// and troubleshoot the availability of Cosmos SDKs</seealso> for more details.
/// </para>
/// <para>
/// This configuration is an alternative to <see cref="ApplicationRegion"/>, either one can be set but not both.
/// </para>
/// </remarks>
/// <example>
/// <code language="c#">
/// <![CDATA[
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// ApplicationPreferredRegions = new List<string>(){ Regions.EastUS, Regions.WestUS }
/// };
///
/// CosmosClient client = new CosmosClient("endpoint", "key", clientOptions);
/// ]]>
/// </code>
/// </example>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/high-availability#high-availability-with-cosmos-db-in-the-event-of-regional-outages">High availability on regional outages</seealso>
public IReadOnlyList<string> ApplicationPreferredRegions { get; set; }
/// <summary>
/// Gets and sets the custom endpoints to use for account initialization for geo-replicated database accounts in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// <para>
/// During the CosmosClient initialization the account information, including the available regions, is obtained from the <see cref="CosmosClient.Endpoint"/>.
/// Should the global endpoint become inaccessible, the CosmosClient will attempt to obtain the account information issuing requests to the custom endpoints provided in <see cref="AccountInitializationCustomEndpoints"/>.
/// </para>
/// <para>
/// Nevertheless, this parameter remains optional and is recommended for implementation when a customer has configured an endpoint with a custom DNS hostname
/// (instead of accountname-region.documents.azure.com) etc. for their Cosmos DB account.
/// </para>
/// <para>
/// See also <seealso href="https://docs.microsoft.com/azure/cosmos-db/sql/troubleshoot-sdk-availability">Diagnose
/// and troubleshoot the availability of Cosmos SDKs</seealso> for more details.
/// </para>
/// </remarks>
/// <example>
/// <code language="c#">
/// <![CDATA[
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// AccountInitializationCustomEndpoints = new HashSet<Uri>()
/// {
/// new Uri("custom.p-1.documents.azure.com"),
/// new Uri("custom.p-2.documents.azure.com")
/// }
/// };
///
/// CosmosClient client = new CosmosClient("endpoint", "key", clientOptions);
/// ]]>
/// </code>
/// </example>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/high-availability#high-availability-with-cosmos-db-in-the-event-of-regional-outages">High availability on regional outages</seealso>
public IEnumerable<Uri> AccountInitializationCustomEndpoints { get; set; }
/// <summary>
/// Get or set the maximum number of concurrent connections allowed for the target
/// service endpoint in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// This setting is only applicable in Gateway mode.
/// </remarks>
/// <value>Default value is 50.</value>
/// <seealso cref="CosmosClientBuilder.WithConnectionModeGateway(int?, IWebProxy)"/>
public int GatewayModeMaxConnectionLimit
{
get => this.gatewayModeMaxConnectionLimit;
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
if (this.HttpClientFactory != null && value != ConnectionPolicy.Default.MaxConnectionLimit)
{
throw new ArgumentException($"{nameof(this.httpClientFactory)} can not be set along with {nameof(this.GatewayModeMaxConnectionLimit)}. This must be set on the HttpClientHandler.MaxConnectionsPerServer property.");
}
this.gatewayModeMaxConnectionLimit = value;
}
}
/// <summary>
/// Gets the request timeout in seconds when connecting to the Azure Cosmos DB service.
/// The number specifies the time to wait for response to come back from network peer.
/// </summary>
/// <value>Default value is 6 seconds.</value>
/// <seealso cref="CosmosClientBuilder.WithRequestTimeout(TimeSpan)"/>
public TimeSpan RequestTimeout { get; set; }
/// <summary>
/// The SDK does a background refresh based on the time interval set to refresh the token credentials.
/// This avoids latency issues because the old token is used until the new token is retrieved.
/// </summary>
/// <remarks>
/// The recommended minimum value is 5 minutes. The default value is 50% of the token expire time.
/// </remarks>
public TimeSpan? TokenCredentialBackgroundRefreshInterval { get; set; }
/// <summary>
/// Gets the handlers run before the process
/// </summary>
/// <seealso cref="CosmosClientBuilder.AddCustomHandlers(RequestHandler[])"/>
[JsonConverter(typeof(ClientOptionJsonConverter))]
public Collection<RequestHandler> CustomHandlers { get; }
/// <summary>
/// Get or set the connection mode used by the client when connecting to the Azure Cosmos DB service.
/// </summary>
/// <value>
/// Default value is <see cref="Cosmos.ConnectionMode.Direct"/>
/// </value>
/// <remarks>
/// For more information, see <see href="https://learn.microsoft.com/azure/cosmos-db/nosql/performance-tips-dotnet-sdk-v3#direct-connection">Connection policy: Use direct connection mode</see>.
/// </remarks>
/// <seealso cref="CosmosClientBuilder.WithConnectionModeDirect()"/>
/// <seealso cref="CosmosClientBuilder.WithConnectionModeGateway(int?, IWebProxy)"/>
public ConnectionMode ConnectionMode
{
get => this.connectionMode;
set
{
if (value == ConnectionMode.Gateway)
{
this.ConnectionProtocol = Protocol.Https;
}
else if (value == ConnectionMode.Direct)
{
this.connectionProtocol = Protocol.Tcp;
}
this.ValidateDirectTCPSettings();
this.connectionMode = value;
}
}
/// <summary>
/// This can be used to weaken the database account consistency level for read operations.
/// If this is not set the database account consistency level will be used for all requests.
/// </summary>
public ConsistencyLevel? ConsistencyLevel { get; set; }
/// <summary>
/// Sets the priority level for requests created using cosmos client.
/// </summary>
/// <remarks>
/// If priority level is also set at request level in <see cref="RequestOptions.PriorityLevel"/>, that priority is used.
/// If <see cref="AllowBulkExecution"/> is set to true in CosmosClientOptions, priority level set on the CosmosClient is used.
/// </remarks>
/// <seealso href="https://aka.ms/CosmosDB/PriorityBasedExecution"/>
public PriorityLevel? PriorityLevel { get; set; }
/// <summary>
/// Gets or sets the maximum number of retries in the case where the request fails
/// because the Azure Cosmos DB service has applied rate limiting on the client.
/// </summary>
/// <value>
/// The default value is 9. This means in the case where the request is rate limited,
/// the same request will be issued for a maximum of 10 times to the server before
/// an error is returned to the application.
///
/// If the value of this property is set to 0, there will be no automatic retry on rate
/// limiting requests from the client and the exception needs to be handled at the
/// application level.
/// </value>
/// <remarks>
/// <para>
/// When a client is sending requests faster than the allowed rate,
/// the service will return HttpStatusCode 429 (Too Many Requests) to rate limit the client. The current
/// implementation in the SDK will then wait for the amount of time the service tells it to wait and
/// retry after the time has elapsed.
/// </para>
/// <para>
/// For more information, see <see href="https://docs.microsoft.com/azure/cosmos-db/performance-tips#throughput">Handle rate limiting/request rate too large</see>.
/// </para>
/// </remarks>
/// <seealso cref="CosmosClientBuilder.WithThrottlingRetryOptions(TimeSpan, int)"/>
public int? MaxRetryAttemptsOnRateLimitedRequests { get; set; }
/// <summary>
/// Gets or sets the maximum retry time in seconds for the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The default value is 30 seconds.
/// </value>
/// <remarks>
/// <para>
/// The minimum interval is seconds. Any interval that is smaller will be ignored.
/// </para>
/// <para>
/// When a request fails due to a rate limiting error, the service sends back a response that
/// contains a value indicating the client should not retry before the <see cref="Microsoft.Azure.Cosmos.CosmosException.RetryAfter"/> time period has
/// elapsed.
///
/// This property allows the application to set a maximum wait time for all retry attempts.
/// If the cumulative wait time exceeds the this value, the client will stop retrying and return the error to the application.
/// </para>
/// <para>
/// For more information, see <see href="https://docs.microsoft.com/azure/cosmos-db/performance-tips#throughput">Handle rate limiting/request rate too large</see>.
/// </para>
/// </remarks>
/// <seealso cref="CosmosClientBuilder.WithThrottlingRetryOptions(TimeSpan, int)"/>
public TimeSpan? MaxRetryWaitTimeOnRateLimitedRequests { get; set; }
/// <summary>
/// Gets or sets the boolean to only return the headers and status code in
/// the Cosmos DB response for write item operation like Create, Upsert, Patch and Replace.
/// Setting the option to false will cause the response to have a null resource. This reduces networking and CPU load by not sending
/// the resource back over the network and serializing it on the client.
/// </summary>
/// <remarks>
/// <para>This is optimal for workloads where the returned resource is not used.</para>
/// <para>This option can be overriden by similar property in ItemRequestOptions and TransactionalBatchItemRequestOptions</para>
/// </remarks>
/// <seealso cref="CosmosClientBuilder.WithContentResponseOnWrite(bool)"/>
/// <seealso cref="ItemRequestOptions.EnableContentResponseOnWrite"/>
/// <seealso cref="TransactionalBatchItemRequestOptions.EnableContentResponseOnWrite"/>
public bool? EnableContentResponseOnWrite { get; set; }
/// <summary>
/// Sets the <see cref="System.Text.Json.JsonSerializerOptions"/> for the System.Text.Json serializer.
/// Note that if this option is provided, then the SDK will use the System.Text.Json as the default serializer and set
/// the serializer options as the constructor args.
/// </summary>
/// <example>
/// An example on how to configure the System.Text.Json serializer options to ignore null values
/// <code language="c#">
/// <![CDATA[
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// UseSystemTextJsonSerializerWithOptions = new System.Text.Json.JsonSerializerOptions()
/// {
/// DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
/// }
/// };
///
/// CosmosClient client = new CosmosClient("endpoint", "key", clientOptions);
/// ]]>
/// </code>
/// </example>
public System.Text.Json.JsonSerializerOptions UseSystemTextJsonSerializerWithOptions
{
get => this.stjSerializerOptions;
set
{
if (this.Serializer != null || this.SerializerOptions != null)
{
throw new ArgumentException(
$"{nameof(this.UseSystemTextJsonSerializerWithOptions)} is not compatible with {nameof(this.Serializer)} or {nameof(this.SerializerOptions)}. Only one can be set. ");
}
this.stjSerializerOptions = value;
this.serializerInternal = new CosmosSystemTextJsonSerializer(
this.stjSerializerOptions);
}
}
/// <summary>
/// Gets or sets the advanced replica selection flag. The advanced replica selection logic keeps track of the replica connection
/// status, and based on status, it prioritizes the replicas which show healthy stable connections, so that the requests can be sent
/// confidently to the particular replica. This helps the cosmos client to become more resilient and effective to any connectivity issues.
/// The default value for this parameter is 'false'.
/// </summary>
/// <remarks>
/// <para>This is optimal for latency-sensitive workloads. Does not apply if <see cref="ConnectionMode.Gateway"/> is used.</para>
/// </remarks>
internal bool? EnableAdvancedReplicaSelectionForTcp { get; set; }
/// <summary>
/// (Direct/TCP) Controls the amount of idle time after which unused connections are closed.
/// </summary>
/// <value>
/// By default, idle connections are kept open indefinitely. Value must be greater than or equal to 10 minutes. Recommended values are between 20 minutes and 24 hours.
/// </value>
/// <remarks>
/// Mainly useful for sparse infrequent access to a large database account.
/// </remarks>
public TimeSpan? IdleTcpConnectionTimeout
{
get => this.idleTcpConnectionTimeout;
set
{
this.idleTcpConnectionTimeout = value;
this.ValidateDirectTCPSettings();
}
}
/// <summary>
/// (Direct/TCP) Controls the amount of time allowed for trying to establish a connection.
/// </summary>
/// <value>
/// The default timeout is 5 seconds. For latency sensitive applications that prefer to retry faster, a recommended value of 1 second can be used.
/// </value>
/// <remarks>
/// When the time elapses, the attempt is cancelled and an error is returned. Longer timeouts will delay retries and failures.
/// </remarks>
public TimeSpan? OpenTcpConnectionTimeout
{
get => this.openTcpConnectionTimeout;
set
{
this.openTcpConnectionTimeout = value;
this.ValidateDirectTCPSettings();
}
}
/// <summary>
/// (Direct/TCP) Controls the number of requests allowed simultaneously over a single TCP connection. When more requests are in flight simultaneously, the direct/TCP client will open additional connections.
/// </summary>
/// <value>
/// The default settings allow 30 simultaneous requests per connection.
/// Do not set this value lower than 4 requests per connection or higher than 50-100 requests per connection.
/// The former can lead to a large number of connections to be created.
/// The latter can lead to head of line blocking, high latency and timeouts.
/// </value>
/// <remarks>
/// Applications with a very high degree of parallelism per connection, with large requests or responses, or with very tight latency requirements might get better performance with 8-16 requests per connection.
/// </remarks>
public int? MaxRequestsPerTcpConnection
{
get => this.maxRequestsPerTcpConnection;
set
{
this.maxRequestsPerTcpConnection = value;
this.ValidateDirectTCPSettings();
}
}
/// <summary>
/// (Direct/TCP) Controls the maximum number of TCP connections that may be opened to each Cosmos DB back-end.
/// Together with MaxRequestsPerTcpConnection, this setting limits the number of requests that are simultaneously sent to a single Cosmos DB back-end(MaxRequestsPerTcpConnection x MaxTcpConnectionPerEndpoint).
/// </summary>
/// <value>
/// The default value is 65,535. Value must be greater than or equal to 16.
/// </value>
public int? MaxTcpConnectionsPerEndpoint
{
get => this.maxTcpConnectionsPerEndpoint;
set
{
this.maxTcpConnectionsPerEndpoint = value;
this.ValidateDirectTCPSettings();
}
}
/// <summary>
/// (Direct/TCP) Controls the client port reuse policy used by the transport stack.
/// </summary>
/// <value>
/// The default value is PortReuseMode.ReuseUnicastPort.
/// </value>
/// <remarks>
/// ReuseUnicastPort and PrivatePortPool are not mutually exclusive.
/// When PrivatePortPool is enabled, the client first tries to reuse a port it already has.
/// It falls back to allocating a new port if the initial attempts failed. If this fails, too, the client then falls back to ReuseUnicastPort.
/// </remarks>
public PortReuseMode? PortReuseMode
{
get => this.portReuseMode;
set
{
this.portReuseMode = value;
this.ValidateDirectTCPSettings();
}
}
/// <summary>
/// (Gateway/Https) Get or set the proxy information used for web requests.
/// </summary>
[JsonIgnore]
public IWebProxy WebProxy
{
get => this.webProxy;
set
{
if (value != null && this.HttpClientFactory != null)
{
throw new ArgumentException($"{nameof(this.WebProxy)} cannot be set along {nameof(this.HttpClientFactory)}");
}
this.webProxy = value;
}
}
/// <summary>
/// Get to set optional serializer options.
/// </summary>
/// <example>
/// An example on how to configure the serialization option to ignore null values
/// <code language="c#">
/// <![CDATA[
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// SerializerOptions = new CosmosSerializationOptions(){
/// IgnoreNullValues = true
/// }
/// };
///
/// CosmosClient client = new CosmosClient("endpoint", "key", clientOptions);
/// ]]>
/// </code>
/// </example>
public CosmosSerializationOptions SerializerOptions
{
get => this.serializerOptions;
set
{
if (this.Serializer != null || this.UseSystemTextJsonSerializerWithOptions != null)
{
throw new ArgumentException(
$"{nameof(this.SerializerOptions)} is not compatible with {nameof(this.Serializer)} or {nameof(this.UseSystemTextJsonSerializerWithOptions)}. Only one can be set. ");
}
this.serializerOptions = value;
}
}
/// <summary>
/// Get to set an optional JSON serializer. The client will use it to serialize or de-serialize user's cosmos request/responses.
/// SDK owned types such as DatabaseProperties and ContainerProperties will always use the SDK default serializer.
/// </summary>
/// <example>
/// An example on how to set a custom serializer. For basic serializer options look at CosmosSerializationOptions
/// <code language="c#">
/// <![CDATA[
/// CosmosSerializer ignoreNullSerializer = new MyCustomIgnoreNullSerializer();
///
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// Serializer = ignoreNullSerializer
/// };
///
/// CosmosClient client = new CosmosClient("endpoint", "key", clientOptions);
/// ]]>
/// </code>
/// </example>
[JsonConverter(typeof(ClientOptionJsonConverter))]
public CosmosSerializer Serializer
{
get => this.serializerInternal;
set
{
if (this.SerializerOptions != null || this.UseSystemTextJsonSerializerWithOptions != null)
{
throw new ArgumentException(
$"{nameof(this.Serializer)} is not compatible with {nameof(this.SerializerOptions)} or {nameof(this.UseSystemTextJsonSerializerWithOptions)}. Only one can be set. ");
}
this.serializerInternal = value;
}
}
/// <summary>
/// Limits the operations to the provided endpoint on the CosmosClient.
/// </summary>
/// <value>
/// Default value is false.
/// </value>
/// <remarks>
/// When the value of this property is false, the SDK will automatically discover write and read regions, and use them when the configured application region is not available.
/// When set to true, availability is limited to the endpoint specified on the CosmosClient constructor.
/// Defining the <see cref="ApplicationRegion"/> or <see cref="ApplicationPreferredRegions"/> is not allowed when setting the value to true.
/// </remarks>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/high-availability">High availability</seealso>
public bool LimitToEndpoint { get; set; } = false;
/// <summary>
/// Allows optimistic batching of requests to service. Setting this option might impact the latency of the operations. Hence this option is recommended for non-latency sensitive scenarios only.
/// <remarks>
/// The use of Resource Tokens scoped to a Partition Key as an authentication mechanism when Bulk is enabled is not recommended as it reduces the potential throughput benefit
/// </remarks>
/// </summary>
public bool AllowBulkExecution { get; set; }
/// <summary>
/// Gets or sets the flag to enable address cache refresh on TCP connection reset notification.
/// </summary>
/// <remarks>
/// Does not apply if <see cref="ConnectionMode.Gateway"/> is used.
/// </remarks>
/// <value>
/// The default value is true
/// </value>
public bool EnableTcpConnectionEndpointRediscovery { get; set; } = true;
/// <summary>
/// Gets or sets a delegate to use to obtain an HttpClient instance to be used for HTTPS communication.
/// </summary>
/// <remarks>
/// <para>
/// HTTPS communication is used when <see cref="ConnectionMode"/> is set to <see cref="ConnectionMode.Gateway"/> for all operations and when <see cref="ConnectionMode"/> is <see cref="ConnectionMode.Direct"/> (default) for metadata operations.
/// </para>
/// <para>
/// Useful in scenarios where the application is using a pool of HttpClient instances to be shared, like ASP.NET Core applications with IHttpClientFactory or Blazor WebAssembly applications.
/// </para>
/// <para>
/// For .NET core applications the default GatewayConnectionLimit will be ignored. It must be set on the HttpClientHandler.MaxConnectionsPerServer to limit the number of connections
/// </para>
/// </remarks>
[JsonIgnore]
public Func<HttpClient> HttpClientFactory
{
get => this.httpClientFactory;
set
{
if (value != null && this.WebProxy != null)
{
throw new ArgumentException($"{nameof(this.HttpClientFactory)} cannot be set along {nameof(this.WebProxy)}");
}
if (this.GatewayModeMaxConnectionLimit != ConnectionPolicy.Default.MaxConnectionLimit)
{
throw new ArgumentException($"{nameof(this.httpClientFactory)} can not be set along with {nameof(this.GatewayModeMaxConnectionLimit)}. This must be set on the HttpClientHandler.MaxConnectionsPerServer property.");
}
this.httpClientFactory = value;
}
}
/// <summary>
/// Availability Strategy to be used for periods of high latency
/// </summary>
/// /// <example>
/// An example on how to set an availability strategy custom serializer.
/// <code language="c#">
/// <![CDATA[
/// CosmosClient client = new CosmosClientBuilder("connection string")
/// .WithApplicationPreferredRegions(
/// new List<string> { "East US", "Central US", "West US" } )
/// .WithAvailabilityStrategy(
/// AvailabilityStrategy.CrossRegionHedgingStrategy(
/// threshold: TimeSpan.FromMilliseconds(500),
/// thresholdStep: TimeSpan.FromMilliseconds(100)
/// ))
/// .Build();
/// ]]>
/// </code>
/// </example>
/// <remarks>
/// The availability strategy in the example is a Cross Region Hedging Strategy.
/// These strategies take two values, a threshold and a threshold step.When a request that is sent
/// out takes longer than the threshold time, the SDK will hedge to the second region in the application preferred regions list.
/// If a response from either the primary request or the first hedged request is not received
/// after the threshold step time, the SDK will hedge to the third region and so on.
/// </remarks>
public AvailabilityStrategy AvailabilityStrategy { get; set; }
/// <summary>
/// Enable partition key level failover
/// </summary>
internal bool EnablePartitionLevelFailover { get; set; } = ConfigurationManager.IsPartitionLevelFailoverEnabled(defaultValue: false);
/// <summary>
/// Quorum Read allowed with eventual consistency account or consistent prefix account.
/// </summary>
internal bool EnableUpgradeConsistencyToLocalQuorum { get; set; } = false;
/// <summary>
/// Gets or sets the connection protocol when connecting to the Azure Cosmos service.
/// </summary>
/// <value>
/// Default value is <see cref="Protocol.Tcp"/>.
/// </value>
/// <remarks>
/// This setting is not used when <see cref="ConnectionMode"/> is set to <see cref="Cosmos.ConnectionMode.Gateway"/>.
/// Gateway mode only supports HTTPS.
/// For more information, see <see href="https://learn.microsoft.com/azure/cosmos-db/nosql/performance-tips-dotnet-sdk-v3#networking">Connection policy: Use the HTTPS protocol</see>.
/// </remarks>
internal Protocol ConnectionProtocol
{
get => this.connectionProtocol;
set
{
this.ValidateDirectTCPSettings();
this.connectionProtocol = value;
}
}
/// <summary>
/// The event handler to be invoked before the request is sent.
/// </summary>
internal EventHandler<SendingRequestEventArgs> SendingRequestEventArgs { get; set; }
/// <summary>
/// (Optional) transport interceptor factory
/// </summary>
internal Func<TransportClient, TransportClient> TransportClientHandlerFactory { get; set; }
/// <summary>
/// A callback delegate to do custom certificate validation for both HTTP and TCP.
/// </summary>
/// <remarks>
/// <para>
/// Emulator: To ignore SSL Certificate please suffix connectionstring with "DisableServerCertificateValidation=True;".
/// When CosmosClientOptions.HttpClientFactory is used, SSL certificate needs to be handled appropriately.
/// NOTE: DO NOT use the `DisableServerCertificateValidation` flag in production (only for emulator)
/// </para>
/// </remarks>
public Func<X509Certificate2, X509Chain, SslPolicyErrors, bool> ServerCertificateCustomValidationCallback { get; set; }
/// <summary>
/// Real call back that will be hooked down-stream to the transport clients (both http and tcp).
/// NOTE: All down stream real-usage should come through this API only and not through the public API.
///
/// Test hook DisableServerCertificateValidationInvocationCallback
/// - When configured will invoke it when ever custom validation is done
/// </summary>
internal Func<X509Certificate2, X509Chain, SslPolicyErrors, bool> GetServerCertificateCustomValidationCallback()
{
if (this.DisableServerCertificateValidation)
{
if (this.DisableServerCertificateValidationInvocationCallback == null)
{
return this.ServerCertificateCustomValidationCallback ?? ((_, _, _) => true);
}
else
{
return (X509Certificate2 cert, X509Chain chain, SslPolicyErrors policyErrors) =>
{
bool bValidationResult = true;
if (this.ServerCertificateCustomValidationCallback != null)
{
bValidationResult = this.ServerCertificateCustomValidationCallback(cert, chain, policyErrors);
}
this.DisableServerCertificateValidationInvocationCallback?.Invoke();
return bValidationResult;
};
}
}
return this.ServerCertificateCustomValidationCallback;
}
internal Action DisableServerCertificateValidationInvocationCallback { get; set; }
/// <summary>
/// API type for the account
/// </summary>
internal ApiType ApiType { get; set; }
/// <summary>
/// Optional store client factory instance to use for all transport requests.
/// </summary>
internal IStoreClientFactory StoreClientFactory { get; set; }
/// <summary>
/// Gets or sets the initial delay retry time in milliseconds for the Azure Cosmos DB service
/// for requests that hit RetryWithExceptions. This covers errors that occur due to concurrency errors in the store.
/// </summary>
/// <value>
/// The default value is 1 second. For an example on how to set this value, please refer to <see cref="ConnectionPolicy.RetryOptions"/>.
/// </value>
/// <remarks>
/// <para>
/// When a request fails due to a RetryWith error, the client delays and retries the request. This configures the client
/// to delay the time specified before retrying the request.
/// </para>
/// </remarks>
internal int? InitialRetryForRetryWithMilliseconds { get; set; }
/// <summary>
/// Gets or sets the maximum delay retry time in milliseconds for the Azure Cosmos DB service
/// for requests that hit RetryWithExceptions. This covers errors that occur due to concurrency errors in the store.
/// </summary>
/// <value>
/// The default value is 30 seconds. For an example on how to set this value, please refer to <see cref="ConnectionPolicy.RetryOptions"/>.
/// </value>
/// <remarks>
/// <para>
/// When a request fails due to a RetryWith error, the client delays and retries the request. This configures the maximum time
/// the client should delay before failing the request.
/// </para>
/// </remarks>
internal int? MaximumRetryForRetryWithMilliseconds { get; set; }
/// <summary>
/// Gets or sets the interval to salt retry with value. This will spread the retry values from 1..n from the exponential back-off
/// subscribed.
/// </summary>
/// <value>
/// The default value is to not salt.
/// </value>
/// <remarks>
/// <para>
/// When a request fails due to a RetryWith error, the client delays and retries the request. This configures the jitter on the retry attempted.
/// </para>
/// </remarks>
internal int? RandomSaltForRetryWithMilliseconds { get; set; }
/// <summary>
/// Gets or sets the total time to wait before failing the request for retry with failures.
/// subscribed.
/// </summary>
/// <value>
/// The default value 30 seconds.
/// </value>
/// <remarks>
/// <para>
/// When a request fails due to a RetryWith error, the client delays and retries the request. This configures total time spent waiting on the request.
/// </para>
/// </remarks>
internal int? TotalWaitTimeForRetryWithMilliseconds { get; set; }
/// <summary>
/// Flag that controls whether CPU monitoring thread is created to enrich timeout exceptions with additional diagnostic. Default value is true.
/// </summary>
internal bool? EnableCpuMonitor { get; set; }
/// <summary>
/// Flag indicates the value of DisableServerCertificateValidation flag set at connection string level.Default it is false.
/// </summary>
internal bool DisableServerCertificateValidation { get; set; }
/// <summary>
/// Gets or sets Client Telemetry Options like feature flags and corresponding options
/// </summary>
public CosmosClientTelemetryOptions CosmosClientTelemetryOptions { get; set; }
/// <summary>
/// Create a client with Fault Injection capabilities using the Cosmos DB Fault Injection Library.
/// </summary>
/// <example>
/// How to create a CosmosClient with Fault Injection capabilities.
/// <code language="c#">
/// <![CDATA[
/// FaultInjectionRule rule = new FaultInjectionRuleBuilder(
/// id: "ruleId",
/// condition: new FaultInjectionConditionBuilder()
/// .WithRegion("East US")
/// .Build(),
/// result: new FaultInjectionResultBuilder.GetResultBuilder(FaultInjectionServerErrorType.ServiceUnavailable)
/// .Build())
/// .Build();
///
/// FaultInjector faultInjector = new FaultInjector(new List<FaultInjectionRule>() { rule });
///
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// FaultInjector = faultInjector
/// };
///
/// CosmosClient client = new CosmosClient("connection string", clientOptions);
/// ]]>
/// </code>
/// </example>
public IFaultInjector FaultInjector
{
get => this.faultInjector;
set
{
this.faultInjector = value;
if (this.faultInjector != null)
{
this.ChaosInterceptorFactory = this.faultInjector.GetChaosInterceptorFactory();
}
}
}
/// <summary>
/// Sets the throughput bucket for requests created using cosmos client.
/// </summary>
/// <remarks>
/// If throughput bucket is also set at request level in <see cref="RequestOptions.ThroughputBucket"/>, that throughput bucket is used.
/// If <see cref="AllowBulkExecution"/> is set to true in CosmosClientOptions, throughput bucket can only be set at client level.
/// </remarks>
/// <seealso href="https://aka.ms/cosmsodb-bucketing"/>
internal int? ThroughputBucket { get; set; }
internal IChaosInterceptorFactory ChaosInterceptorFactory { get; set; }
internal void SetSerializerIfNotConfigured(CosmosSerializer serializer)
{
if (this.serializerInternal == null)
{
this.serializerInternal = serializer ?? throw new ArgumentNullException(nameof(serializer));
}
}
internal CosmosClientOptions Clone()
{
CosmosClientOptions cloneConfiguration = (CosmosClientOptions)this.MemberwiseClone();
return cloneConfiguration;
}
internal virtual ConnectionPolicy GetConnectionPolicy(int clientId)
{
this.ValidateDirectTCPSettings();
this.ValidateLimitToEndpointSettings();
this.ValidatePartitionLevelFailoverSettings();
ConnectionPolicy connectionPolicy = new ConnectionPolicy()
{
MaxConnectionLimit = this.GatewayModeMaxConnectionLimit,
RequestTimeout = this.RequestTimeout,
ConnectionMode = this.ConnectionMode,
ConnectionProtocol = this.ConnectionProtocol,
UserAgentContainer = this.CreateUserAgentContainerWithFeatures(clientId),
UseMultipleWriteLocations = true,
IdleTcpConnectionTimeout = this.IdleTcpConnectionTimeout,
OpenTcpConnectionTimeout = this.OpenTcpConnectionTimeout,
MaxRequestsPerTcpConnection = this.MaxRequestsPerTcpConnection,
MaxTcpConnectionsPerEndpoint = this.MaxTcpConnectionsPerEndpoint,
EnableEndpointDiscovery = !this.LimitToEndpoint,
EnablePartitionLevelFailover = this.EnablePartitionLevelFailover,
PortReuseMode = this.portReuseMode,
EnableTcpConnectionEndpointRediscovery = this.EnableTcpConnectionEndpointRediscovery,
EnableAdvancedReplicaSelectionForTcp = this.EnableAdvancedReplicaSelectionForTcp,
HttpClientFactory = this.httpClientFactory,
ServerCertificateCustomValidationCallback = this.ServerCertificateCustomValidationCallback,
CosmosClientTelemetryOptions = new CosmosClientTelemetryOptions()
};
if (this.CosmosClientTelemetryOptions != null)
{
connectionPolicy.CosmosClientTelemetryOptions = this.CosmosClientTelemetryOptions;
}
RegionNameMapper mapper = new RegionNameMapper();
if (!string.IsNullOrEmpty(this.ApplicationRegion))
{
connectionPolicy.SetCurrentLocation(mapper.GetCosmosDBRegionName(this.ApplicationRegion));