package quic
import (
"fmt"
"github.com/tmc/go-iroh/internal/qng/internal/monotime"
"github.com/tmc/go-iroh/internal/qng/internal/protocol"
"github.com/tmc/go-iroh/internal/qng/internal/qerr"
"github.com/tmc/go-iroh/internal/qng/internal/utils"
)
type streamFlowController struct {
receiveFlowController
bytesSent protocol .ByteCount
sendWindow protocol .ByteCount
lastBlockedAt protocol .ByteCount
streamID protocol .StreamID
connection *connectionFlowController
receivedFinalOffset bool
}
func newStreamFlowController(
streamID protocol .StreamID ,
cfc *connectionFlowController ,
receiveWindow protocol .ByteCount ,
maxReceiveWindow protocol .ByteCount ,
initialSendWindow protocol .ByteCount ,
rttStats *utils .RTTStats ,
logger utils .Logger ,
) *streamFlowController {
return &streamFlowController {
streamID : streamID ,
connection : cfc ,
sendWindow : initialSendWindow ,
receiveFlowController : receiveFlowController {
rttStats : rttStats ,
receiveWindow : receiveWindow ,
receiveWindowSize : receiveWindow ,
maxReceiveWindowSize : maxReceiveWindow ,
logger : logger ,
},
}
}
func (c *streamFlowController ) UpdateHighestReceived (offset protocol .ByteCount , final bool , now monotime .Time ) error {
if c .receivedFinalOffset {
if final && offset != c .highestReceived {
return &qerr .TransportError {
ErrorCode : qerr .FinalSizeError ,
ErrorMessage : fmt .Sprintf ("received inconsistent final offset for stream %d (old: %d, new: %d bytes)" , c .streamID , c .highestReceived , offset ),
}
}
if offset > c .highestReceived {
return &qerr .TransportError {
ErrorCode : qerr .FinalSizeError ,
ErrorMessage : fmt .Sprintf ("received offset %d for stream %d, but final offset was already received at %d" , offset , c .streamID , c .highestReceived ),
}
}
}
if final {
c .receivedFinalOffset = true
}
if offset == c .highestReceived {
return nil
}
if offset < c .highestReceived {
if final {
return &qerr .TransportError {
ErrorCode : qerr .FinalSizeError ,
ErrorMessage : fmt .Sprintf ("received final offset %d for stream %d, but already received offset %d before" , offset , c .streamID , c .highestReceived ),
}
}
return nil
}
if c .highestReceived == 0 {
c .startNewAutoTuningEpoch (now )
}
increment := offset - c .highestReceived
c .highestReceived = offset
if c .checkFlowControlViolation () {
return &qerr .TransportError {
ErrorCode : qerr .FlowControlError ,
ErrorMessage : fmt .Sprintf ("received %d bytes on stream %d, allowed %d bytes" , offset , c .streamID , c .receiveWindow ),
}
}
return c .connection .IncrementHighestReceived (increment , now )
}
func (c *streamFlowController ) AddBytesRead (n protocol .ByteCount ) (hasStreamWindowUpdate , hasConnWindowUpdate bool ) {
c .mutex .Lock ()
c .addBytesRead (n )
hasStreamWindowUpdate = c .shouldQueueWindowUpdate ()
c .mutex .Unlock ()
hasConnWindowUpdate = c .connection .AddBytesRead (n )
return
}
func (c *streamFlowController ) Abandon () {
c .mutex .Lock ()
unread := c .highestReceived - c .bytesRead
c .bytesRead = c .highestReceived
c .mutex .Unlock ()
if unread > 0 {
c .connection .AddBytesRead (unread )
}
}
func (c *streamFlowController ) UpdateSendWindow (offset protocol .ByteCount ) (updated bool ) {
if offset > c .sendWindow {
c .sendWindow = offset
return true
}
return false
}
func (c *streamFlowController ) TryAddBytesSent (n protocol .ByteCount ) bool {
if c .bytesSent > c .sendWindow || n > c .sendWindow -c .bytesSent {
return false
}
if !c .connection .TryAddBytesSent (n ) {
return false
}
c .bytesSent += n
return true
}
func (c *streamFlowController ) AddBytesSentWithLimiter (
n protocol .ByteCount ,
limiter func (int ) int ,
) (protocol .ByteCount , bool ) {
if c .bytesSent >= c .sendWindow {
return 0 , false
}
n = min (n , c .sendWindow -c .bytesSent )
added , limited := c .connection .AddBytesSentWithLimiter (n , limiter )
c .bytesSent += added
return added , limited
}
func (c *streamFlowController ) SendWindowSize () protocol .ByteCount {
return min (c .sendWindow -c .bytesSent , c .connection .SendWindowSize ())
}
func (c *streamFlowController ) IsNewlyBlocked () bool {
blocked , _ := c .isNewlyBlocked ()
return blocked
}
func (c *streamFlowController ) isNewlyBlocked () (bool , protocol .ByteCount ) {
if c .bytesSent < c .sendWindow || c .sendWindow == c .lastBlockedAt {
return false , 0
}
c .lastBlockedAt = c .sendWindow
return true , c .sendWindow
}
func (c *streamFlowController ) shouldQueueWindowUpdate () bool {
return !c .receivedFinalOffset && c .hasWindowUpdate ()
}
func (c *streamFlowController ) GetWindowUpdate (now monotime .Time ) protocol .ByteCount {
if c .receivedFinalOffset {
return 0
}
c .mutex .Lock ()
defer c .mutex .Unlock ()
oldWindowSize := c .receiveWindowSize
offset := c .getWindowUpdate (now )
if c .receiveWindowSize > oldWindowSize {
c .logger .Debugf ("Increasing receive flow control window for stream %d to %d" , c .streamID , c .receiveWindowSize )
c .connection .EnsureMinimumWindowSize (
protocol .ByteCount (float64 (c .receiveWindowSize )*protocol .ConnectionFlowControlMultiplier ),
now ,
)
}
return offset
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .