keep track of highest byte received in FlowController

ref #39
This commit is contained in:
Marten Seemann
2016-05-18 09:32:36 +07:00
parent d6ef71a54c
commit daeb601cdc
3 changed files with 30 additions and 5 deletions

View File

@@ -15,6 +15,7 @@ type flowController struct {
lastBlockedSentForOffset protocol.ByteCount
bytesRead protocol.ByteCount
highestReceived protocol.ByteCount
receiveWindowUpdateThreshold protocol.ByteCount
receiveFlowControlWindow protocol.ByteCount
receiveFlowControlWindowIncrement protocol.ByteCount
@@ -61,6 +62,15 @@ func (c *flowController) SendWindowSize() protocol.ByteCount {
return c.sendFlowControlWindow - c.bytesSent
}
func (c *flowController) UpdateHighestReceived(n protocol.ByteCount) {
c.mutex.Lock()
defer c.mutex.Unlock()
if n > c.highestReceived {
c.highestReceived = n
}
}
func (c *flowController) AddBytesRead(n protocol.ByteCount) {
c.mutex.Lock()
defer c.mutex.Unlock()
@@ -100,11 +110,11 @@ func (c *flowController) MaybeTriggerWindowUpdate() (bool, protocol.ByteCount) {
return false, 0
}
func (c *flowController) CheckFlowControlViolation(highestByte protocol.ByteCount) bool {
func (c *flowController) CheckFlowControlViolation() bool {
c.mutex.Lock()
defer c.mutex.Unlock()
if highestByte > c.receiveFlowControlWindow {
if c.highestReceived > c.receiveFlowControlWindow {
return true
}
return false

View File

@@ -103,12 +103,26 @@ var _ = Describe("Flow controller", func() {
Expect(updateNecessary).To(BeFalse())
})
It("updates the highestReceived", func() {
controller.highestReceived = 1337
controller.UpdateHighestReceived(1338)
Expect(controller.highestReceived).To(Equal(protocol.ByteCount(1338)))
})
It("does not decrease the highestReceived", func() {
controller.highestReceived = 1337
controller.UpdateHighestReceived(1000)
Expect(controller.highestReceived).To(Equal(protocol.ByteCount(1337)))
})
It("detects a flow control violation", func() {
Expect(controller.CheckFlowControlViolation(receiveFlowControlWindow + 1)).To(BeTrue())
controller.UpdateHighestReceived(receiveFlowControlWindow + 1)
Expect(controller.CheckFlowControlViolation()).To(BeTrue())
})
It("does not give a flow control violation when using the window completely", func() {
Expect(controller.CheckFlowControlViolation(receiveFlowControlWindow)).To(BeFalse())
controller.UpdateHighestReceived(receiveFlowControlWindow)
Expect(controller.CheckFlowControlViolation()).To(BeFalse())
})
})
})

View File

@@ -203,7 +203,8 @@ func (s *stream) Close() error {
// AddStreamFrame adds a new stream frame
func (s *stream) AddStreamFrame(frame *frames.StreamFrame) error {
maxOffset := frame.Offset + protocol.ByteCount(len(frame.Data))
if s.flowController.CheckFlowControlViolation(maxOffset) {
s.flowController.UpdateHighestReceived(maxOffset)
if s.flowController.CheckFlowControlViolation() {
return errFlowControlViolation
}