protected internal override async ValueTask DisposeAsync(int version, CancellationToken cancellationToken) { if (_owningConnection is PooledHttpConnection owningConnection) { ThrowIfDisposed(version); PooledHttp1Connection pooledConnection = (PooledHttp1Connection)((Http1Request)_request.Request).Connection; bool disposeConnection = false; try { await _request.DrainAsync(owningConnection.MaximumDrainBytes, cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { disposeConnection = true; } await _request.DisposeAsync(cancellationToken).ConfigureAwait(false); _owningConnection = null; owningConnection.PushCachedRequest(this); if (!disposeConnection && !pooledConnection.IsExpired(Environment.TickCount64, owningConnection.PooledConnectionLifetimeLimit, owningConnection.PooledConnectionIdleLimit)) { owningConnection.PushHttp1Connection(pooledConnection); } else { await pooledConnection.DisposeAsync(cancellationToken).ConfigureAwait(false); } } }
protected internal override async ValueTask DisposeAsync(int version, CancellationToken cancellationToken) { if (_owningConnection is PooledHttpConnection owningConnection) { ThrowIfDisposed(version); PooledHttp1Connection pooledConnection = (PooledHttp1Connection)((Http1Request)_request.Request).Connection; bool disposeConnection = false; try { await _request.DrainAsync(owningConnection.MaximumDrainBytes, cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { disposeConnection = true; } await _request.DisposeAsync(cancellationToken).ConfigureAwait(false); _owningConnection = null; bool owningConnectionDisposed = owningConnection._disposed; disposeConnection = disposeConnection || owningConnectionDisposed || pooledConnection.Status != HttpConnectionStatus.Open || pooledConnection.IsExpired(Environment.TickCount64, owningConnection.PooledConnectionLifetimeLimit, owningConnection.PooledConnectionIdleLimit); if (!owningConnectionDisposed) { owningConnection.PushCachedRequestToTail(this); } if (!disposeConnection) { owningConnection.PushHttp1ConnectionToTail(pooledConnection); if (owningConnection._disposed) { // If the owning connection was disposed between last checking, the connection // will be pushed to freelist with noone else to dispose it. // Re-dispose the owning connection to clear out the freelist. await owningConnection.DisposeAsync(cancellationToken).ConfigureAwait(false); } } else { await pooledConnection.DisposeAsync(cancellationToken).ConfigureAwait(false); } } }