public int AdvanceUpToAndWait(long bytes, out OutputFlowControlAwaitable awaitable)
        {
            var leastAvailableFlow = _connectionLevelFlowControl.Available < _streamLevelFlowControl.Available
                ? _connectionLevelFlowControl : _streamLevelFlowControl;

            // Clamp ~= Math.Clamp from netcoreapp >= 2.0
            var actual = Clamp(leastAvailableFlow.Available, 0, bytes);

            // Make sure to advance prior to accessing AvailabilityAwaitable.
            _connectionLevelFlowControl.Advance(actual);
            _streamLevelFlowControl.Advance(actual);

            awaitable = null;
            _currentConnectionLevelAwaitable = null;

            if (actual < bytes)
            {
                awaitable = leastAvailableFlow.AvailabilityAwaitable;

                if (leastAvailableFlow == _connectionLevelFlowControl)
                {
                    _currentConnectionLevelAwaitable = awaitable;
                }
            }

            return(actual);
        }
 public void Reset(uint initialWindowSize)
 {
     _streamLevelFlowControl.Reset(initialWindowSize);
     if (_currentConnectionLevelAwaitable != null)
     {
         Debug.Assert(_currentConnectionLevelAwaitable.IsCompleted, "Should have been completed by the previous stream.");
         _currentConnectionLevelAwaitable = null;
     }
 }