replace streamFrameQueue with just-in-time framing of written data

This commits replaces the stream frame queue with a framer which
requests data from the streams just when a frame is needed by the
packet packer. This simplifies a lot of things and allows some other
refactorings, see issue #83.

There are a few pending tests which will be fixed soon.
This commit is contained in:
Lucas Clemente 2016-07-07 18:04:10 +02:00
parent ee77e85af3
commit d1e3b541d3
15 changed files with 1019 additions and 1499 deletions

View file

@ -14,18 +14,10 @@ import (
)
type mockStreamHandler struct {
frames []*frames.StreamFrame
receivedBlockedCalled bool
receivedBlockedForStream protocol.StreamID
receiveFlowControlWindowCalled bool
receiveFlowControlWindowCalledForStream protocol.StreamID
}
func (m *mockStreamHandler) streamBlocked(streamID protocol.StreamID, byteOffset protocol.ByteCount) {
m.receivedBlockedCalled = true
m.receivedBlockedForStream = streamID
scheduledSending bool
}
func (m *mockStreamHandler) updateReceiveFlowControlWindow(streamID protocol.StreamID, byteOffset protocol.ByteCount) error {
@ -33,11 +25,7 @@ func (m *mockStreamHandler) updateReceiveFlowControlWindow(streamID protocol.Str
m.receiveFlowControlWindowCalledForStream = streamID
return nil
}
func (m *mockStreamHandler) queueStreamFrame(f *frames.StreamFrame) error {
m.frames = append(m.frames, f)
return nil
}
func (m *mockStreamHandler) scheduleSending() { m.scheduledSending = true }
type mockFlowControlHandler struct {
streamsContributing []protocol.StreamID
@ -118,10 +106,9 @@ var _ = Describe("Stream", func() {
var streamID protocol.StreamID = 1337
handler = &mockStreamHandler{}
cpm := handshake.NewConnectionParamatersManager()
flowController := flowcontrol.NewFlowController(streamID, cpm)
flowControlManager := flowcontrol.NewFlowControlManager(cpm)
flowControlManager.NewStream(streamID, true)
str, _ = newStream(handler, cpm, flowController, flowControlManager, streamID)
str, _ = newStream(handler, cpm, flowControlManager, streamID)
})
It("gets stream id", func() {
@ -302,263 +289,92 @@ var _ = Describe("Stream", func() {
})
Context("writing", func() {
It("writes str frames", func() {
n, err := str.Write([]byte("foobar"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(6))
Expect(handler.frames).To(HaveLen(1))
Expect(handler.frames[0]).To(Equal(&frames.StreamFrame{
StreamID: 1337,
Data: []byte("foobar"),
}))
It("writes and gets all data at once", func(done Done) {
go func() {
n, err := str.Write([]byte("foobar"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(6))
close(done)
}()
Eventually(func() []byte {
str.mutex.Lock()
defer str.mutex.Unlock()
return str.dataForWriting
}).Should(Equal([]byte("foobar")))
Expect(handler.scheduledSending).To(BeTrue())
Expect(str.lenOfDataForWriting()).To(Equal(protocol.ByteCount(6)))
data := str.getDataForWriting(1000)
Expect(data).To(Equal([]byte("foobar")))
Expect(str.writeOffset).To(Equal(protocol.ByteCount(6)))
Expect(str.dataForWriting).To(BeNil())
})
It("writes multiple str frames", func() {
n, err := str.Write([]byte("foo"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(3))
n, err = str.Write([]byte("bar"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(3))
Expect(handler.frames).To(HaveLen(2))
Expect(handler.frames[0]).To(Equal(&frames.StreamFrame{
StreamID: 1337,
Data: []byte("foo"),
}))
Expect(handler.frames[1]).To(Equal(&frames.StreamFrame{
StreamID: 1337,
Data: []byte("bar"),
Offset: 3,
}))
It("writes and gets data in two turns", func(done Done) {
go func() {
n, err := str.Write([]byte("foobar"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(6))
close(done)
}()
Eventually(func() []byte {
str.mutex.Lock()
defer str.mutex.Unlock()
return str.dataForWriting
}).Should(Equal([]byte("foobar")))
Expect(str.lenOfDataForWriting()).To(Equal(protocol.ByteCount(6)))
data := str.getDataForWriting(3)
Expect(data).To(Equal([]byte("foo")))
Expect(str.writeOffset).To(Equal(protocol.ByteCount(3)))
Expect(str.dataForWriting).ToNot(BeNil())
Expect(str.lenOfDataForWriting()).To(Equal(protocol.ByteCount(3)))
data = str.getDataForWriting(3)
Expect(data).To(Equal([]byte("bar")))
Expect(str.writeOffset).To(Equal(protocol.ByteCount(6)))
Expect(str.dataForWriting).To(BeNil())
Expect(str.lenOfDataForWriting()).To(Equal(protocol.ByteCount(0)))
})
It("closes", func() {
err := str.Close()
Expect(err).ToNot(HaveOccurred())
Expect(handler.frames).To(HaveLen(1))
Expect(handler.frames[0]).To(Equal(&frames.StreamFrame{
StreamID: 1337,
FinBit: true,
Offset: 0,
}))
})
It("returns remote errors", func() {
It("returns remote errors", func(done Done) {
testErr := errors.New("test")
str.RegisterError(testErr)
n, err := str.Write([]byte("foo"))
Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr))
})
Context("flow control", func() {
It("writes everything if the flow control window is big enough", func() {
data := []byte{0xDE, 0xCA, 0xFB, 0xAD}
updated := str.flowController.UpdateSendWindow(4)
Expect(updated).To(BeTrue())
n, err := str.Write(data)
Expect(n).To(Equal(4))
Expect(err).ToNot(HaveOccurred())
Expect(handler.frames).To(HaveLen(1))
Expect(handler.frames[0].Data).To(Equal(data))
})
It("doesn't care about the connection flow control window if it is not contributing", func() {
updated := str.flowController.UpdateSendWindow(4)
Expect(updated).To(BeTrue())
str.contributesToConnectionFlowControl = false
updated = str.connectionFlowController.UpdateSendWindow(1)
Expect(updated).To(BeTrue())
n, err := str.Write([]byte{0xDE, 0xCA, 0xFB, 0xAD})
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(4))
})
It("returns true when the flow control window was updated", func() {
updated := str.flowController.UpdateSendWindow(4)
Expect(updated).To(BeTrue())
updated = str.UpdateSendFlowControlWindow(5)
Expect(updated).To(BeTrue())
})
It("returns false when the flow control window was not updated", func() {
updated := str.flowController.UpdateSendWindow(4)
Expect(updated).To(BeTrue())
updated = str.UpdateSendFlowControlWindow(3)
Expect(updated).To(BeFalse())
})
It("waits for a stream flow control window update", func() {
var b bool
updated := str.flowController.UpdateSendWindow(1)
Expect(updated).To(BeTrue())
_, err := str.Write([]byte{0x42})
Expect(err).ToNot(HaveOccurred())
go func() {
time.Sleep(2 * time.Millisecond)
b = true
str.UpdateSendFlowControlWindow(3)
}()
n, err := str.Write([]byte{0x13, 0x37})
Expect(err).ToNot(HaveOccurred())
Expect(b).To(BeTrue())
Expect(n).To(Equal(2))
Expect(str.writeOffset).To(Equal(protocol.ByteCount(3)))
Expect(handler.frames).To(HaveLen(2))
Expect(handler.frames[0].Offset).To(Equal(protocol.ByteCount(0)))
Expect(handler.frames[0].Data).To(Equal([]byte{0x42}))
Expect(handler.frames[1].Offset).To(Equal(protocol.ByteCount(1)))
Expect(handler.frames[1].Data).To(Equal([]byte{0x13, 0x37}))
})
It("does not write too much data after receiving a window update", func() {
var b bool
updated := str.flowController.UpdateSendWindow(1)
Expect(updated).To(BeTrue())
go func() {
time.Sleep(2 * time.Millisecond)
b = true
str.UpdateSendFlowControlWindow(5)
}()
n, err := str.Write([]byte{0x13, 0x37})
Expect(b).To(BeTrue())
Expect(n).To(Equal(2))
Expect(str.writeOffset).To(Equal(protocol.ByteCount(2)))
Expect(err).ToNot(HaveOccurred())
Expect(handler.frames).To(HaveLen(2))
Expect(handler.frames[0].Data).To(Equal([]byte{0x13}))
Expect(handler.frames[1].Data).To(Equal([]byte{0x37}))
})
It("waits for a connection flow control window update", func() {
var b bool
updated := str.flowController.UpdateSendWindow(1000)
Expect(updated).To(BeTrue())
updated = str.connectionFlowController.UpdateSendWindow(1)
Expect(updated).To(BeTrue())
str.contributesToConnectionFlowControl = true
_, err := str.Write([]byte{0x42})
Expect(err).ToNot(HaveOccurred())
Expect(str.writeOffset).To(Equal(protocol.ByteCount(1)))
var sendWindowUpdated bool
go func() {
time.Sleep(2 * time.Millisecond)
b = true
sendWindowUpdated = str.connectionFlowController.UpdateSendWindow(3)
str.ConnectionFlowControlWindowUpdated()
}()
n, err := str.Write([]byte{0x13, 0x37})
Expect(b).To(BeTrue())
Expect(sendWindowUpdated).To(BeTrue())
Expect(n).To(Equal(2))
Expect(str.writeOffset).To(Equal(protocol.ByteCount(3)))
Expect(err).ToNot(HaveOccurred())
})
It("splits writing of frames when given more data than the flow control windows size", func() {
updated := str.flowController.UpdateSendWindow(2)
Expect(updated).To(BeTrue())
var b bool
go func() {
time.Sleep(time.Millisecond)
b = true
str.UpdateSendFlowControlWindow(4)
}()
n, err := str.Write([]byte{0xDE, 0xCA, 0xFB, 0xAD})
Expect(err).ToNot(HaveOccurred())
Expect(handler.frames).To(HaveLen(2))
Expect(b).To(BeTrue())
Expect(n).To(Equal(4))
Expect(str.writeOffset).To(Equal(protocol.ByteCount(4)))
})
It("writes after a flow control window update", func() {
var b bool
updated := str.flowController.UpdateSendWindow(1)
Expect(updated).To(BeTrue())
_, err := str.Write([]byte{0x42})
Expect(err).ToNot(HaveOccurred())
go func() {
time.Sleep(time.Millisecond)
b = true
str.UpdateSendFlowControlWindow(3)
}()
n, err := str.Write([]byte{0xDE, 0xAD})
Expect(err).ToNot(HaveOccurred())
Expect(b).To(BeTrue())
Expect(n).To(Equal(2))
Expect(str.writeOffset).To(Equal(protocol.ByteCount(3)))
})
It("immediately returns on remote errors", func() {
var b bool
updated := str.flowController.UpdateSendWindow(1)
Expect(updated).To(BeTrue())
testErr := errors.New("test error")
go func() {
time.Sleep(time.Millisecond)
b = true
str.RegisterError(testErr)
}()
_, err := str.Write([]byte{0xDE, 0xCA, 0xFB, 0xAD})
Expect(err).To(MatchError(testErr))
Expect(b).To(BeTrue())
})
It("works with large flow control windows", func() {
// This paniced before due to a wrong cast,
// see https://github.com/lucas-clemente/quic-go/issues/143
str.contributesToConnectionFlowControl = false
updated := str.UpdateSendFlowControlWindow(protocol.ByteCount(1) << 63)
Expect(updated).To(BeTrue())
_, err := str.Write([]byte("foobar"))
Expect(err).NotTo(HaveOccurred())
})
close(done)
})
})
PContext("Blocked streams", func() {
It("notifies the session when a stream is flow control blocked", func() {
updated, err := str.flowControlManager.UpdateWindow(str.streamID, 1337)
Expect(err).ToNot(HaveOccurred())
Expect(updated).To(BeTrue())
str.flowControlManager.AddBytesSent(str.streamID, 1337)
str.maybeTriggerBlocked()
Expect(handler.receivedBlockedCalled).To(BeTrue())
Expect(handler.receivedBlockedForStream).To(Equal(str.streamID))
Context("closing", func() {
It("sets closed when calling Close", func() {
str.Close()
Expect(str.closed).ToNot(BeZero())
})
It("notifies the session as soon as a stream is reaching the end of the window", func() {
updated, err := str.flowControlManager.UpdateWindow(str.streamID, 4)
Expect(err).ToNot(HaveOccurred())
Expect(updated).To(BeTrue())
str.Write([]byte{0xDE, 0xCA, 0xFB, 0xAD})
Expect(handler.receivedBlockedCalled).To(BeTrue())
Expect(handler.receivedBlockedForStream).To(Equal(str.streamID))
It("allows FIN", func() {
str.Close()
Expect(str.shouldSendFin()).To(BeTrue())
})
It("notifies the session as soon as a stream is flow control blocked", func() {
updated, err := str.flowControlManager.UpdateWindow(str.streamID, 2)
Expect(err).ToNot(HaveOccurred())
Expect(updated).To(BeTrue())
go func() {
str.Write([]byte{0xDE, 0xCA, 0xFB, 0xAD})
}()
time.Sleep(time.Millisecond)
Expect(handler.receivedBlockedCalled).To(BeTrue())
Expect(handler.receivedBlockedForStream).To(Equal(str.streamID))
It("does not allow FIN when there's still data", func() {
str.dataForWriting = []byte("foobar")
str.Close()
Expect(str.shouldSendFin()).To(BeFalse())
})
It("does not allow FIN when the stream is not closed", func() {
Expect(str.shouldSendFin()).To(BeFalse())
})
It("does not allow FIN after an error", func() {
str.RegisterError(errors.New("test"))
Expect(str.shouldSendFin()).To(BeFalse())
})
It("does not allow FIN twice", func() {
str.Close()
Expect(str.shouldSendFin()).To(BeTrue())
str.sentFin()
Expect(str.shouldSendFin()).To(BeFalse())
})
})