package quic
import (
"fmt"
"sync"
"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 connectionFlowController struct {
receiveFlowController
sendMutex sync .Mutex
bytesSent protocol .ByteCount
sendWindow protocol .ByteCount
lastBlockedAt protocol .ByteCount
}
func newConnectionFlowController(
receiveWindow protocol .ByteCount ,
maxReceiveWindow protocol .ByteCount ,
allowWindowIncrease func (size protocol .ByteCount ) bool ,
rttStats *utils .RTTStats ,
logger utils .Logger ,
) *connectionFlowController {
return &connectionFlowController {
receiveFlowController : receiveFlowController {
rttStats : rttStats ,
receiveWindow : receiveWindow ,
receiveWindowSize : receiveWindow ,
maxReceiveWindowSize : maxReceiveWindow ,
allowWindowIncrease : allowWindowIncrease ,
logger : logger ,
},
}
}
func (c *connectionFlowController ) IncrementHighestReceived (increment protocol .ByteCount , now monotime .Time ) error {
c .mutex .Lock ()
defer c .mutex .Unlock ()
if c .highestReceived == 0 {
c .startNewAutoTuningEpoch (now )
}
c .highestReceived += increment
if c .checkFlowControlViolation () {
return &qerr .TransportError {
ErrorCode : qerr .FlowControlError ,
ErrorMessage : fmt .Sprintf ("received %d bytes for the connection, allowed %d bytes" , c .highestReceived , c .receiveWindow ),
}
}
return nil
}
func (c *connectionFlowController ) AddBytesRead (n protocol .ByteCount ) (hasWindowUpdate bool ) {
c .mutex .Lock ()
defer c .mutex .Unlock ()
c .addBytesRead (n )
return c .hasWindowUpdate ()
}
func (c *connectionFlowController ) TryAddBytesSent (n protocol .ByteCount ) bool {
c .sendMutex .Lock ()
defer c .sendMutex .Unlock ()
if c .bytesSent > c .sendWindow || n > c .sendWindow -c .bytesSent {
return false
}
c .bytesSent += n
return true
}
func (c *connectionFlowController ) AddBytesSentWithLimiter (
n protocol .ByteCount ,
limiter func (int ) int ,
) (protocol .ByteCount , bool ) {
c .sendMutex .Lock ()
defer c .sendMutex .Unlock ()
if c .bytesSent >= c .sendWindow {
return 0 , false
}
n = min (n , c .sendWindow -c .bytesSent )
added := min (
max (protocol .ByteCount (limiter (int (n ))), 0 ),
n ,
)
c .bytesSent += added
return added , added < n
}
func (c *connectionFlowController ) UpdateSendWindow (offset protocol .ByteCount ) (updated bool ) {
c .sendMutex .Lock ()
defer c .sendMutex .Unlock ()
if offset > c .sendWindow {
c .sendWindow = offset
return true
}
return false
}
func (c *connectionFlowController ) SendWindowSize () protocol .ByteCount {
c .sendMutex .Lock ()
defer c .sendMutex .Unlock ()
return c .sendWindow - c .bytesSent
}
func (c *connectionFlowController ) IsNewlyBlocked () (bool , protocol .ByteCount ) {
c .sendMutex .Lock ()
defer c .sendMutex .Unlock ()
if c .bytesSent < c .sendWindow || c .sendWindow == c .lastBlockedAt {
return false , 0
}
c .lastBlockedAt = c .sendWindow
return true , c .sendWindow
}
func (c *connectionFlowController ) GetWindowUpdate (now monotime .Time ) protocol .ByteCount {
c .mutex .Lock ()
defer c .mutex .Unlock ()
oldWindowSize := c .receiveWindowSize
offset := c .getWindowUpdate (now )
if c .logger .Debug () && oldWindowSize < c .receiveWindowSize {
c .logger .Debugf ("Increasing receive flow control window for the connection to %d kB" , c .receiveWindowSize /(1 <<10 ))
}
return offset
}
func (c *connectionFlowController ) EnsureMinimumWindowSize (inc protocol .ByteCount , now monotime .Time ) {
c .mutex .Lock ()
defer c .mutex .Unlock ()
if inc <= c .receiveWindowSize {
return
}
newSize := min (inc , c .maxReceiveWindowSize )
if delta := newSize - c .receiveWindowSize ; delta > 0 && c .allowWindowIncrease (delta ) {
c .receiveWindowSize = newSize
if c .logger .Debug () {
c .logger .Debugf ("Increasing receive flow control window for the connection to %d, in response to stream flow control window increase" , newSize )
}
}
c .startNewAutoTuningEpoch (now )
}
func (c *connectionFlowController ) Reset () {
c .sendMutex .Lock ()
defer c .sendMutex .Unlock ()
c .bytesSent = 0
c .lastBlockedAt = 0
c .sendWindow = 0
}
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 .