private ValueTask <HttpConnection> GetOrReserveHttpConnectionAsync(CancellationToken cancellationToken) { CachedConnection cachedConnection = null; TaskCompletionSourceWithCancellation <HttpConnection> waiter = default(TaskCompletionSourceWithCancellation <HttpConnection>); lock (SyncObj) { var list = _idleConnections; if (list.Count > 0) { cachedConnection = list[list.Count - 1]; list.RemoveAt(list.Count - 1); } else { if (_associatedConnectionCount < _maxConnections) { IncrementConnectionCountNoLock(); return(new ValueTask <HttpConnection>((HttpConnection)null)); } else { waiter = EnqueueWaiter(cancellationToken); } } } if (cachedConnection != null) { return(new ValueTask <HttpConnection>(cachedConnection._connection)); } else { //wait for connection return to pool return(new ValueTask <HttpConnection>(waiter.WaitWithCancellationAsync(cancellationToken))); } }
public bool Equals(CachedConnection other) => ReferenceEquals(other._connection, _connection);