forked from quic-go/quic-go
make the FlowController thread-safe
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package quic
|
package quic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/lucas-clemente/quic-go/handshake"
|
"github.com/lucas-clemente/quic-go/handshake"
|
||||||
"github.com/lucas-clemente/quic-go/protocol"
|
"github.com/lucas-clemente/quic-go/protocol"
|
||||||
)
|
)
|
||||||
@@ -16,6 +18,8 @@ type flowController struct {
|
|||||||
receiveWindowUpdateThreshold protocol.ByteCount
|
receiveWindowUpdateThreshold protocol.ByteCount
|
||||||
receiveFlowControlWindow protocol.ByteCount
|
receiveFlowControlWindow protocol.ByteCount
|
||||||
receiveFlowControlWindowIncrement protocol.ByteCount
|
receiveFlowControlWindowIncrement protocol.ByteCount
|
||||||
|
|
||||||
|
mutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFlowController(connectionParametersManager *handshake.ConnectionParametersManager) *flowController {
|
func newFlowController(connectionParametersManager *handshake.ConnectionParametersManager) *flowController {
|
||||||
@@ -28,12 +32,18 @@ func newFlowController(connectionParametersManager *handshake.ConnectionParamete
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *flowController) AddBytesSent(n protocol.ByteCount) {
|
func (c *flowController) AddBytesSent(n protocol.ByteCount) {
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
c.bytesSent += n
|
c.bytesSent += n
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateSendWindow should be called after receiving a WindowUpdateFrame
|
// UpdateSendWindow should be called after receiving a WindowUpdateFrame
|
||||||
// it returns true if the window was actually updated
|
// it returns true if the window was actually updated
|
||||||
func (c *flowController) UpdateSendWindow(newOffset protocol.ByteCount) bool {
|
func (c *flowController) UpdateSendWindow(newOffset protocol.ByteCount) bool {
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
if newOffset > c.sendFlowControlWindow {
|
if newOffset > c.sendFlowControlWindow {
|
||||||
c.sendFlowControlWindow = newOffset
|
c.sendFlowControlWindow = newOffset
|
||||||
return true
|
return true
|
||||||
@@ -42,6 +52,9 @@ func (c *flowController) UpdateSendWindow(newOffset protocol.ByteCount) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *flowController) SendWindowSize() protocol.ByteCount {
|
func (c *flowController) SendWindowSize() protocol.ByteCount {
|
||||||
|
c.mutex.RLock()
|
||||||
|
defer c.mutex.RUnlock()
|
||||||
|
|
||||||
if c.bytesSent > c.sendFlowControlWindow { // should never happen, but make sure we don't do an underflow here
|
if c.bytesSent > c.sendFlowControlWindow { // should never happen, but make sure we don't do an underflow here
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@@ -49,6 +62,9 @@ func (c *flowController) SendWindowSize() protocol.ByteCount {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *flowController) AddBytesRead(n protocol.ByteCount) {
|
func (c *flowController) AddBytesRead(n protocol.ByteCount) {
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
c.bytesRead += n
|
c.bytesRead += n
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,6 +75,9 @@ func (c *flowController) MaybeTriggerBlocked() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
if c.lastBlockedSentForOffset == c.sendFlowControlWindow {
|
if c.lastBlockedSentForOffset == c.sendFlowControlWindow {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -70,6 +89,9 @@ func (c *flowController) MaybeTriggerBlocked() bool {
|
|||||||
// MaybeTriggerWindowUpdate determines if it is necessary to send a WindowUpdate
|
// MaybeTriggerWindowUpdate determines if it is necessary to send a WindowUpdate
|
||||||
// if so, it returns true and the offset of the window
|
// if so, it returns true and the offset of the window
|
||||||
func (c *flowController) MaybeTriggerWindowUpdate() (bool, protocol.ByteCount) {
|
func (c *flowController) MaybeTriggerWindowUpdate() (bool, protocol.ByteCount) {
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
diff := c.receiveFlowControlWindow - c.bytesRead
|
diff := c.receiveFlowControlWindow - c.bytesRead
|
||||||
if diff < c.receiveWindowUpdateThreshold {
|
if diff < c.receiveWindowUpdateThreshold {
|
||||||
c.receiveFlowControlWindow += c.receiveFlowControlWindowIncrement
|
c.receiveFlowControlWindow += c.receiveFlowControlWindowIncrement
|
||||||
@@ -79,6 +101,9 @@ func (c *flowController) MaybeTriggerWindowUpdate() (bool, protocol.ByteCount) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *flowController) CheckFlowControlViolation(highestByte protocol.ByteCount) bool {
|
func (c *flowController) CheckFlowControlViolation(highestByte protocol.ByteCount) bool {
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
if highestByte > c.receiveFlowControlWindow {
|
if highestByte > c.receiveFlowControlWindow {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -290,20 +290,18 @@ func (s *Session) handleWindowUpdateFrame(frame *frames.WindowUpdateFrame) error
|
|||||||
// return errors.New("Connection level flow control not yet implemented")
|
// return errors.New("Connection level flow control not yet implemented")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
s.streamsMutex.RLock()
|
s.streamsMutex.RLock()
|
||||||
defer s.streamsMutex.RUnlock()
|
|
||||||
|
|
||||||
stream, streamExists := s.streams[frame.StreamID]
|
stream, streamExists := s.streams[frame.StreamID]
|
||||||
|
|
||||||
if !streamExists {
|
if !streamExists {
|
||||||
return errWindowUpdateOnInvalidStream
|
return errWindowUpdateOnInvalidStream
|
||||||
}
|
}
|
||||||
if stream == nil {
|
if stream == nil {
|
||||||
return errWindowUpdateOnClosedStream
|
return errWindowUpdateOnClosedStream
|
||||||
}
|
}
|
||||||
|
s.streamsMutex.RUnlock()
|
||||||
|
|
||||||
stream.UpdateSendFlowControlWindow(frame.ByteOffset)
|
stream.UpdateSendFlowControlWindow(frame.ByteOffset)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -140,9 +140,6 @@ func (s *stream) ReadByte() (byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *stream) UpdateSendFlowControlWindow(n protocol.ByteCount) {
|
func (s *stream) UpdateSendFlowControlWindow(n protocol.ByteCount) {
|
||||||
s.mutex.Lock()
|
|
||||||
defer s.mutex.Unlock()
|
|
||||||
|
|
||||||
if s.flowController.UpdateSendWindow(n) {
|
if s.flowController.UpdateSendWindow(n) {
|
||||||
s.windowUpdateOrErrCond.Broadcast()
|
s.windowUpdateOrErrCond.Broadcast()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user