private async Task PrunePoolAsync(CancellationToken cancellationToken) { bool enteredPool = false; try { // if it takes too long to enter the pool, then the pool is fully utilized // and we don't want to mess with it. enteredPool = await _poolQueue.WaitAsync(TimeSpan.FromMilliseconds(20), cancellationToken).ConfigureAwait(false); if (!enteredPool) { return; } _connectionHolder.Prune(); } finally { if (enteredPool) { try { _poolQueue.Release(); } catch { // log this... it's a bug } } } }
// private methods private void MaintainSize(CancellationToken cancellationToken) { try { while (!cancellationToken.IsCancellationRequested) { try { _connectionHolder.Prune(cancellationToken); EnsureMinSize(cancellationToken); } catch { // ignore exceptions } ThreadHelper.Sleep(_settings.MaintenanceInterval, cancellationToken); } } catch { // ignore exceptions } }
// private methods private async Task MaintainSizeAsync(CancellationToken cancellationToken) { try { while (!cancellationToken.IsCancellationRequested) { try { _connectionHolder.Prune(cancellationToken); await EnsureMinSizeAsync(cancellationToken).ConfigureAwait(false); } catch { // ignore exceptions } await Task.Delay(_settings.MaintenanceInterval, cancellationToken).ConfigureAwait(false); } } catch { // ignore exceptions } }