Skip to content

Commit da1d212

Browse files
author
Oleksandr Poliakov
committed
pr
1 parent fa4b6d3 commit da1d212

File tree

5 files changed

+32
-30
lines changed

5 files changed

+32
-30
lines changed

src/MongoDB.Driver/Core/Clusters/Cluster.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,21 @@ public IServer SelectServer(IServerSelector selector, OperationContext operation
222222
Ensure.IsNotNull(selector, nameof(selector));
223223
Ensure.IsNotNull(operationContext, nameof(operationContext));
224224

225-
var serverSelectionCancellationContext = operationContext.WithTimeout(Settings.ServerSelectionTimeout);
225+
var serverSelectionOperationContext = operationContext.WithTimeout(Settings.ServerSelectionTimeout);
226226

227227
using (var helper = new SelectServerHelper(this, selector))
228228
{
229229
try
230230
{
231231
while (true)
232232
{
233-
var server = helper.SelectServer(serverSelectionCancellationContext);
233+
var server = helper.SelectServer(serverSelectionOperationContext);
234234
if (server != null)
235235
{
236236
return server;
237237
}
238238

239-
helper.WaitForDescriptionChanged(serverSelectionCancellationContext);
239+
helper.WaitForDescriptionChanged(serverSelectionOperationContext);
240240
}
241241
}
242242
catch (TimeoutException)
@@ -261,21 +261,21 @@ public async Task<IServer> SelectServerAsync(IServerSelector selector, Operation
261261
Ensure.IsNotNull(selector, nameof(selector));
262262
Ensure.IsNotNull(operationContext, nameof(operationContext));
263263

264-
var serverSelectionCancellationContext = operationContext.WithTimeout(Settings.ServerSelectionTimeout);
264+
var serverSelectionOperationContext = operationContext.WithTimeout(Settings.ServerSelectionTimeout);
265265

266266
using (var helper = new SelectServerHelper(this, selector))
267267
{
268268
try
269269
{
270270
while (true)
271271
{
272-
var server = helper.SelectServer(serverSelectionCancellationContext);
272+
var server = helper.SelectServer(serverSelectionOperationContext);
273273
if (server != null)
274274
{
275275
return server;
276276
}
277277

278-
await helper.WaitForDescriptionChangedAsync(serverSelectionCancellationContext).ConfigureAwait(false);
278+
await helper.WaitForDescriptionChangedAsync(serverSelectionOperationContext).ConfigureAwait(false);
279279
}
280280
}
281281
catch (TimeoutException)

src/MongoDB.Driver/Core/Clusters/LoadBalancedCluster.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public IServer SelectServer(IServerSelector selector, OperationContext operation
176176
Ensure.IsNotNull(operationContext, nameof(operationContext));
177177
ThrowIfDisposed();
178178

179-
var serverSelectionCancellationContext = operationContext.WithTimeout(_settings.ServerSelectionTimeout);
179+
var serverSelectionOperationContext = operationContext.WithTimeout(_settings.ServerSelectionTimeout);
180180

181181
_serverSelectionEventLogger.LogAndPublish(new ClusterSelectingServerEvent(
182182
_description,
@@ -186,7 +186,7 @@ public IServer SelectServer(IServerSelector selector, OperationContext operation
186186

187187
try
188188
{
189-
serverSelectionCancellationContext.WaitTask(_serverReadyTaskCompletionSource.Task);
189+
serverSelectionOperationContext.WaitTask(_serverReadyTaskCompletionSource.Task);
190190
}
191191
catch (TimeoutException)
192192
{
@@ -199,7 +199,7 @@ public IServer SelectServer(IServerSelector selector, OperationContext operation
199199
_description,
200200
selector,
201201
_server.Description,
202-
serverSelectionCancellationContext.Elapsed,
202+
serverSelectionOperationContext.Elapsed,
203203
null,
204204
EventContext.OperationName));
205205
}
@@ -214,7 +214,7 @@ public async Task<IServer> SelectServerAsync(IServerSelector selector, Operation
214214
Ensure.IsNotNull(operationContext, nameof(operationContext));
215215
ThrowIfDisposed();
216216

217-
var serverSelectionCancellationContext = operationContext.WithTimeout(_settings.ServerSelectionTimeout);
217+
var serverSelectionOperationContext = operationContext.WithTimeout(_settings.ServerSelectionTimeout);
218218

219219
_serverSelectionEventLogger.LogAndPublish(new ClusterSelectingServerEvent(
220220
_description,
@@ -224,7 +224,7 @@ public async Task<IServer> SelectServerAsync(IServerSelector selector, Operation
224224

225225
try
226226
{
227-
await serverSelectionCancellationContext.WaitTaskAsync(_serverReadyTaskCompletionSource.Task).ConfigureAwait(false);
227+
await serverSelectionOperationContext.WaitTaskAsync(_serverReadyTaskCompletionSource.Task).ConfigureAwait(false);
228228
}
229229
catch (TimeoutException)
230230
{
@@ -237,7 +237,7 @@ public async Task<IServer> SelectServerAsync(IServerSelector selector, Operation
237237
_description,
238238
selector,
239239
_server.Description,
240-
serverSelectionCancellationContext.Elapsed,
240+
serverSelectionOperationContext.Elapsed,
241241
null,
242242
EventContext.OperationName));
243243
}

src/MongoDB.Driver/Core/ConnectionPools/ExclusiveConnectionPool.Helpers.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace MongoDB.Driver.Core.ConnectionPools
3333
internal sealed partial class ExclusiveConnectionPool
3434
{
3535
// private methods
36-
private Exception CreateTimeoutException(OperationContext operationContext, string message)
36+
private Exception CreateTimeoutException(TimeSpan elapsed, string message)
3737
{
3838
var checkOutsForCursorCount = _checkOutReasonCounter.GetCheckOutsCount(CheckOutReason.Cursor);
3939
var checkOutsForTransactionCount = _checkOutReasonCounter.GetCheckOutsCount(CheckOutReason.Transaction);
@@ -47,7 +47,7 @@ private Exception CreateTimeoutException(OperationContext operationContext, stri
4747
var checkOutsForOtherCount = checkOutsCount - checkOutsForCursorCount - checkOutsForTransactionCount;
4848

4949
message =
50-
$"Timed out after {operationContext.Elapsed.TotalMilliseconds}ms waiting for a connection from the connection pool. " +
50+
$"Timed out after {elapsed.TotalMilliseconds}ms waiting for a connection from the connection pool. " +
5151
$"maxPoolSize: {maxPoolSize}, " +
5252
$"connections in use by cursors: {checkOutsForCursorCount}, " +
5353
$"connections in use by transactions: {checkOutsForTransactionCount}, " +
@@ -296,9 +296,9 @@ private IConnectionHandle EndCheckingOut(PooledConnection pooledConnection, Oper
296296

297297
private void EnsureTimeout(OperationContext operationContext)
298298
{
299-
if (operationContext.IsTimedOut())
299+
if (operationContext.IsTimedOut(out var elapsed))
300300
{
301-
throw _pool.CreateTimeoutException(operationContext, $"Timed out waiting for a connection after {operationContext.Elapsed.TotalMilliseconds}ms.");
301+
throw _pool.CreateTimeoutException(elapsed, $"Timed out waiting for a connection after {elapsed.TotalMilliseconds}ms.");
302302
}
303303
}
304304

@@ -308,7 +308,7 @@ private Exception CreateException(OperationContext operationContext) =>
308308
SemaphoreSlimSignalable.SemaphoreWaitResult.Signaled =>
309309
MongoConnectionPoolPausedException.ForConnectionPool(_pool._endPoint),
310310
SemaphoreSlimSignalable.SemaphoreWaitResult.TimedOut =>
311-
_pool.CreateTimeoutException(operationContext, $"Timed out waiting for a connection after {operationContext.Elapsed.TotalMilliseconds}ms."),
311+
_pool.CreateTimeoutException(operationContext.Elapsed, $"Timed out waiting for a connection after {operationContext.Elapsed.TotalMilliseconds}ms."),
312312
// should not be reached
313313
_ => new InvalidOperationException($"Invalid {_poolQueueWaitResult}.")
314314
};
@@ -853,7 +853,7 @@ public PooledConnection CreateOpened(OperationContext operationContext)
853853

854854
if (_connectingWaitStatus == SemaphoreSlimSignalable.SemaphoreWaitResult.TimedOut)
855855
{
856-
_pool.CreateTimeoutException(operationContext, $"Timed out waiting for in connecting queue after {operationContext.Elapsed.TotalMilliseconds}ms.");
856+
_pool.CreateTimeoutException(operationContext.Elapsed, $"Timed out waiting for in connecting queue after {operationContext.Elapsed.TotalMilliseconds}ms.");
857857
}
858858

859859
return CreateOpenedInternal(operationContext);
@@ -885,13 +885,13 @@ public PooledConnection CreateOpenedOrReuse(OperationContext operationContext)
885885
{
886886
SemaphoreSlimSignalable.SemaphoreWaitResult.Signaled => _pool._connectionHolder.Acquire(),
887887
SemaphoreSlimSignalable.SemaphoreWaitResult.Entered => CreateOpenedInternal(operationContext),
888-
SemaphoreSlimSignalable.SemaphoreWaitResult.TimedOut => throw CreateTimeoutException(operationContext),
888+
SemaphoreSlimSignalable.SemaphoreWaitResult.TimedOut => throw CreateTimeoutException(operationContext.Elapsed),
889889
_ => throw new InvalidOperationException($"Invalid wait result {_connectingWaitStatus}")
890890
};
891891

892-
if (connection == null && operationContext.IsTimedOut())
892+
if (connection == null && operationContext.IsTimedOut(out var elapsed))
893893
{
894-
throw CreateTimeoutException(operationContext);
894+
throw CreateTimeoutException(elapsed);
895895
}
896896
}
897897

@@ -924,13 +924,13 @@ public async Task<PooledConnection> CreateOpenedOrReuseAsync(OperationContext op
924924
{
925925
SemaphoreSlimSignalable.SemaphoreWaitResult.Signaled => _pool._connectionHolder.Acquire(),
926926
SemaphoreSlimSignalable.SemaphoreWaitResult.Entered => await CreateOpenedInternalAsync(operationContext).ConfigureAwait(false),
927-
SemaphoreSlimSignalable.SemaphoreWaitResult.TimedOut => throw CreateTimeoutException(operationContext),
927+
SemaphoreSlimSignalable.SemaphoreWaitResult.TimedOut => throw CreateTimeoutException(operationContext.Elapsed),
928928
_ => throw new InvalidOperationException($"Invalid wait result {_connectingWaitStatus}")
929929
};
930930

931-
if (connection == null && operationContext.IsTimedOut())
931+
if (connection == null && operationContext.IsTimedOut(out var elapsed))
932932
{
933-
throw CreateTimeoutException(operationContext);
933+
throw CreateTimeoutException(elapsed);
934934
}
935935
}
936936

@@ -987,7 +987,7 @@ private void StartCreating(OperationContext operationContext)
987987
_pool._eventLogger.LogAndPublish(new ConnectionPoolAddingConnectionEvent(_pool._serverId, EventContext.OperationId));
988988

989989
operationContext.CancellationToken.ThrowIfCancellationRequested();
990-
if (operationContext.IsTimedOut())
990+
if (operationContext.IsTimedOut(out _))
991991
{
992992
throw new TimeoutException();
993993
}
@@ -1004,10 +1004,10 @@ private void FinishCreating(ConnectionDescription description, OperationContext
10041004
_pool._serviceStates.IncrementConnectionCount(description?.ServiceId);
10051005
}
10061006

1007-
private Exception CreateTimeoutException(OperationContext operationContext)
1007+
private Exception CreateTimeoutException(TimeSpan elapsed)
10081008
{
1009-
var message = $"Timed out waiting in connecting queue after {operationContext.Elapsed.TotalMilliseconds}ms.";
1010-
return _pool.CreateTimeoutException(operationContext, message);
1009+
var message = $"Timed out waiting in connecting queue after {elapsed.TotalMilliseconds}ms.";
1010+
return _pool.CreateTimeoutException(elapsed, message);
10111011
}
10121012
}
10131013
}

src/MongoDB.Driver/OperationContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ public TimeSpan RemainingTimeout
6363

6464
private Stopwatch Stopwatch { get; }
6565

66-
public bool IsTimedOut()
66+
public bool IsTimedOut(out TimeSpan elapsed)
6767
{
68+
elapsed = Elapsed;
6869
var remainingTimeout = RemainingTimeout;
6970
if (remainingTimeout == System.Threading.Timeout.InfiniteTimeSpan)
7071
{

tests/MongoDB.Driver.Tests/OperationContextTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ public void IsTimedOut_should_return_expected_result(bool expected, TimeSpan tim
9999
stopwatch.Stop();
100100

101101
var operationContext = new OperationContext(stopwatch, timeout, CancellationToken.None);
102-
var result = operationContext.IsTimedOut();
102+
var result = operationContext.IsTimedOut(out var elapsed);
103103

104104
result.Should().Be(expected);
105+
elapsed.Should().Be(stopwatch.Elapsed);
105106
}
106107

107108
public static IEnumerable<object[]> IsTimedOut_test_cases =

0 commit comments

Comments
 (0)