public async Task <IConnectionHandle> AcquireConnectionAsync(CancellationToken cancellationToken) { try { StartCheckingOut(); var stopwatch = Stopwatch.StartNew(); _poolQueueWaitResult = await _pool._maxConnectionsQueue.WaitSignaledAsync(_timeout, cancellationToken).ConfigureAwait(false); if (_poolQueueWaitResult == SemaphoreSlimSignalable.SemaphoreWaitResult.Entered) { PooledConnection pooledConnection = null; var timeout = EnsureTimeout(stopwatch); using (var connectionCreator = new ConnectionCreator(_pool, timeout)) { pooledConnection = await connectionCreator.CreateOpenedOrReuseAsync(cancellationToken).ConfigureAwait(false); } return(EndCheckingOut(pooledConnection, stopwatch)); } stopwatch.Stop(); throw CreateException(stopwatch); } catch (Exception ex) { HandleException(ex); throw; } }
private async Task EnsureMinSizeAsync(CancellationToken cancellationToken) { var minTimeout = TimeSpan.FromMilliseconds(20); while (CreatedCount < _settings.MinConnections && !cancellationToken.IsCancellationRequested) { using (var poolAwaiter = _maxConnectionsQueue.CreateAwaiter()) { var entered = await poolAwaiter.WaitSignaledAsync(minTimeout, cancellationToken).ConfigureAwait(false); if (!entered) { return; } using (var connectionCreator = new ConnectionCreator(this, minTimeout)) { var connection = await connectionCreator.CreateOpenedAsync(cancellationToken).ConfigureAwait(false); _connectionHolder.Return(connection); } } cancellationToken.ThrowIfCancellationRequested(); } }
public async Task <IConnectionHandle> EnteredPoolAsync(bool enteredPool, CancellationToken cancellationToken) { _enteredPool = enteredPool; PooledConnection connection = null; if (enteredPool) { var timeSpentInWaitQueue = _stopwatch.Elapsed; using (var connectionCreator = new ConnectionCreator(_pool, _pool._settings.WaitQueueTimeout - timeSpentInWaitQueue)) { connection = await connectionCreator.CreateOpenedOrReuseAsync(cancellationToken).ConfigureAwait(false); } } return(FinalizePoolEnterance(connection)); }
public IConnectionHandle EnteredPool(bool enteredPool, CancellationToken cancellationToken) { _enteredPool = enteredPool; PooledConnection connection = null; if (enteredPool) { var timeSpentInWaitQueue = _stopwatch.Elapsed; using (var connectionCreator = new ConnectionCreator(_pool, _pool._settings.WaitQueueTimeout - timeSpentInWaitQueue)) { connection = connectionCreator.CreateOpenedOrReuse(cancellationToken); } } return(FinalizePoolEnterance(connection)); }
private async Task EnsureMinSizeAsync(CancellationToken cancellationToken) { var minTimeout = TimeSpan.FromMilliseconds(20); while (CreatedCount < _settings.MinConnections) { bool enteredPool = false; try { enteredPool = await _poolQueue.WaitAsync(minTimeout, cancellationToken).ConfigureAwait(false); if (!enteredPool) { return; } using (var connectionCreator = new ConnectionCreator(this, minTimeout)) { var connection = await connectionCreator.CreateOpenedAsync(cancellationToken).ConfigureAwait(false); _connectionHolder.Return(connection); } } finally { if (enteredPool) { try { _poolQueue.Release(); } catch { // log this... it's a bug } } } } }
private void EnsureMinSize(CancellationToken cancellationToken) { var minTimeout = TimeSpan.FromMilliseconds(20); while (_connectionPool.CreatedCount < _connectionPool.Settings.MinConnections && !cancellationToken.IsCancellationRequested) { using (var poolAwaiter = _connectionPool.CreateMaxConnectionsAwaiter()) { var entered = poolAwaiter.WaitSignaled(minTimeout, cancellationToken); if (!entered) { return; } using (var connectionCreator = new ConnectionCreator(_connectionPool, minTimeout)) { var connection = connectionCreator.CreateOpened(cancellationToken); _connectionPool.ConnectionHolder.Return(connection); } } cancellationToken.ThrowIfCancellationRequested(); } }