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(); } }
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 } } } } }