-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ConnectionPoolSettings.cs
196 lines (182 loc) · 8.39 KB
/
ConnectionPoolSettings.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
/* Copyright 2013-present MongoDB 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.
*/
using System;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver.Core.Configuration
{
/// <summary>
/// Represents settings for a connection pool.
/// </summary>
public class ConnectionPoolSettings
{
// fields
private readonly TimeSpan _maintenanceInterval;
private readonly int _maxConnections;
private readonly int _minConnections;
private readonly int _waitQueueSize;
private readonly TimeSpan _waitQueueTimeout;
private readonly int _maxConnecting;
private readonly bool _isPausable;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionPoolSettings"/> class.
/// </summary>
/// <param name="maintenanceInterval">The maintenance interval.</param>
/// <param name="maxConnections">The maximum number of connections.</param>
/// <param name="minConnections">The minimum number of connections.</param>
/// <param name="waitQueueSize">Size of the wait queue.</param>
/// <param name="waitQueueTimeout">The wait queue timeout.</param>
/// <param name="maxConnecting">The maximum concurrently connecting connections.</param>
public ConnectionPoolSettings(
Optional<TimeSpan> maintenanceInterval = default(Optional<TimeSpan>),
Optional<int> maxConnections = default(Optional<int>),
Optional<int> minConnections = default(Optional<int>),
Optional<int> waitQueueSize = default(Optional<int>),
Optional<TimeSpan> waitQueueTimeout = default(Optional<TimeSpan>),
Optional<int> maxConnecting = default(Optional<int>))
: this(maintenanceInterval: maintenanceInterval,
maxConnections: maxConnections,
minConnections: minConnections,
waitQueueSize: waitQueueSize,
waitQueueTimeout: waitQueueTimeout,
maxConnecting: maxConnecting,
isPausable: true)
{
}
private ConnectionPoolSettings(
Optional<TimeSpan> maintenanceInterval = default(Optional<TimeSpan>),
Optional<int> maxConnections = default(Optional<int>),
Optional<int> minConnections = default(Optional<int>),
Optional<int> waitQueueSize = default(Optional<int>),
Optional<TimeSpan> waitQueueTimeout = default(Optional<TimeSpan>),
Optional<bool> isPausable = default(Optional<bool>),
Optional<int> maxConnecting = default(Optional<int>))
{
_maintenanceInterval = Ensure.IsInfiniteOrGreaterThanOrEqualToZero(maintenanceInterval.WithDefault(TimeSpan.FromMinutes(1)), nameof(maintenanceInterval));
_maxConnections = Ensure.IsGreaterThanZero(maxConnections.WithDefault(100), nameof(maxConnections));
_maxConnecting = Ensure.IsGreaterThanZero(maxConnecting.WithDefault(MongoInternalDefaults.ConnectionPool.MaxConnecting), nameof(maxConnecting));
_minConnections = Ensure.IsGreaterThanOrEqualToZero(minConnections.WithDefault(0), nameof(minConnections));
#pragma warning disable CS0618 // Type or member is obsolete
_waitQueueSize = Ensure.IsGreaterThanOrEqualToZero(waitQueueSize.WithDefault(ConnectionStringConversions.GetComputedWaitQueueSize(_maxConnections, 5)), nameof(waitQueueSize));
#pragma warning restore CS0618 // Type or member is obsolete
_waitQueueTimeout = Ensure.IsInfiniteOrGreaterThanOrEqualToZero(waitQueueTimeout.WithDefault(TimeSpan.FromMinutes(2)), nameof(waitQueueTimeout));
_isPausable = isPausable.WithDefault(true);
}
// properties
/// <summary>
/// Gets the maintenance interval.
/// <see cref="System.Threading.Timeout.Infinite"/> values indicates that maintenance thread is disabled.
/// </summary>
/// <value>
/// The maintenance interval.
/// </value>
public TimeSpan MaintenanceInterval
{
get { return _maintenanceInterval; }
}
/// <summary>
/// Gets the maximum number of connections a pool may be establishing concurrently. Defaults to 2.
/// </summary>
/// <value>
/// The maximum concurrently connecting connections.
/// </value>
public int MaxConnecting
{
get { return _maxConnecting; }
}
/// <summary>
/// Gets the maximum number of connections.
/// </summary>
/// <value>
/// The maximum number of connections.
/// </value>
public int MaxConnections
{
get { return _maxConnections; }
}
/// <summary>
/// Gets the minimum number of connections.
/// </summary>
/// <value>
/// The minimum number of connections.
/// </value>
public int MinConnections
{
get { return _minConnections; }
}
/// <summary>
/// Gets the size of the wait queue.
/// </summary>
/// <value>
/// The size of the wait queue.
/// </value>
[Obsolete("This property will be removed in a later release.")]
public int WaitQueueSize
{
get { return _waitQueueSize; }
}
/// <summary>
/// Gets the wait queue timeout.
/// </summary>
/// <value>
/// The wait queue timeout.
/// </value>
public TimeSpan WaitQueueTimeout
{
get { return _waitQueueTimeout; }
}
// internal properties
internal bool IsPausable
{
get { return _isPausable; }
}
// methods
/// <summary>
/// Returns a new ConnectionPoolSettings instance with some settings changed.
/// </summary>
/// <param name="maintenanceInterval">The maintenance interval.</param>
/// <param name="maxConnecting">The maximum concurrently connecting connections.</param>
/// <param name="maxConnections">The maximum connections.</param>
/// <param name="minConnections">The minimum connections.</param>
/// <param name="waitQueueSize">Size of the wait queue.</param>
/// <param name="waitQueueTimeout">The wait queue timeout.</param>
/// <returns>A new ConnectionPoolSettings instance.</returns>
public ConnectionPoolSettings With(
Optional<TimeSpan> maintenanceInterval = default(Optional<TimeSpan>),
Optional<int> maxConnections = default(Optional<int>),
Optional<int> minConnections = default(Optional<int>),
Optional<int> waitQueueSize = default(Optional<int>),
Optional<TimeSpan> waitQueueTimeout = default(Optional<TimeSpan>),
Optional<int> maxConnecting = default(Optional<int>))
{
return new ConnectionPoolSettings(
maintenanceInterval: maintenanceInterval.WithDefault(_maintenanceInterval),
maxConnections: maxConnections.WithDefault(_maxConnections),
minConnections: minConnections.WithDefault(_minConnections),
waitQueueSize: waitQueueSize.WithDefault(_waitQueueSize),
waitQueueTimeout: waitQueueTimeout.WithDefault(_waitQueueTimeout),
maxConnecting: maxConnecting.WithDefault(_maxConnecting));
}
internal ConnectionPoolSettings WithInternal(Optional<bool> isPausable = default(Optional<bool>)) =>
new ConnectionPoolSettings(
maintenanceInterval: _maintenanceInterval,
maxConnections: _maxConnections,
minConnections: _minConnections,
waitQueueSize: _waitQueueSize,
waitQueueTimeout: _waitQueueTimeout,
isPausable: isPausable.WithDefault(_isPausable),
maxConnecting: _maxConnecting);
}
}