public async Task <PooledConnection> CreateOpenedAsync(CancellationToken cancellationToken) { try { var stopwatch = Stopwatch.StartNew(); _connectingWaitStatus = await _pool._maxConnectingQueue.WaitAsync(_connectingTimeout, cancellationToken).ConfigureAwait(false); stopwatch.Stop(); _pool._poolState.ThrowIfNotReady(); if (_connectingWaitStatus == SemaphoreSlimSignalable.SemaphoreWaitResult.TimedOut) { _pool.CreateTimeoutException(stopwatch, $"Timed out waiting for in connecting queue after {stopwatch.ElapsedMilliseconds}ms."); } var connection = await CreateOpenedInternalAsync(cancellationToken).ConfigureAwait(false); return(connection); } catch (Exception ex) { _pool._connectionExceptionHandler.HandleExceptionOnOpen(ex); throw; } }
private TimeSpan EnsureTimeout(Stopwatch stopwatch) { var timeSpentInWaitQueue = stopwatch.Elapsed; var timeout = _timeout - timeSpentInWaitQueue; if (timeout < TimeSpan.Zero) { throw _pool.CreateTimeoutException(stopwatch, $"Timed out waiting for a connection after {timeSpentInWaitQueue.TotalMilliseconds}ms."); } return(timeout); }