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

@ -2,6 +2,7 @@ package quic
import (
"bytes"
"sync"
"time"
"github.com/lucas-clemente/quic-go/ackhandlerlegacy"
@ -47,6 +48,7 @@ var _ = Describe("Packet packer", func() {
var (
packer *packetPacker
publicHeaderLen protocol.ByteCount
streamFramer *streamFramer
)
BeforeEach(func() {
@ -55,12 +57,14 @@ var _ = Describe("Packet packer", func() {
fcm.sendWindowSizes[5] = protocol.MaxByteCount
fcm.sendWindowSizes[7] = protocol.MaxByteCount
streamFramer = newStreamFramer(&map[protocol.StreamID]*stream{}, &sync.RWMutex{}, fcm)
packer = &packetPacker{
cryptoSetup: &handshake.CryptoSetup{},
connectionParametersManager: handshake.NewConnectionParamatersManager(),
sentPacketHandler: newMockSentPacketHandler(),
blockedManager: newBlockedManager(),
streamFrameQueue: newStreamFrameQueue(fcm),
streamFramer: streamFramer,
}
publicHeaderLen = 1 + 8 + 1 // 1 flag byte, 8 connection ID, 1 packet number
packer.version = protocol.Version34
@ -79,19 +83,19 @@ var _ = Describe("Packet packer", func() {
It("doesn't set a private header for QUIC version >= 34", func() {
// This is not trivial to test, since PackPacket() already encrypts the packet
// So pack the packet for QUIC 33, then for QUIC 34. The packet for QUIC 33 should be 1 byte longer, since it contains the Private Header
f := frames.StreamFrame{
f := &frames.StreamFrame{
StreamID: 5,
Data: []byte("foobar"),
}
// pack the packet for QUIC version 33
packer.version = protocol.Version33
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p33, err := packer.PackPacket(nil, []frames.Frame{})
Expect(err).ToNot(HaveOccurred())
Expect(p33).ToNot(BeNil())
// pack the packet for QUIC version 34
packer.version = protocol.Version34
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p34, err := packer.PackPacket(nil, []frames.Frame{})
Expect(err).ToNot(HaveOccurred())
Expect(p34).ToNot(BeNil())
@ -100,11 +104,11 @@ var _ = Describe("Packet packer", func() {
})
It("packs single packets", func() {
f := frames.StreamFrame{
f := &frames.StreamFrame{
StreamID: 5,
Data: []byte{0xDE, 0xCA, 0xFB, 0xAD},
}
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p, err := packer.PackPacket(nil, []frames.Frame{})
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
@ -214,11 +218,11 @@ var _ = Describe("Packet packer", func() {
})
It("only increases the packet number when there is an actual packet to send", func() {
f := frames.StreamFrame{
f := &frames.StreamFrame{
StreamID: 5,
Data: []byte{0xDE, 0xCA, 0xFB, 0xAD},
}
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p, err := packer.PackPacket(nil, []frames.Frame{})
Expect(p).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
@ -227,7 +231,7 @@ var _ = Describe("Packet packer", func() {
Expect(p).To(BeNil())
Expect(err).ToNot(HaveOccurred())
Expect(packer.lastPacketNumber).To(Equal(protocol.PacketNumber(1)))
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p, err = packer.PackPacket(nil, []frames.Frame{})
Expect(p).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
@ -236,7 +240,7 @@ var _ = Describe("Packet packer", func() {
Context("Stream Frame handling", func() {
It("does not splits a stream frame with maximum size", func() {
f := frames.StreamFrame{
f := &frames.StreamFrame{
Offset: 1,
StreamID: 5,
DataLenPresent: false,
@ -244,7 +248,7 @@ var _ = Describe("Packet packer", func() {
minLength, _ := f.MinLength(0)
maxStreamFrameDataLen := protocol.MaxFrameAndPublicHeaderSize - publicHeaderLen - minLength
f.Data = bytes.Repeat([]byte{'f'}, int(maxStreamFrameDataLen))
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
payloadFrames, err := packer.composeNextPacket(nil, publicHeaderLen)
Expect(err).ToNot(HaveOccurred())
Expect(payloadFrames).To(HaveLen(1))
@ -256,18 +260,18 @@ var _ = Describe("Packet packer", func() {
It("correctly handles a stream frame with one byte less than maximum size", func() {
maxStreamFrameDataLen := protocol.MaxFrameAndPublicHeaderSize - publicHeaderLen - (1 + 1 + 2) - 1
f1 := frames.StreamFrame{
f1 := &frames.StreamFrame{
StreamID: 5,
Offset: 1,
Data: bytes.Repeat([]byte{'f'}, int(maxStreamFrameDataLen)),
}
f2 := frames.StreamFrame{
f2 := &frames.StreamFrame{
StreamID: 5,
Offset: 1,
Data: []byte("foobar"),
}
packer.AddStreamFrame(f1)
packer.AddStreamFrame(f2)
streamFramer.AddFrameForRetransmission(f1)
streamFramer.AddFrameForRetransmission(f2)
p, err := packer.PackPacket(nil, []frames.Frame{})
Expect(err).ToNot(HaveOccurred())
Expect(p.raw).To(HaveLen(int(protocol.MaxPacketSize - 1)))
@ -280,21 +284,21 @@ var _ = Describe("Packet packer", func() {
})
It("packs multiple small stream frames into single packet", func() {
f1 := frames.StreamFrame{
f1 := &frames.StreamFrame{
StreamID: 5,
Data: []byte{0xDE, 0xCA, 0xFB, 0xAD},
}
f2 := frames.StreamFrame{
f2 := &frames.StreamFrame{
StreamID: 5,
Data: []byte{0xBE, 0xEF, 0x13, 0x37},
}
f3 := frames.StreamFrame{
f3 := &frames.StreamFrame{
StreamID: 3,
Data: []byte{0xCA, 0xFE},
}
packer.AddStreamFrame(f1)
packer.AddStreamFrame(f2)
packer.AddStreamFrame(f3)
streamFramer.AddFrameForRetransmission(f1)
streamFramer.AddFrameForRetransmission(f2)
streamFramer.AddFrameForRetransmission(f3)
p, err := packer.PackPacket(nil, []frames.Frame{})
Expect(p).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
@ -313,26 +317,26 @@ var _ = Describe("Packet packer", func() {
It("packs a packet with a stream frame larger than maximum size, in QUIC < 34", func() {
packer.version = protocol.Version33
f := frames.StreamFrame{
f := &frames.StreamFrame{
StreamID: 5,
Offset: 1,
Data: bytes.Repeat([]byte{'f'}, int(protocol.MaxPacketSize)+100),
}
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p, err := packer.PackPacket(nil, []frames.Frame{})
Expect(err).ToNot(HaveOccurred())
Expect(p.raw).To(HaveLen(int(protocol.MaxPacketSize)))
})
It("splits one stream frame larger than maximum size", func() {
f := frames.StreamFrame{
f := &frames.StreamFrame{
StreamID: 7,
Offset: 1,
}
minLength, _ := f.MinLength(0)
maxStreamFrameDataLen := protocol.MaxFrameAndPublicHeaderSize - publicHeaderLen - minLength + 1 // + 1 since MinceLength is 1 bigger than the actual StreamFrame header
maxStreamFrameDataLen := protocol.MaxFrameAndPublicHeaderSize - publicHeaderLen - minLength
f.Data = bytes.Repeat([]byte{'f'}, int(maxStreamFrameDataLen)+200)
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
payloadFrames, err := packer.composeNextPacket(nil, publicHeaderLen)
Expect(err).ToNot(HaveOccurred())
Expect(payloadFrames).To(HaveLen(1))
@ -350,18 +354,18 @@ var _ = Describe("Packet packer", func() {
It("packs 2 stream frames that are too big for one packet correctly", func() {
maxStreamFrameDataLen := protocol.MaxFrameAndPublicHeaderSize - publicHeaderLen - (1 + 1 + 2)
f1 := frames.StreamFrame{
f1 := &frames.StreamFrame{
StreamID: 5,
Data: bytes.Repeat([]byte{'f'}, int(maxStreamFrameDataLen)+100),
Offset: 1,
}
f2 := frames.StreamFrame{
f2 := &frames.StreamFrame{
StreamID: 5,
Data: bytes.Repeat([]byte{'f'}, int(maxStreamFrameDataLen)+100),
Offset: 1,
}
packer.AddStreamFrame(f1)
packer.AddStreamFrame(f2)
streamFramer.AddFrameForRetransmission(f1)
streamFramer.AddFrameForRetransmission(f2)
p, err := packer.PackPacket(nil, []frames.Frame{})
Expect(err).ToNot(HaveOccurred())
Expect(p.frames).To(HaveLen(1))
@ -384,13 +388,13 @@ var _ = Describe("Packet packer", func() {
})
It("packs a packet that has the maximum packet size when given a large enough stream frame", func() {
f := frames.StreamFrame{
f := &frames.StreamFrame{
StreamID: 5,
Offset: 1,
}
minLength, _ := f.MinLength(0)
f.Data = bytes.Repeat([]byte{'f'}, int(protocol.MaxFrameAndPublicHeaderSize-publicHeaderLen-minLength+1)) // + 1 since MinceLength is 1 bigger than the actual StreamFrame header
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p, err := packer.PackPacket(nil, []frames.Frame{})
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
@ -398,14 +402,14 @@ var _ = Describe("Packet packer", func() {
})
It("splits a stream frame larger than the maximum size", func() {
f := frames.StreamFrame{
f := &frames.StreamFrame{
StreamID: 5,
Offset: 1,
}
minLength, _ := f.MinLength(0)
f.Data = bytes.Repeat([]byte{'f'}, int(protocol.MaxFrameAndPublicHeaderSize-publicHeaderLen-minLength+2)) // + 2 since MinceLength is 1 bigger than the actual StreamFrame header
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
payloadFrames, err := packer.composeNextPacket(nil, publicHeaderLen)
Expect(err).ToNot(HaveOccurred())
Expect(payloadFrames).To(HaveLen(1))
@ -418,12 +422,12 @@ var _ = Describe("Packet packer", func() {
PContext("Blocked frames", func() {
It("adds a blocked frame to a packet if there is enough space", func() {
length := 100
packer.AddBlocked(5, protocol.ByteCount(length))
f := frames.StreamFrame{
// packer.AddBlocked(5, protocol.ByteCount(length))
f := &frames.StreamFrame{
StreamID: 5,
Data: bytes.Repeat([]byte{'f'}, length),
}
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p, err := packer.composeNextPacket(nil, publicHeaderLen)
Expect(err).ToNot(HaveOccurred())
Expect(p).To(HaveLen(2))
@ -432,12 +436,12 @@ var _ = Describe("Packet packer", func() {
It("removes the dataLen attribute from the last StreamFrame, even if it inserted a BlockedFrame before", func() {
length := 100
packer.AddBlocked(5, protocol.ByteCount(length))
f := frames.StreamFrame{
// packer.AddBlocked(5, protocol.ByteCount(length))
f := &frames.StreamFrame{
StreamID: 5,
Data: bytes.Repeat([]byte{'f'}, length),
}
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p, err := packer.composeNextPacket(nil, publicHeaderLen)
Expect(err).ToNot(HaveOccurred())
Expect(p).To(HaveLen(2))
@ -446,12 +450,12 @@ var _ = Describe("Packet packer", func() {
It("packs a BlockedFrame in the next packet if the current packet doesn't have enough space", func() {
dataLen := int(protocol.MaxFrameAndPublicHeaderSize-publicHeaderLen) - (1 + 1 + 2) + 1
packer.AddBlocked(5, protocol.ByteCount(dataLen))
f := frames.StreamFrame{
// packer.AddBlocked(5, protocol.ByteCount(dataLen))
f := &frames.StreamFrame{
StreamID: 5,
Data: bytes.Repeat([]byte{'f'}, dataLen),
}
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p, err := packer.composeNextPacket(nil, publicHeaderLen)
Expect(err).ToNot(HaveOccurred())
Expect(p).To(HaveLen(1))
@ -461,20 +465,19 @@ var _ = Describe("Packet packer", func() {
Expect(p[0]).To(Equal(&frames.BlockedFrame{StreamID: 5}))
})
It("packs a packet with the maximum size with a BlocedFrame", func() {
It("packs a packet with the maximum size with a BlockedFrame", func() {
blockedFrame := &frames.BlockedFrame{StreamID: 0x1337}
blockedFrameLen, _ := blockedFrame.MinLength(0)
f1 := frames.StreamFrame{
f1 := &frames.StreamFrame{
StreamID: 5,
Offset: 1,
}
streamFrameHeaderLen, _ := f1.MinLength(0)
streamFrameHeaderLen-- // - 1 since MinceLength is 1 bigger than the actual StreamFrame header
// this is the maximum dataLen of a StreamFrames that fits into one packet
dataLen := int(protocol.MaxFrameAndPublicHeaderSize - publicHeaderLen - streamFrameHeaderLen - blockedFrameLen)
packer.AddBlocked(5, protocol.ByteCount(dataLen))
// packer.AddBlocked(5, protocol.ByteCount(dataLen))
f1.Data = bytes.Repeat([]byte{'f'}, dataLen)
packer.AddStreamFrame(f1)
streamFramer.AddFrameForRetransmission(f1)
p, err := packer.PackPacket(nil, []frames.Frame{})
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
@ -487,12 +490,12 @@ var _ = Describe("Packet packer", func() {
// TODO: fix this once connection-level BlockedFrames are sent out at the right time
// see https://github.com/lucas-clemente/quic-go/issues/113
It("packs a connection-level BlockedFrame", func() {
packer.AddBlocked(0, 0x1337)
f := frames.StreamFrame{
// packer.AddBlocked(0, 0x1337)
f := &frames.StreamFrame{
StreamID: 5,
Data: []byte("foobar"),
}
packer.AddStreamFrame(f)
streamFramer.AddFrameForRetransmission(f)
p, err := packer.composeNextPacket(nil, publicHeaderLen)
Expect(err).ToNot(HaveOccurred())
Expect(p).To(HaveLen(2))