add FlowController method to determine if we need to send a Blocked frame

ref #100
This commit is contained in:
Marten Seemann
2016-05-17 17:59:18 +07:00
parent e4a7386ca1
commit d2766cad02
2 changed files with 46 additions and 2 deletions

View File

@@ -8,8 +8,9 @@ import (
type flowController struct {
streamID protocol.StreamID
bytesSent protocol.ByteCount
sendFlowControlWindow protocol.ByteCount
bytesSent protocol.ByteCount
sendFlowControlWindow protocol.ByteCount
lastBlockedSentForOffset protocol.ByteCount
bytesRead protocol.ByteCount
receiveWindowUpdateThreshold protocol.ByteCount
@@ -51,6 +52,21 @@ func (c *flowController) AddBytesRead(n protocol.ByteCount) {
c.bytesRead += n
}
// MaybeTriggerBlocked determines if it is necessary to send a Blocked for this stream
// it makes sure that only one Blocked is sent for each offset
func (c *flowController) MaybeTriggerBlocked() bool {
if c.SendWindowSize() != 0 {
return false
}
if c.lastBlockedSentForOffset == c.sendFlowControlWindow {
return false
}
c.lastBlockedSentForOffset = c.sendFlowControlWindow
return true
}
// MaybeTriggerWindowUpdate determines if it is necessary to send a WindowUpdate
// if so, it returns true and the offset of the window
func (c *flowController) MaybeTriggerWindowUpdate() (bool, protocol.ByteCount) {

View File

@@ -41,6 +41,34 @@ var _ = Describe("Flow controller", func() {
Expect(updateSuccessful).To(BeFalse())
Expect(controller.SendWindowSize()).To(Equal(protocol.ByteCount(20)))
})
Context("Blocked", func() {
var sendFlowControlWindow protocol.ByteCount = 20
BeforeEach(func() {
controller.sendFlowControlWindow = sendFlowControlWindow
})
It("sends a Blocked when there's no space left in the window", func() {
controller.bytesSent = sendFlowControlWindow
Expect(controller.MaybeTriggerBlocked()).To(BeTrue())
})
It("does not send a Blocked when there's still space in the window", func() {
controller.bytesSent = sendFlowControlWindow - 1
Expect(controller.MaybeTriggerBlocked()).To(BeFalse())
})
It("only sends one Blocked for one offset", func() {
controller.bytesSent = sendFlowControlWindow
Expect(controller.MaybeTriggerBlocked()).To(BeTrue())
Expect(controller.MaybeTriggerBlocked()).To(BeFalse())
updateSuccessfull := controller.UpdateSendWindow(sendFlowControlWindow + 1)
Expect(updateSuccessfull).To(BeTrue())
controller.bytesSent = sendFlowControlWindow + 1
Expect(controller.MaybeTriggerBlocked()).To(BeTrue())
})
})
})
Context("receive flow control", func() {