-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ConfigurationOptions.cs
1082 lines (976 loc) · 45.9 KB
/
ConfigurationOptions.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using StackExchange.Redis.Configuration;
namespace StackExchange.Redis
{
/// <summary>
/// The options relevant to a set of redis connections.
/// </summary>
/// <remarks>
/// Some options are not observed by a <see cref="ConnectionMultiplexer"/> after initial creation:
/// <list type="bullet">
/// <item><see cref="CommandMap"/></item>
/// <item><see cref="ConfigurationChannel"/></item>
/// <item><see cref="EndPoints"/></item>
/// <item><see cref="SocketManager"/></item>
/// </list>
/// </remarks>
public sealed class ConfigurationOptions : ICloneable
{
private static class OptionKeys
{
public static int ParseInt32(string key, string value, int minValue = int.MinValue, int maxValue = int.MaxValue)
{
if (!Format.TryParseInt32(value, out int tmp)) throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' requires an integer value; the value '{value}' is not recognised.");
if (tmp < minValue) throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' has a minimum value of '{minValue}'; the value '{tmp}' is not permitted.");
if (tmp > maxValue) throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' has a maximum value of '{maxValue}'; the value '{tmp}' is not permitted.");
return tmp;
}
internal static bool ParseBoolean(string key, string value)
{
if (!Format.TryParseBoolean(value, out bool tmp)) throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' requires a boolean value; the value '{value}' is not recognised.");
return tmp;
}
internal static Version ParseVersion(string key, string value)
{
if (Format.TryParseVersion(value, out Version? tmp))
{
return tmp;
}
throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' requires a version value; the value '{value}' is not recognised.");
}
internal static Proxy ParseProxy(string key, string value)
{
if (!Enum.TryParse(value, true, out Proxy tmp)) throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' requires a proxy value; the value '{value}' is not recognised.");
return tmp;
}
internal static SslProtocols ParseSslProtocols(string key, string? value)
{
//Flags expect commas as separators, but we need to use '|' since commas are already used in the connection string to mean something else
value = value?.Replace("|", ",");
if (!Enum.TryParse(value, true, out SslProtocols tmp)) throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' requires an SslProtocol value (multiple values separated by '|'); the value '{value}' is not recognised.");
return tmp;
}
internal static RedisProtocol ParseRedisProtocol(string key, string value)
{
if (TryParseRedisProtocol(value, out var protocol)) return protocol;
throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' requires a RedisProtocol value or a known protocol version number; the value '{value}' is not recognised.");
}
internal static void Unknown(string key) =>
throw new ArgumentException($"Keyword '{key}' is not supported.", key);
internal const string
AbortOnConnectFail = "abortConnect",
AllowAdmin = "allowAdmin",
AsyncTimeout = "asyncTimeout",
ChannelPrefix = "channelPrefix",
ConfigChannel = "configChannel",
ConfigCheckSeconds = "configCheckSeconds",
ConnectRetry = "connectRetry",
ConnectTimeout = "connectTimeout",
DefaultDatabase = "defaultDatabase",
HighPrioritySocketThreads = "highPriorityThreads",
KeepAlive = "keepAlive",
ClientName = "name",
User = "user",
Password = "password",
PreserveAsyncOrder = "preserveAsyncOrder",
Proxy = "proxy",
ResolveDns = "resolveDns",
ResponseTimeout = "responseTimeout",
ServiceName = "serviceName",
Ssl = "ssl",
SslHost = "sslHost",
SslProtocols = "sslProtocols",
SyncTimeout = "syncTimeout",
TieBreaker = "tiebreaker",
Version = "version",
WriteBuffer = "writeBuffer",
CheckCertificateRevocation = "checkCertificateRevocation",
Tunnel = "tunnel",
SetClientLibrary = "setlib",
Protocol = "protocol";
private static readonly Dictionary<string, string> normalizedOptions = new[]
{
AbortOnConnectFail,
AllowAdmin,
AsyncTimeout,
ChannelPrefix,
ClientName,
ConfigChannel,
ConfigCheckSeconds,
ConnectRetry,
ConnectTimeout,
DefaultDatabase,
HighPrioritySocketThreads,
KeepAlive,
User,
Password,
PreserveAsyncOrder,
Proxy,
ResolveDns,
ServiceName,
Ssl,
SslHost,
SslProtocols,
SyncTimeout,
TieBreaker,
Version,
WriteBuffer,
CheckCertificateRevocation,
Protocol,
}.ToDictionary(x => x, StringComparer.OrdinalIgnoreCase);
public static string TryNormalize(string value)
{
if (value != null && normalizedOptions.TryGetValue(value, out string? tmp))
{
return tmp ?? "";
}
return value ?? "";
}
}
private DefaultOptionsProvider? defaultOptions;
private bool? allowAdmin, abortOnConnectFail, resolveDns, ssl, checkCertificateRevocation,
includeDetailInExceptions, includePerformanceCountersInExceptions, setClientLibrary;
private string? tieBreaker, sslHost, configChannel, user, password;
private TimeSpan? heartbeatInterval;
private CommandMap? commandMap;
private Version? defaultVersion;
private int? keepAlive, asyncTimeout, syncTimeout, connectTimeout, responseTimeout, connectRetry, configCheckSeconds;
private Proxy? proxy;
private IReconnectRetryPolicy? reconnectRetryPolicy;
private BacklogPolicy? backlogPolicy;
private ILoggerFactory? loggerFactory;
/// <summary>
/// A LocalCertificateSelectionCallback delegate responsible for selecting the certificate used for authentication; note
/// that this cannot be specified in the configuration-string.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly")]
public event LocalCertificateSelectionCallback? CertificateSelection;
/// <summary>
/// A RemoteCertificateValidationCallback delegate responsible for validating the certificate supplied by the remote party; note
/// that this cannot be specified in the configuration-string.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly")]
public event RemoteCertificateValidationCallback? CertificateValidation;
/// <summary>
/// The default (not explicitly configured) options for this connection, fetched based on our parsed endpoints.
/// </summary>
public DefaultOptionsProvider Defaults
{
get => defaultOptions ??= DefaultOptionsProvider.GetProvider(EndPoints);
set => defaultOptions = value;
}
/// <summary>
/// Allows modification of a <see cref="Socket"/> between creation and connection.
/// Passed in is the endpoint we're connecting to, which type of connection it is, and the socket itself.
/// For example, a specific local IP endpoint could be bound, linger time altered, etc.
/// </summary>
public Action<EndPoint, ConnectionType, Socket>? BeforeSocketConnect { get; set; }
internal Func<ConnectionMultiplexer, Action<string>, Task> AfterConnectAsync => Defaults.AfterConnectAsync;
/// <summary>
/// Gets or sets whether connect/configuration timeouts should be explicitly notified via a TimeoutException.
/// </summary>
public bool AbortOnConnectFail
{
get => abortOnConnectFail ?? Defaults.AbortOnConnectFail;
set => abortOnConnectFail = value;
}
/// <summary>
/// Indicates whether admin operations should be allowed.
/// </summary>
public bool AllowAdmin
{
get => allowAdmin ?? Defaults.AllowAdmin;
set => allowAdmin = value;
}
/// <summary>
/// Specifies the time in milliseconds that the system should allow for asynchronous operations (defaults to SyncTimeout).
/// </summary>
public int AsyncTimeout
{
get => asyncTimeout ?? SyncTimeout;
set => asyncTimeout = value;
}
/// <summary>
/// Indicates whether the connection should be encrypted
/// </summary>
[Obsolete("Please use .Ssl instead of .UseSsl, will be removed in 3.0."),
Browsable(false),
EditorBrowsable(EditorBrowsableState.Never)]
public bool UseSsl
{
get => Ssl;
set => Ssl = value;
}
/// <summary>
/// Gets or sets whether the library should identify itself by library-name/version when possible.
/// </summary>
public bool SetClientLibrary
{
get => setClientLibrary ?? Defaults.SetClientLibrary;
set => setClientLibrary = value;
}
/// <summary>
/// Gets or sets the library name to use for CLIENT SETINFO lib-name calls to Redis during handshake.
/// Defaults to "SE.Redis".
/// </summary>
/// <remarks>If the value is null, empty or whitespace, then the value from the options-provideer is used;
/// to disable the library name feature, use <see cref="SetClientLibrary"/> instead.</remarks>
public string? LibraryName { get; set; }
/// <summary>
/// Automatically encodes and decodes channels.
/// </summary>
public RedisChannel ChannelPrefix { get; set; }
/// <summary>
/// A Boolean value that specifies whether the certificate revocation list is checked during authentication.
/// </summary>
public bool CheckCertificateRevocation
{
get => checkCertificateRevocation ?? Defaults.CheckCertificateRevocation;
set => checkCertificateRevocation = value;
}
/// <summary>
/// Create a certificate validation check that checks against the supplied issuer even if not known by the machine.
/// </summary>
/// <param name="issuerCertificatePath">The file system path to find the certificate at.</param>
public void TrustIssuer(string issuerCertificatePath) => CertificateValidationCallback = TrustIssuerCallback(issuerCertificatePath);
/// <summary>
/// Create a certificate validation check that checks against the supplied issuer even if not known by the machine.
/// </summary>
/// <param name="issuer">The issuer to trust.</param>
public void TrustIssuer(X509Certificate2 issuer) => CertificateValidationCallback = TrustIssuerCallback(issuer);
internal static RemoteCertificateValidationCallback TrustIssuerCallback(string issuerCertificatePath)
=> TrustIssuerCallback(new X509Certificate2(issuerCertificatePath));
private static RemoteCertificateValidationCallback TrustIssuerCallback(X509Certificate2 issuer)
{
if (issuer == null) throw new ArgumentNullException(nameof(issuer));
return (object _, X509Certificate? certificate, X509Chain? __, SslPolicyErrors sslPolicyError)
=> sslPolicyError == SslPolicyErrors.RemoteCertificateChainErrors
&& certificate is X509Certificate2 v2
&& CheckTrustedIssuer(v2, issuer);
}
private static bool CheckTrustedIssuer(X509Certificate2 certificateToValidate, X509Certificate2 authority)
{
// reference: https://stackoverflow.com/questions/6497040/how-do-i-validate-that-a-certificate-was-created-by-a-particular-certification-a
X509Chain chain = new X509Chain();
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
chain.ChainPolicy.VerificationTime = DateTime.Now;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 0, 0);
chain.ChainPolicy.ExtraStore.Add(authority);
return chain.Build(certificateToValidate);
}
/// <summary>
/// The client name to use for all connections.
/// </summary>
public string? ClientName { get; set; }
/// <summary>
/// The number of times to repeat the initial connect cycle if no servers respond promptly.
/// </summary>
public int ConnectRetry
{
get => connectRetry ?? Defaults.ConnectRetry;
set => connectRetry = value;
}
/// <summary>
/// The command-map associated with this configuration.
/// </summary>
/// <remarks>
/// This is memoized when a <see cref="ConnectionMultiplexer"/> connects.
/// Modifying it afterwards will have no effect on already-created multiplexers.
/// </remarks>
public CommandMap CommandMap
{
get => commandMap ?? Defaults.CommandMap ?? Proxy switch
{
Proxy.Twemproxy => CommandMap.Twemproxy,
Proxy.Envoyproxy => CommandMap.Envoyproxy,
_ => CommandMap.Default,
};
set => commandMap = value ?? throw new ArgumentNullException(nameof(value));
}
/// <summary>
/// Gets the command map for a given server type, since some supersede settings when connecting.
/// </summary>
internal CommandMap GetCommandMap(ServerType? serverType) => serverType switch
{
ServerType.Sentinel => CommandMap.Sentinel,
_ => CommandMap,
};
/// <summary>
/// Channel to use for broadcasting and listening for configuration change notification.
/// </summary>
/// <remarks>
/// This is memoized when a <see cref="ConnectionMultiplexer"/> connects.
/// Modifying it afterwards will have no effect on already-created multiplexers.
/// </remarks>
public string ConfigurationChannel
{
get => configChannel ?? Defaults.ConfigurationChannel;
set => configChannel = value;
}
/// <summary>
/// Specifies the time in milliseconds that should be allowed for connection (defaults to 5 seconds unless SyncTimeout is higher).
/// </summary>
public int ConnectTimeout
{
get => connectTimeout ?? ((int?)Defaults.ConnectTimeout?.TotalMilliseconds) ?? Math.Max(5000, SyncTimeout);
set => connectTimeout = value;
}
/// <summary>
/// Specifies the default database to be used when calling <see cref="ConnectionMultiplexer.GetDatabase(int, object)"/> without any parameters.
/// </summary>
public int? DefaultDatabase { get; set; }
/// <summary>
/// The server version to assume.
/// </summary>
public Version DefaultVersion
{
get => defaultVersion ?? Defaults.DefaultVersion;
set => defaultVersion = value;
}
/// <summary>
/// The endpoints defined for this configuration.
/// </summary>
/// <remarks>
/// This is memoized when a <see cref="ConnectionMultiplexer"/> connects.
/// Modifying it afterwards will have no effect on already-created multiplexers.
/// </remarks>
public EndPointCollection EndPoints { get; init; } = new EndPointCollection();
/// <summary>
/// Controls how often the connection heartbeats. A heartbeat includes:
/// - Evaluating if any messages have timed out
/// - Evaluating connection status (checking for failures)
/// - Sending a server message to keep the connection alive if needed
/// </summary>
/// <remarks>
/// This defaults to 1000 milliseconds and should not be changed for most use cases.
/// If for example you want to evaluate whether commands have violated the <see cref="AsyncTimeout"/> at a lower fidelity
/// than 1000 milliseconds, you could lower this value.
/// Be aware setting this very low incurs additional overhead of evaluating the above more often.
/// </remarks>
public TimeSpan HeartbeatInterval
{
get => heartbeatInterval ?? Defaults.HeartbeatInterval;
set => heartbeatInterval = value;
}
/// <summary>
/// Use ThreadPriority.AboveNormal for SocketManager reader and writer threads (true by default).
/// If <see langword="false"/>, <see cref="ThreadPriority.Normal"/> will be used.
/// </summary>
[Obsolete($"This setting no longer has any effect, please use {nameof(SocketManager.SocketManagerOptions)}.{nameof(SocketManager.SocketManagerOptions.UseHighPrioritySocketThreads)} instead - this setting will be removed in 3.0.")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public bool HighPrioritySocketThreads
{
get => false;
set { }
}
/// <summary>
/// Should exceptions include identifiable details? (key names, additional .Data annotations)
/// </summary>
public bool IncludeDetailInExceptions
{
get => includeDetailInExceptions ?? Defaults.IncludeDetailInExceptions;
set => includeDetailInExceptions = value;
}
/// <summary>
/// Should exceptions include performance counter details?
/// </summary>
/// <remarks>
/// CPU usage, etc - note that this can be problematic on some platforms.
/// </remarks>
public bool IncludePerformanceCountersInExceptions
{
get => includePerformanceCountersInExceptions ?? Defaults.IncludePerformanceCountersInExceptions;
set => includePerformanceCountersInExceptions = value;
}
/// <summary>
/// Specifies the time in seconds at which connections should be pinged to ensure validity.
/// -1 Defaults to 60 Seconds
/// </summary>
public int KeepAlive
{
get => keepAlive ?? (int)Defaults.KeepAliveInterval.TotalSeconds;
set => keepAlive = value;
}
/// <summary>
/// The <see cref="ILoggerFactory"/> to get loggers for connection events.
/// Note: changes here only affect <see cref="ConnectionMultiplexer"/>s created after.
/// </summary>
public ILoggerFactory? LoggerFactory
{
get => loggerFactory ?? Defaults.LoggerFactory;
set => loggerFactory = value;
}
/// <summary>
/// The username to use to authenticate with the server.
/// </summary>
public string? User
{
get => user ?? Defaults.User;
set => user = value;
}
/// <summary>
/// The password to use to authenticate with the server.
/// </summary>
public string? Password
{
get => password ?? Defaults.Password;
set => password = value;
}
/// <summary>
/// Specifies whether asynchronous operations should be invoked in a way that guarantees their original delivery order.
/// </summary>
[Obsolete("Not supported; if you require ordered pub/sub, please see " + nameof(ChannelMessageQueue) + " - this will be removed in 3.0.", false)]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public bool PreserveAsyncOrder
{
get => false;
set { }
}
/// <summary>
/// Type of proxy to use (if any); for example <see cref="Proxy.Twemproxy"/>.
/// </summary>
public Proxy Proxy
{
get => proxy ?? Defaults.Proxy;
set => proxy = value;
}
/// <summary>
/// The retry policy to be used for connection reconnects.
/// </summary>
public IReconnectRetryPolicy ReconnectRetryPolicy
{
get => reconnectRetryPolicy ??= Defaults.ReconnectRetryPolicy ?? new ExponentialRetry(ConnectTimeout / 2);
set => reconnectRetryPolicy = value;
}
/// <summary>
/// The backlog policy to be used for commands when a connection is unhealthy.
/// </summary>
public BacklogPolicy BacklogPolicy
{
get => backlogPolicy ?? Defaults.BacklogPolicy;
set => backlogPolicy = value;
}
/// <summary>
/// Indicates whether endpoints should be resolved via DNS before connecting.
/// If enabled the ConnectionMultiplexer will not re-resolve DNS when attempting to re-connect after a connection failure.
/// </summary>
public bool ResolveDns
{
get => resolveDns ?? Defaults.ResolveDns;
set => resolveDns = value;
}
/// <summary>
/// Specifies the time in milliseconds that the system should allow for responses before concluding that the socket is unhealthy.
/// </summary>
[Obsolete("This setting no longer has any effect, and should not be used - will be removed in 3.0.")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public int ResponseTimeout
{
get => 0;
set { }
}
/// <summary>
/// The service name used to resolve a service via sentinel.
/// </summary>
public string? ServiceName { get; set; }
/// <summary>
/// Gets or sets the SocketManager instance to be used with these options.
/// If this is null a shared cross-multiplexer <see cref="SocketManager"/> is used.
/// </summary>
/// <remarks>
/// This is only used when a <see cref="ConnectionMultiplexer"/> is created.
/// Modifying it afterwards will have no effect on already-created multiplexers.
/// </remarks>
public SocketManager? SocketManager { get; set; }
#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// A <see cref="SslClientAuthenticationOptions"/> provider for a given host, for custom TLS connection options.
/// Note: this overrides *all* other TLS and certificate settings, only for advanced use cases.
/// </summary>
public Func<string, SslClientAuthenticationOptions>? SslClientAuthenticationOptions { get; set; }
#endif
/// <summary>
/// Indicates whether the connection should be encrypted.
/// </summary>
public bool Ssl
{
get => ssl ?? Defaults.GetDefaultSsl(EndPoints);
set => ssl = value;
}
/// <summary>
/// The target-host to use when validating SSL certificate; setting a value here enables SSL mode.
/// </summary>
public string? SslHost
{
get => sslHost ?? Defaults.GetSslHostFromEndpoints(EndPoints);
set => sslHost = value;
}
/// <summary>
/// Configures which SSL/TLS protocols should be allowed. If not set, defaults are chosen by the .NET framework.
/// </summary>
public SslProtocols? SslProtocols { get; set; }
/// <summary>
/// Specifies the time in milliseconds that the system should allow for synchronous operations (defaults to 5 seconds).
/// </summary>
public int SyncTimeout
{
get => syncTimeout ?? (int)Defaults.SyncTimeout.TotalMilliseconds;
set => syncTimeout = value;
}
/// <summary>
/// Tie-breaker used to choose between primaries (must match the endpoint exactly).
/// </summary>
public string TieBreaker
{
get => tieBreaker ?? Defaults.TieBreaker;
set => tieBreaker = value;
}
/// <summary>
/// The size of the output buffer to use.
/// </summary>
[Obsolete("This setting no longer has any effect, and should not be used - will be removed in 3.0.")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public int WriteBuffer
{
get => 0;
set { }
}
internal LocalCertificateSelectionCallback? CertificateSelectionCallback
{
get => CertificateSelection;
private set => CertificateSelection = value;
}
// these just rip out the underlying handlers, bypassing the event accessors - needed when creating the SSL stream
internal RemoteCertificateValidationCallback? CertificateValidationCallback
{
get => CertificateValidation;
private set => CertificateValidation = value;
}
/// <summary>
/// Check configuration every n seconds (every minute by default).
/// </summary>
public int ConfigCheckSeconds
{
get => configCheckSeconds ?? (int)Defaults.ConfigCheckInterval.TotalSeconds;
set => configCheckSeconds = value;
}
/// <summary>
/// Parse the configuration from a comma-delimited configuration string.
/// </summary>
/// <param name="configuration">The configuration string to parse.</param>
/// <exception cref="ArgumentNullException"><paramref name="configuration"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="configuration"/> is empty.</exception>
public static ConfigurationOptions Parse(string configuration) => Parse(configuration, false);
/// <summary>
/// Parse the configuration from a comma-delimited configuration string.
/// </summary>
/// <param name="configuration">The configuration string to parse.</param>
/// <param name="ignoreUnknown">Whether to ignore unknown elements in <paramref name="configuration"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="configuration"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="configuration"/> is empty.</exception>
public static ConfigurationOptions Parse(string configuration, bool ignoreUnknown) =>
new ConfigurationOptions().DoParse(configuration, ignoreUnknown);
/// <summary>
/// Create a copy of the configuration.
/// </summary>
public ConfigurationOptions Clone() => new ConfigurationOptions
{
defaultOptions = defaultOptions,
ClientName = ClientName,
ServiceName = ServiceName,
keepAlive = keepAlive,
syncTimeout = syncTimeout,
asyncTimeout = asyncTimeout,
allowAdmin = allowAdmin,
defaultVersion = defaultVersion,
connectTimeout = connectTimeout,
user = user,
password = password,
tieBreaker = tieBreaker,
ssl = ssl,
sslHost = sslHost,
configChannel = configChannel,
abortOnConnectFail = abortOnConnectFail,
resolveDns = resolveDns,
proxy = proxy,
commandMap = commandMap,
CertificateValidationCallback = CertificateValidationCallback,
CertificateSelectionCallback = CertificateSelectionCallback,
ChannelPrefix = ChannelPrefix.Clone(),
SocketManager = SocketManager,
connectRetry = connectRetry,
configCheckSeconds = configCheckSeconds,
responseTimeout = responseTimeout,
DefaultDatabase = DefaultDatabase,
reconnectRetryPolicy = reconnectRetryPolicy,
backlogPolicy = backlogPolicy,
SslProtocols = SslProtocols,
checkCertificateRevocation = checkCertificateRevocation,
BeforeSocketConnect = BeforeSocketConnect,
EndPoints = EndPoints.Clone(),
LoggerFactory = LoggerFactory,
#if NETCOREAPP3_1_OR_GREATER
SslClientAuthenticationOptions = SslClientAuthenticationOptions,
#endif
Tunnel = Tunnel,
setClientLibrary = setClientLibrary,
LibraryName = LibraryName,
Protocol = Protocol,
};
/// <summary>
/// Apply settings to configure this instance of <see cref="ConfigurationOptions"/>, e.g. for a specific scenario.
/// </summary>
/// <param name="configure">An action that will update the properties of this <see cref="ConfigurationOptions"/> instance.</param>
/// <returns>This <see cref="ConfigurationOptions"/> instance, with any changes <paramref name="configure"/> made.</returns>
public ConfigurationOptions Apply(Action<ConfigurationOptions> configure)
{
configure?.Invoke(this);
return this;
}
/// <summary>
/// Resolve the default port for any endpoints that did not have a port explicitly specified.
/// </summary>
public void SetDefaultPorts() => EndPoints.SetDefaultPorts(ServerType.Standalone, ssl: Ssl);
internal bool IsSentinel => !string.IsNullOrEmpty(ServiceName);
/// <summary>
/// Gets a tie breaker if we both have one set, and should be using one.
/// </summary>
internal bool TryGetTieBreaker(out RedisKey tieBreaker)
{
var key = TieBreaker;
if (!IsSentinel && !string.IsNullOrWhiteSpace(key))
{
tieBreaker = key;
return true;
}
tieBreaker = default;
return false;
}
/// <summary>
/// Returns the effective configuration string for this configuration, including Redis credentials.
/// </summary>
/// <remarks>
/// Includes password to allow generation of configuration strings used for connecting multiplexer.
/// </remarks>
public override string ToString() => ToString(includePassword: true);
/// <summary>
/// Returns the effective configuration string for this configuration
/// with the option to include or exclude the password from the string.
/// </summary>
/// <param name="includePassword">Whether to include the password.</param>
public string ToString(bool includePassword)
{
var sb = new StringBuilder();
foreach (var endpoint in EndPoints)
{
Append(sb, Format.ToString(endpoint));
}
Append(sb, OptionKeys.ClientName, ClientName);
Append(sb, OptionKeys.ServiceName, ServiceName);
Append(sb, OptionKeys.KeepAlive, keepAlive);
Append(sb, OptionKeys.SyncTimeout, syncTimeout);
Append(sb, OptionKeys.AsyncTimeout, asyncTimeout);
Append(sb, OptionKeys.AllowAdmin, allowAdmin);
Append(sb, OptionKeys.Version, defaultVersion);
Append(sb, OptionKeys.ConnectTimeout, connectTimeout);
Append(sb, OptionKeys.User, user);
Append(sb, OptionKeys.Password, (includePassword || string.IsNullOrEmpty(password)) ? password : "*****");
Append(sb, OptionKeys.TieBreaker, tieBreaker);
Append(sb, OptionKeys.Ssl, ssl);
Append(sb, OptionKeys.SslProtocols, SslProtocols?.ToString().Replace(',', '|'));
Append(sb, OptionKeys.CheckCertificateRevocation, checkCertificateRevocation);
Append(sb, OptionKeys.SslHost, sslHost);
Append(sb, OptionKeys.ConfigChannel, configChannel);
Append(sb, OptionKeys.AbortOnConnectFail, abortOnConnectFail);
Append(sb, OptionKeys.ResolveDns, resolveDns);
Append(sb, OptionKeys.ChannelPrefix, (string?)ChannelPrefix);
Append(sb, OptionKeys.ConnectRetry, connectRetry);
Append(sb, OptionKeys.Proxy, proxy);
Append(sb, OptionKeys.ConfigCheckSeconds, configCheckSeconds);
Append(sb, OptionKeys.ResponseTimeout, responseTimeout);
Append(sb, OptionKeys.DefaultDatabase, DefaultDatabase);
Append(sb, OptionKeys.SetClientLibrary, setClientLibrary);
Append(sb, OptionKeys.Protocol, FormatProtocol(Protocol));
if (Tunnel is { IsInbuilt: true } tunnel)
{
Append(sb, OptionKeys.Tunnel, tunnel.ToString());
}
commandMap?.AppendDeltas(sb);
return sb.ToString();
static string? FormatProtocol(RedisProtocol? protocol) => protocol switch {
null => null,
RedisProtocol.Resp2 => "resp2",
RedisProtocol.Resp3 => "resp3",
_ => protocol.GetValueOrDefault().ToString(),
};
}
private static void Append(StringBuilder sb, object value)
{
if (value == null) return;
string s = Format.ToString(value);
if (!string.IsNullOrWhiteSpace(s))
{
if (sb.Length != 0) sb.Append(',');
sb.Append(s);
}
}
private static void Append(StringBuilder sb, string prefix, object? value)
{
string? s = value?.ToString();
if (!string.IsNullOrWhiteSpace(s))
{
if (sb.Length != 0) sb.Append(',');
if (!string.IsNullOrEmpty(prefix))
{
sb.Append(prefix).Append('=');
}
sb.Append(s);
}
}
private void Clear()
{
ClientName = ServiceName = user = password = tieBreaker = sslHost = configChannel = null;
keepAlive = syncTimeout = asyncTimeout = connectTimeout = connectRetry = configCheckSeconds = DefaultDatabase = null;
allowAdmin = abortOnConnectFail = resolveDns = ssl = setClientLibrary = null;
SslProtocols = null;
defaultVersion = null;
EndPoints.Clear();
commandMap = null;
CertificateSelection = null;
CertificateValidation = null;
ChannelPrefix = default;
SocketManager = null;
Tunnel = null;
}
object ICloneable.Clone() => Clone();
private ConfigurationOptions DoParse(string configuration, bool ignoreUnknown)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
if (string.IsNullOrWhiteSpace(configuration))
{
throw new ArgumentException("is empty", nameof(configuration));
}
Clear();
// break it down by commas
var arr = configuration.Split(StringSplits.Comma);
Dictionary<string, string?>? map = null;
foreach (var paddedOption in arr)
{
var option = paddedOption.Trim();
if (string.IsNullOrWhiteSpace(option)) continue;
// check for special tokens
int idx = option.IndexOf('=');
if (idx > 0)
{
var key = option.Substring(0, idx).Trim();
var value = option.Substring(idx + 1).Trim();
switch (OptionKeys.TryNormalize(key))
{
case OptionKeys.CheckCertificateRevocation:
CheckCertificateRevocation = OptionKeys.ParseBoolean(key, value);
break;
case OptionKeys.SyncTimeout:
SyncTimeout = OptionKeys.ParseInt32(key, value, minValue: 1);
break;
case OptionKeys.AsyncTimeout:
AsyncTimeout = OptionKeys.ParseInt32(key, value, minValue: 1);
break;
case OptionKeys.AllowAdmin:
AllowAdmin = OptionKeys.ParseBoolean(key, value);
break;
case OptionKeys.AbortOnConnectFail:
AbortOnConnectFail = OptionKeys.ParseBoolean(key, value);
break;
case OptionKeys.ResolveDns:
ResolveDns = OptionKeys.ParseBoolean(key, value);
break;
case OptionKeys.ServiceName:
ServiceName = value;
break;
case OptionKeys.ClientName:
ClientName = value;
break;
case OptionKeys.ChannelPrefix:
ChannelPrefix = RedisChannel.Literal(value);
break;
case OptionKeys.ConfigChannel:
ConfigurationChannel = value;
break;
case OptionKeys.KeepAlive:
KeepAlive = OptionKeys.ParseInt32(key, value);
break;
case OptionKeys.ConnectTimeout:
ConnectTimeout = OptionKeys.ParseInt32(key, value);
break;
case OptionKeys.ConnectRetry:
ConnectRetry = OptionKeys.ParseInt32(key, value);
break;
case OptionKeys.ConfigCheckSeconds:
ConfigCheckSeconds = OptionKeys.ParseInt32(key, value);
break;
case OptionKeys.Version:
DefaultVersion = OptionKeys.ParseVersion(key, value);
break;
case OptionKeys.User:
user = value;
break;
case OptionKeys.Password:
password = value;
break;
case OptionKeys.TieBreaker:
TieBreaker = value;
break;
case OptionKeys.Ssl:
Ssl = OptionKeys.ParseBoolean(key, value);
break;
case OptionKeys.SslHost:
SslHost = value;
break;
case OptionKeys.Proxy:
Proxy = OptionKeys.ParseProxy(key, value);
break;
case OptionKeys.DefaultDatabase:
DefaultDatabase = OptionKeys.ParseInt32(key, value);
break;
case OptionKeys.SslProtocols:
SslProtocols = OptionKeys.ParseSslProtocols(key, value);
break;
case OptionKeys.SetClientLibrary:
SetClientLibrary = OptionKeys.ParseBoolean(key, value);
break;
case OptionKeys.Tunnel:
if (value.IsNullOrWhiteSpace())
{
Tunnel = null;
}
else
{
// For backwards compatibility with `http:address_with_port`.
if (value.StartsWith("http:") && !value.StartsWith("http://"))
{
value = value.Insert(5, "//");
}
var uri = new Uri(value, UriKind.Absolute);
if (uri.Scheme != "http")
{
throw new ArgumentException("Tunnel cannot be parsed: " + value);
}
if (!Format.TryParseEndPoint($"{uri.Host}:{uri.Port}", out var ep))
{
throw new ArgumentException("HTTP tunnel cannot be parsed: " + value);
}
Tunnel = Tunnel.HttpProxy(ep);
}
break;
case OptionKeys.Protocol:
Protocol = OptionKeys.ParseRedisProtocol(key, value);
break;
// Deprecated options we ignore...
case OptionKeys.HighPrioritySocketThreads:
case OptionKeys.PreserveAsyncOrder:
case OptionKeys.ResponseTimeout:
case OptionKeys.WriteBuffer:
break;
default:
if (!string.IsNullOrEmpty(key) && key[0] == '$')
{
var cmdName = option.Substring(1, idx - 1);
if (Enum.TryParse(cmdName, true, out RedisCommand cmd))
{
map ??= new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
map[cmdName] = value;
}