-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathMultiplexedConnectionLockPool.cs
More file actions
208 lines (185 loc) · 8.9 KB
/
Copy pathMultiplexedConnectionLockPool.cs
File metadata and controls
208 lines (185 loc) · 8.9 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
namespace Medallion.Threading.Internal.Data;
/// <summary>
/// Implements a pool of <see cref="MultiplexedConnectionLock"/> instances
/// </summary>
#if DEBUG
public
#else
internal
#endif
sealed class MultiplexedConnectionLockPool<TConnectionSource>
where TConnectionSource : notnull
{
private readonly AsyncLock _lock = AsyncLock.Create();
private readonly Dictionary<TConnectionSource, Queue<MultiplexedConnectionLock>> _poolsByConnectionSource;
/// <summary>
/// The number of times we've called <see cref="StoreOrDisposeLockAsync(TConnectionSource, MultiplexedConnectionLock, bool)"/>
/// since we last called <see cref="PrunePoolsNoLockAsync"/>
/// </summary>
private uint _storeCountSinceLastPrune;
/// <summary>
/// The number of <see cref="MultiplexedConnectionLock"/>s stored in <see cref="_poolsByConnectionSource"/>
/// </summary>
private uint _pooledLockCount;
public MultiplexedConnectionLockPool(Func<TConnectionSource, DatabaseConnection> connectionFactory, IEqualityComparer<TConnectionSource>? comparer = null)
{
this.ConnectionFactory = connectionFactory;
this._poolsByConnectionSource = new(comparer);
}
internal Func<TConnectionSource, DatabaseConnection> ConnectionFactory { get; }
public async ValueTask<IDistributedSynchronizationHandle?> TryAcquireAsync<TLockCookie>(
TConnectionSource connectionSource,
string name,
TimeoutValue timeout,
IDbSynchronizationStrategy<TLockCookie> strategy,
TimeoutValue keepaliveCadence,
CancellationToken cancellationToken)
where TLockCookie : class
{
// opportunistic phase: see if we can use a connection that is already holding a lock
// to acquire the current lock
var existingLock = await this.GetExistingLockOrDefaultAsync(connectionSource).ConfigureAwait(false);
if (existingLock != null)
{
var canSafelyDisposeExistingLock = false;
try
{
var opportunisticResult = await TryAcquireAsync(existingLock, opportunistic: true).ConfigureAwait(false);
if (opportunisticResult.Handle != null) { return opportunisticResult.Handle; }
// this will always be false if handle is non-null, so we can set if afterwards
canSafelyDisposeExistingLock = opportunisticResult.CanSafelyDispose;
switch (opportunisticResult.Retry)
{
case MultiplexedConnectionLockRetry.NoRetry:
return null;
case MultiplexedConnectionLockRetry.RetryOnThisLock:
var retryOnThisLockResult = await TryAcquireAsync(existingLock, opportunistic: false).ConfigureAwait(false);
canSafelyDisposeExistingLock = retryOnThisLockResult.CanSafelyDispose;
return retryOnThisLockResult.Handle;
case MultiplexedConnectionLockRetry.Retry:
break;
default:
throw new InvalidOperationException("unexpected retry");
}
}
finally
{
// since we took this lock from the pool, always return it to the pool
await this.StoreOrDisposeLockAsync(connectionSource, existingLock, shouldDispose: canSafelyDisposeExistingLock).ConfigureAwait(false);
}
}
// normal phase: if we were not able to be opportunistic, ensure that we have a lock
var @lock = new MultiplexedConnectionLock(this.ConnectionFactory(connectionSource));
MultiplexedConnectionLock.Result? result = null;
try
{
result = await TryAcquireAsync(@lock, opportunistic: false).ConfigureAwait(false);
Invariant.Require(result!.Value.Retry == MultiplexedConnectionLockRetry.NoRetry, "Acquire on fresh lock should not recommend a retry");
}
finally
{
// if we failed to even acquire a result on a brand new lock, then there's definitely no reason to store it
await this.StoreOrDisposeLockAsync(connectionSource, @lock, shouldDispose: result?.CanSafelyDispose ?? true).ConfigureAwait(false);
}
return result.Value.Handle;
ValueTask<MultiplexedConnectionLock.Result> TryAcquireAsync(MultiplexedConnectionLock @lock, bool opportunistic) =>
@lock.TryAcquireAsync(name, timeout, strategy, keepaliveCadence, cancellationToken, opportunistic);
}
private async ValueTask<MultiplexedConnectionLock?> GetExistingLockOrDefaultAsync(TConnectionSource connectionSource)
{
using var _ = await this._lock.AcquireAsync(CancellationToken.None).ConfigureAwait(false);
if (this._poolsByConnectionSource.TryGetValue(connectionSource, out var pool) && pool.Count != 0)
{
--this._pooledLockCount;
return pool.Dequeue();
}
return null;
}
private async ValueTask StoreOrDisposeLockAsync(TConnectionSource connectionSource, MultiplexedConnectionLock @lock, bool shouldDispose)
{
if (shouldDispose)
{
try { await @lock.DisposeAsync().ConfigureAwait(false); }
catch { /* swallow */ }
}
using (await this._lock.AcquireAsync(CancellationToken.None).ConfigureAwait(false))
{
++this._storeCountSinceLastPrune;
if (shouldDispose)
{
// If we're about to dispose the lock, check if it has an empty pool that can be removed from our dictionary.
// By itself this doesn't guarantee cleanup: after a successful acquire we'll have an empty lock left over that won't
// go away unless we use THAT connection source again. To help with this, we have pruning
if (this._poolsByConnectionSource.TryGetValue(connectionSource, out var pool) && pool.Count == 0)
{
this._poolsByConnectionSource.Remove(connectionSource);
}
}
else // otherwise, store the lock
{
++this._pooledLockCount;
if (this._poolsByConnectionSource.TryGetValue(connectionSource, out var existing))
{
existing.Enqueue(@lock);
}
else
{
var newPool = new Queue<MultiplexedConnectionLock>();
newPool.Enqueue(@lock);
this._poolsByConnectionSource.Add(connectionSource, newPool);
}
}
if (this.IsDueForPruningNoLock())
{
await this.PrunePoolsNoLockAsync().ConfigureAwait(false);
}
}
}
private bool IsDueForPruningNoLock()
{
// Since pruning is expensive, we want to amortize its cost across many operations. The idea here is
// that each StoreOrDisposeLockAsync() call gives us one "ticket" that we can cache in later to justify
// some pruning work. The cost to prune is equal to the number of queues to scan plus the total number of
// items in each queue. Therefore we prune when we've built up enough tickets to "pay for" a pruning operation.
// The whole reason to prune is to avoid memory bloat (connection bloat isn't an issue since we only keep connections
// open when needed). So, we don't even consider pruning below a certain storage threshold
var pruningCost = this._pooledLockCount + this._poolsByConnectionSource.Count;
return pruningCost > 64 && this._storeCountSinceLastPrune >= pruningCost;
}
private async ValueTask PrunePoolsNoLockAsync()
{
this._storeCountSinceLastPrune = 0; // reset
List<TConnectionSource>? connectionSourcesToRemove = null;
foreach (var kvp in this._poolsByConnectionSource)
{
var pool = kvp.Value;
MultiplexedConnectionLock? firstRetainedLock = null;
while (pool.Count != 0 && pool.Peek() != firstRetainedLock)
{
var @lock = pool.Dequeue();
if (await @lock.GetIsInUseAsync().ConfigureAwait(false))
{
firstRetainedLock ??= @lock;
pool.Enqueue(@lock);
}
else
{
--this._pooledLockCount;
try { await @lock.DisposeAsync().ConfigureAwait(false); }
catch { /* swallow */ }
}
}
if (pool.Count == 0)
{
(connectionSourcesToRemove ??= new List<TConnectionSource>()).Add(kvp.Key);
}
}
if (connectionSourcesToRemove != null)
{
foreach (var connectionSourceToRemove in connectionSourcesToRemove)
{
this._poolsByConnectionSource.Remove(connectionSourceToRemove);
}
}
}
}