forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoolManager.cs
More file actions
373 lines (330 loc) · 12.2 KB
/
PoolManager.cs
File metadata and controls
373 lines (330 loc) · 12.2 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
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsyncRewriter;
using Npgsql.Logging;
namespace Npgsql
{
static class PoolManager
{
/// <summary>
/// Holds connector pools indexed by their connection strings.
/// </summary>
internal static ConcurrentDictionary<NpgsqlConnectionStringBuilder, ConnectorPool> Pools { get; }
/// <summary>
/// Maximum number of possible connections in the pool.
/// </summary>
internal const int PoolSizeLimit = 1024;
static PoolManager()
{
Pools = new ConcurrentDictionary<NpgsqlConnectionStringBuilder, ConnectorPool>();
#if NET45 || NET451
// When the appdomain gets unloaded (e.g. web app redeployment) attempt to nicely
// close idle connectors to prevent errors in PostgreSQL logs (#491).
AppDomain.CurrentDomain.DomainUnload += (sender, args) => ClearAll();
AppDomain.CurrentDomain.ProcessExit += (sender, args) => ClearAll();
#endif
}
internal static ConnectorPool GetOrAdd(NpgsqlConnectionStringBuilder connString)
{
Contract.Requires(connString != null);
Contract.Ensures(Contract.Result<ConnectorPool>() != null);
return Pools.GetOrAdd(connString, cs =>
{
if (cs.MaxPoolSize < cs.MinPoolSize)
throw new ArgumentException($"Connection can't have MaxPoolSize {cs.MaxPoolSize} under MinPoolSize {cs.MinPoolSize}");
return new ConnectorPool(cs);
});
}
internal static ConnectorPool Get(NpgsqlConnectionStringBuilder connString)
{
Contract.Requires(connString != null);
Contract.Ensures(Contract.Result<ConnectorPool>() != null);
return Pools[connString];
}
internal static void ClearAll()
{
foreach (var pool in Pools.Values)
pool.Clear();
}
}
partial class ConnectorPool
{
internal NpgsqlConnectionStringBuilder ConnectionString;
/// <summary>
/// Open connectors waiting to be requested by new connections
/// </summary>
internal IdleConnectorList Idle;
readonly int _max;
internal int Min { get; }
internal int Busy { get; private set; }
internal Queue<TaskCompletionSource<NpgsqlConnector>> Waiting;
/// <summary>
/// Incremented every time this pool is cleared via <see cref="NpgsqlConnection.ClearPool"/> or
/// <see cref="NpgsqlConnection.ClearAllPools"/>. Allows us to identify connections which were
/// created before the clear.
/// </summary>
int _clearCounter;
static Timer _pruningTimer;
readonly TimeSpan _pruningInterval;
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
internal ConnectorPool(NpgsqlConnectionStringBuilder csb)
{
_max = csb.MaxPoolSize;
Min = csb.MinPoolSize;
ConnectionString = csb;
_pruningInterval = TimeSpan.FromSeconds(ConnectionString.ConnectionPruningInterval);
Idle = new IdleConnectorList();
Waiting = new Queue<TaskCompletionSource<NpgsqlConnector>>();
}
[RewriteAsync]
internal NpgsqlConnector Allocate(NpgsqlConnection conn, NpgsqlTimeout timeout)
{
NpgsqlConnector connector;
Monitor.Enter(this);
while (Idle.Count > 0)
{
connector = Idle.Pop();
// An idle connector could be broken because of a keepalive
if (connector.IsBroken)
continue;
connector.Connection = conn;
Busy++;
EnsurePruningTimerState();
Monitor.Exit(this);
return connector;
}
Contract.Assert(Busy <= _max);
if (Busy == _max)
{
// TODO: Async cancellation
var tcs = new TaskCompletionSource<NpgsqlConnector>();
Waiting.Enqueue(tcs);
Monitor.Exit(this);
try
{
WaitForTask(tcs.Task, timeout.TimeLeft);
}
catch
{
// We're here if the timeout expired or the cancellation token was triggered
// Re-lock and check in case the task was set to completed after coming out of the Wait
lock (this)
{
if (!tcs.Task.IsCompleted)
{
tcs.SetCanceled();
throw;
}
}
}
connector = tcs.Task.Result;
connector.Connection = conn;
return connector;
}
// No idle connectors are available, and we're under the pool's maximum capacity.
Busy++;
Monitor.Exit(this);
try
{
connector = new NpgsqlConnector(conn) { ClearCounter = _clearCounter };
connector.Open(timeout);
EnsureMinPoolSize(conn);
return connector;
}
catch
{
lock (this)
Busy--;
throw;
}
}
internal void Release(NpgsqlConnector connector)
{
// If Clear/ClearAll has been been called since this connector was first opened,
// throw it away.
if (connector.ClearCounter < _clearCounter)
{
try
{
connector.Close();
}
catch (Exception e)
{
Log.Warn("Exception while closing outdated connector", e, connector.Id);
}
lock (this)
Busy--;
return;
}
if (connector.IsBroken)
{
lock (this)
Busy--;
return;
}
connector.Reset();
lock (this)
{
// If there are any pending open attempts in progress hand the connector off to
// them directly.
while (Waiting.Count > 0)
{
var tcs = Waiting.Dequeue();
// Some attempts may be in the queue but in cancelled state, since they've already timed out.
// Simply dequeue these and move on.
if (tcs.Task.IsCanceled)
continue;
// We have a pending open attempt. "Complete" it, handing off the connector.
// We do this in another thread because we don't want to execute the continuation here.
Task.Run(() => tcs.SetResult(connector));
return;
}
Idle.Push(connector);
Busy--;
EnsurePruningTimerState();
Contract.Assert(Idle.Count <= _max);
}
}
/// <summary>
/// Attempts to ensure, on a best-effort basis, that there are enough connections to meet MinPoolSize.
/// This method never throws an exception.
/// </summary>
void EnsureMinPoolSize(NpgsqlConnection conn)
{
int missing;
lock (this)
{
missing = Min - (Busy + Idle.Count);
if (missing <= 0)
return;
Busy += missing;
}
for (; missing > 0; missing--)
{
try
{
#if NET451 || NET45
var connector = new NpgsqlConnector((NpgsqlConnection) ((ICloneable) conn).Clone())
#else
var connector = new NpgsqlConnector(conn.Clone())
#endif
{
ClearCounter = _clearCounter
};
connector.Open();
connector.Reset();
lock (this)
{
Idle.Push(connector);
EnsurePruningTimerState();
Busy--;
}
}
catch (Exception e)
{
lock (this)
Busy -= missing;
Log.Warn("Connection error while attempting to ensure MinPoolSize", e);
return;
}
}
}
void EnsurePruningTimerState()
{
Contract.Requires(Monitor.IsEntered(this));
if (Idle.Count <= Min)
{
if (_pruningTimer != null)
{
_pruningTimer.Dispose();
_pruningTimer = null;
}
}
else if (_pruningTimer == null)
_pruningTimer = new Timer(PruneIdleConnectors, null, _pruningInterval, _pruningInterval);
}
internal void PruneIdleConnectors(object state)
{
if (Idle.Count <= Min)
return;
var idleLifetime = ConnectionString.ConnectionIdleLifetime;
lock (this)
{
while (Idle.Count > Min &&
(DateTime.UtcNow - Idle.Last.Value.ReleaseTimestamp).TotalSeconds >= idleLifetime)
{
var connector = Idle.Last.Value;
Idle.RemoveLast();
try
{
connector.Close();
}
catch (Exception e)
{
Log.Warn("Exception while closing connector", e, connector.Id);
}
}
EnsurePruningTimerState();
}
}
internal void Clear()
{
NpgsqlConnector[] idleConnectors;
lock (this)
{
idleConnectors = Idle.ToArray();
Idle.Clear();
EnsurePruningTimerState();
}
foreach (var connector in idleConnectors)
{
try { connector.Close(); }
catch (Exception e)
{
Log.Warn("Exception while closing connector during clear", e, connector.Id);
}
}
_clearCounter++;
}
void WaitForTask(Task task, TimeSpan timeout)
{
if (!task.Wait(timeout))
throw new NpgsqlException($"The connection pool has been exhausted, either raise MaxPoolSize (currently {_max}) or Timeout (currently {ConnectionString.Timeout} seconds)");
}
async Task WaitForTaskAsync(Task task, TimeSpan timeout, CancellationToken cancellationToken)
{
var timeoutTask = Task.Delay(timeout, cancellationToken);
if (task != await Task.WhenAny(task, timeoutTask).ConfigureAwait(false))
{
cancellationToken.ThrowIfCancellationRequested();
throw new NpgsqlException($"The connection pool has been exhausted, either raise MaxPoolSize (currently {_max}) or Timeout (currently {ConnectionString.Timeout} seconds)");
}
}
public override string ToString() => $"[{Busy} busy, {Idle.Count} idle, {Waiting.Count} waiting]";
[ContractInvariantMethod]
void ObjectInvariants()
{
Contract.Invariant(Busy <= _max);
}
}
class IdleConnectorList : LinkedList<NpgsqlConnector>
{
internal void Push(NpgsqlConnector connector)
{
connector.ReleaseTimestamp = DateTime.UtcNow;
AddFirst(connector);
}
internal NpgsqlConnector Pop()
{
var connector = First.Value;
connector.ReleaseTimestamp = DateTime.UtcNow;
RemoveFirst();
return connector;
}
}
}