-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusterOptions.cs
More file actions
177 lines (154 loc) · 7.02 KB
/
ClusterOptions.cs
File metadata and controls
177 lines (154 loc) · 7.02 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
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
#region License
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2025 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion
using System.ComponentModel;
using System.Security.Authentication;
using Couchbase.AnalyticsClient.DI;
using Couchbase.AnalyticsClient.HTTP;
using Couchbase.AnalyticsClient.Internal;
using Couchbase.AnalyticsClient.Internal.DI;
using Couchbase.AnalyticsClient.Internal.Utils;
using Couchbase.Core.Utils;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Couchbase.AnalyticsClient.Options;
public record ClusterOptions
{
public SecurityOptions SecurityOptions { get; private set; } = new();
public TimeoutOptions TimeoutOptions { get; private set; } = new();
[InterfaceStability(StabilityLevel.Volatile)]
public uint MaxRetries { get; private set; } = 7;
internal ConnectionString? ConnectionStringValue { get; private set; }
private ILoggerFactory? Logging { get; set; }
public ClusterOptions WithSecurityOptions(SecurityOptions securityOptions)
{
return this with { SecurityOptions = securityOptions };
}
public ClusterOptions WithSecurityOptions(Func<SecurityOptions, SecurityOptions> securityOptions)
{
SecurityOptions = securityOptions.Invoke(SecurityOptions);
return this;
}
public ClusterOptions WithTimeoutOptions(TimeoutOptions timeoutOptions)
{
return this with { TimeoutOptions = timeoutOptions };
}
public ClusterOptions WithTimeoutOptions(Func<TimeoutOptions, TimeoutOptions> securityOptions)
{
TimeoutOptions = securityOptions.Invoke(TimeoutOptions);
return this;
}
[InterfaceStability(StabilityLevel.Volatile)]
public ClusterOptions WithMaxRetries(uint maxRetries)
{
return this with { MaxRetries = maxRetries };
}
/// <summary>
/// Set the <see cref="ILoggerFactory"/> to use for logging.
/// </summary>
/// <param name="loggerFactory">The logger factory.</param>
/// <returns>
/// A reference to this <see cref="ClusterOptions"/> object for method chaining.
/// </returns>
public ClusterOptions WithLogging(ILoggerFactory? loggerFactory = null)
{
return this with { Logging = loggerFactory };
}
private readonly IDictionary<Type, IServiceFactory> _services = DefaultServices.GetDefaultServices();
internal ICouchbaseServiceProvider BuildServiceProvider(ICredential? credential = null)
{
this.AddClusterService(this);
this.AddClusterService(Logging ??= new NullLoggerFactory());
if (credential is not null) this.AddClusterService(credential);
return new CouchbaseServiceProvider(_services);
}
/// <summary>
/// Register a service with the cluster's <see cref="CouchbaseServiceProvider"/>.
/// </summary>
/// <typeparam name="TService">The type of the service which will be requested.</typeparam>
/// <typeparam name="TImplementation">The type of the service implementation which is returned.</typeparam>
/// <param name="factory">Factory which will create the service.</param>
/// <param name="lifetime">Lifetime of the service.</param>
/// <returns>The <see cref="ClusterOptions"/>.</returns>
public ClusterOptions AddService<TService, TImplementation>(
Func<IServiceProvider, TImplementation> factory,
ClusterServiceLifetime lifetime)
where TImplementation : notnull, TService
{
_services[typeof(TService)] = lifetime switch
{
ClusterServiceLifetime.Transient => new TransientServiceFactory(serviceProvider => factory(serviceProvider)),
ClusterServiceLifetime.Cluster => new SingletonServiceFactory(serviceProvider => factory(serviceProvider)),
_ => throw new InvalidEnumArgumentException(nameof(lifetime), (int) lifetime,
typeof(ClusterServiceLifetime))
};
return this;
}
/// <summary>
/// The connection string for the cluster.
/// </summary>
internal string? ConnectionString
{
get => ConnectionStringValue?.ToString();
set
{
ConnectionStringValue = value != null ? Internal.ConnectionString.Parse(value) : null;
if (ConnectionStringValue == null) return;
if (ConnectionStringValue.TryGetParameter(ConnectionStringParams.MaxRetries, out uint maxRetries))
{
MaxRetries = maxRetries;
}
if (ConnectionStringValue.TryGetParameter(ConnectionStringParams.ConnectTimeout, out TimeSpan connectTimeout))
{
TimeoutOptions = TimeoutOptions.WithConnectTimeout(connectTimeout);
}
if (ConnectionStringValue.TryGetParameter(ConnectionStringParams.DispatchTimeout, out TimeSpan dispatchTimeout))
{
TimeoutOptions = TimeoutOptions.WithDispatchTimeout(dispatchTimeout);
}
if (ConnectionStringValue.TryGetParameter(ConnectionStringParams.QueryTimeout, out TimeSpan queryTimeout))
{
TimeoutOptions = TimeoutOptions.WithQueryTimeout(queryTimeout);
}
if (ConnectionStringValue.TryGetParameter(ConnectionStringParams.TrustOnlyPemFile, out string pathToPemFile))
{
SecurityOptions = SecurityOptions.WithTrustOnlyPemFile(pathToPemFile);
}
if (ConnectionStringValue.TryGetParameter(ConnectionStringParams.DisableServerCertificateVerification, out bool disableServerCertificateVerification))
{
SecurityOptions = SecurityOptions.WithDisableCertificateVerification(disableServerCertificateVerification);
}
if (ConnectionStringValue.TryGetParameter(ConnectionStringParams.CipherSuites, out string? cipherSuites))
{
var protocolStrings = cipherSuites.Split(',');
var protocols = SslProtocols.None;
foreach (var protocolString in protocolStrings)
{
if (Enum.TryParse<SslProtocols>(protocolString.Trim(), ignoreCase: true, out var protocol))
{
protocols |= protocol;
}
}
SecurityOptions = SecurityOptions.WithSslProtocols(protocols);
}
}
}
//internal JsonSerializer Serializer { get; init; } //static
}