use the same interface for new and legacy AckHandlers

ref #55
This commit is contained in:
Marten Seemann 2016-07-26 18:31:54 +07:00
parent 50a7e55c72
commit 03bad0e86b
15 changed files with 150 additions and 271 deletions

View file

@ -3,17 +3,18 @@ package ackhandler
import ( import (
"time" "time"
"github.com/lucas-clemente/quic-go/ackhandlerlegacy"
"github.com/lucas-clemente/quic-go/frames" "github.com/lucas-clemente/quic-go/frames"
"github.com/lucas-clemente/quic-go/protocol" "github.com/lucas-clemente/quic-go/protocol"
) )
// SentPacketHandler handles ACKs received for outgoing packets // SentPacketHandler handles ACKs received for outgoing packets
type SentPacketHandler interface { type SentPacketHandler interface {
SentPacket(packet *Packet) error SentPacket(packet *ackhandlerlegacy.Packet) error
ReceivedAck(ackFrame *frames.AckFrame, withPacketNumber protocol.PacketNumber) error ReceivedAck(ackFrame *frames.AckFrame, withPacketNumber protocol.PacketNumber) error
ProbablyHasPacketForRetransmission() bool ProbablyHasPacketForRetransmission() bool
DequeuePacketForRetransmission() (packet *Packet) DequeuePacketForRetransmission() (packet *ackhandlerlegacy.Packet)
BytesInFlight() protocol.ByteCount BytesInFlight() protocol.ByteCount
GetLargestAcked() protocol.PacketNumber GetLargestAcked() protocol.PacketNumber
@ -26,7 +27,8 @@ type SentPacketHandler interface {
// ReceivedPacketHandler handles ACKs needed to send for incoming packets // ReceivedPacketHandler handles ACKs needed to send for incoming packets
type ReceivedPacketHandler interface { type ReceivedPacketHandler interface {
ReceivedPacket(packetNumber protocol.PacketNumber) error // TODO: remove entroypyBit once we drop support for QUIC 33
ReceivedPacket(packetNumber protocol.PacketNumber, entropyBit bool) error
ReceivedStopWaiting(*frames.StopWaitingFrame) error ReceivedStopWaiting(*frames.StopWaitingFrame) error
GetAckFrame(dequeue bool) (*frames.AckFrame, error) GetAckFrame(dequeue bool) (*frames.AckFrame, error)
@ -34,7 +36,7 @@ type ReceivedPacketHandler interface {
// StopWaitingManager manages StopWaitings for sent packets // StopWaitingManager manages StopWaitings for sent packets
type StopWaitingManager interface { type StopWaitingManager interface {
RegisterPacketForRetransmission(packet *Packet) RegisterPacketForRetransmission(packet *ackhandlerlegacy.Packet)
GetStopWaitingFrame() *frames.StopWaitingFrame GetStopWaitingFrame() *frames.StopWaitingFrame
SentStopWaitingWithPacket(packetNumber protocol.PacketNumber) SentStopWaitingWithPacket(packetNumber protocol.PacketNumber)
ReceivedAckForPacketNumber(packetNumber protocol.PacketNumber) ReceivedAckForPacketNumber(packetNumber protocol.PacketNumber)

View file

@ -1,49 +0,0 @@
package ackhandler
import (
"time"
"github.com/lucas-clemente/quic-go/frames"
"github.com/lucas-clemente/quic-go/protocol"
)
// A Packet is a packet
type Packet struct {
PacketNumber protocol.PacketNumber
Frames []frames.Frame
Length protocol.ByteCount
MissingReports uint8
Retransmitted bool // has this Packet ever been retransmitted
sendTime time.Time
}
// GetStreamFramesForRetransmission gets all the streamframes for retransmission
func (p *Packet) GetStreamFramesForRetransmission() []*frames.StreamFrame {
var streamFrames []*frames.StreamFrame
for _, frame := range p.Frames {
if streamFrame, isStreamFrame := frame.(*frames.StreamFrame); isStreamFrame {
streamFrames = append(streamFrames, streamFrame)
}
}
return streamFrames
}
// GetControlFramesForRetransmission gets all the control frames for retransmission
func (p *Packet) GetControlFramesForRetransmission() []frames.Frame {
var controlFrames []frames.Frame
for _, frame := range p.Frames {
// omit ACKs
if _, isStreamFrame := frame.(*frames.StreamFrame); isStreamFrame {
continue
}
_, isAck := frame.(*frames.AckFrame)
_, isStopWaiting := frame.(*frames.StopWaitingFrame)
if !isAck && !isStopWaiting {
controlFrames = append(controlFrames, frame)
}
}
return controlFrames
}

View file

@ -1,96 +0,0 @@
package ackhandler
import (
"github.com/lucas-clemente/quic-go/frames"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Packet", func() {
Context("getFramesForRetransmission", func() {
var packet Packet
var streamFrame1, streamFrame2 *frames.StreamFrame
var ackFrame1, ackFrame2 *frames.AckFrame
var stopWaitingFrame *frames.StopWaitingFrame
var rstStreamFrame *frames.RstStreamFrame
var windowUpdateFrame *frames.WindowUpdateFrame
BeforeEach(func() {
streamFrame1 = &frames.StreamFrame{
StreamID: 5,
Data: []byte{0x13, 0x37},
}
streamFrame2 = &frames.StreamFrame{
StreamID: 6,
Data: []byte{0xDE, 0xCA, 0xFB, 0xAD},
}
ackFrame1 = &frames.AckFrame{
LargestAcked: 13,
LowestAcked: 1,
}
ackFrame2 = &frames.AckFrame{
LargestAcked: 333,
LowestAcked: 100,
}
rstStreamFrame = &frames.RstStreamFrame{
StreamID: 555,
ErrorCode: 1337,
}
stopWaitingFrame = &frames.StopWaitingFrame{
LeastUnacked: 7331,
}
windowUpdateFrame = &frames.WindowUpdateFrame{
StreamID: 999,
}
packet = Packet{
PacketNumber: 1337,
Frames: []frames.Frame{windowUpdateFrame, streamFrame1, ackFrame1, streamFrame2, rstStreamFrame, ackFrame2, stopWaitingFrame},
}
})
It("gets all StreamFrames", func() {
streamFrames := packet.GetStreamFramesForRetransmission()
Expect(streamFrames).To(HaveLen(2))
Expect(streamFrames).To(ContainElement(streamFrame1))
Expect(streamFrames).To(ContainElement(streamFrame2))
})
It("gets all control frames", func() {
controlFrames := packet.GetControlFramesForRetransmission()
Expect(controlFrames).To(HaveLen(2))
Expect(controlFrames).To(ContainElement(rstStreamFrame))
Expect(controlFrames).To(ContainElement(windowUpdateFrame))
})
It("does not return any ACK frames", func() {
controlFrames := packet.GetControlFramesForRetransmission()
Expect(controlFrames).ToNot(ContainElement(ackFrame1))
Expect(controlFrames).ToNot(ContainElement(ackFrame2))
})
It("does not return any ACK frames", func() {
controlFrames := packet.GetControlFramesForRetransmission()
Expect(controlFrames).ToNot(ContainElement(stopWaitingFrame))
})
It("returns an empty slice of StreamFrames if no StreamFrames are queued", func() {
// overwrite the globally defined packet here
packet := Packet{
PacketNumber: 1337,
Frames: []frames.Frame{ackFrame1, rstStreamFrame},
}
streamFrames := packet.GetStreamFramesForRetransmission()
Expect(streamFrames).To(BeEmpty())
})
It("returns an empty slice of control frames if no applicable control frames are queued", func() {
// overwrite the globally defined packet here
packet := Packet{
PacketNumber: 1337,
Frames: []frames.Frame{streamFrame1, ackFrame1, stopWaitingFrame},
}
controlFrames := packet.GetControlFramesForRetransmission()
Expect(controlFrames).To(BeEmpty())
})
})
})

View file

@ -41,7 +41,7 @@ func NewReceivedPacketHandler() ReceivedPacketHandler {
} }
} }
func (h *receivedPacketHandler) ReceivedPacket(packetNumber protocol.PacketNumber) error { func (h *receivedPacketHandler) ReceivedPacket(packetNumber protocol.PacketNumber, entropyBit bool) error {
if packetNumber == 0 { if packetNumber == 0 {
return errInvalidPacketNumber return errInvalidPacketNumber
} }

View file

@ -21,62 +21,62 @@ var _ = Describe("receivedPacketHandler", func() {
Context("accepting packets", func() { Context("accepting packets", func() {
It("handles a packet that arrives late", func() { It("handles a packet that arrives late", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(1)) err := handler.ReceivedPacket(protocol.PacketNumber(1), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(1))) Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(1)))
err = handler.ReceivedPacket(protocol.PacketNumber(3)) err = handler.ReceivedPacket(protocol.PacketNumber(3), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(3))) Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(3)))
err = handler.ReceivedPacket(protocol.PacketNumber(2)) err = handler.ReceivedPacket(protocol.PacketNumber(2), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(2))) Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(2)))
}) })
It("rejects packets with packet number 0", func() { It("rejects packets with packet number 0", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(0)) err := handler.ReceivedPacket(protocol.PacketNumber(0), false)
Expect(err).To(MatchError(errInvalidPacketNumber)) Expect(err).To(MatchError(errInvalidPacketNumber))
}) })
It("rejects a duplicate package with PacketNumber equal to LargestObserved", func() { It("rejects a duplicate package with PacketNumber equal to LargestObserved", func() {
for i := 1; i < 5; i++ { for i := 1; i < 5; i++ {
err := handler.ReceivedPacket(protocol.PacketNumber(i)) err := handler.ReceivedPacket(protocol.PacketNumber(i), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
err := handler.ReceivedPacket(4) err := handler.ReceivedPacket(4, false)
Expect(err).To(MatchError(ErrDuplicatePacket)) Expect(err).To(MatchError(ErrDuplicatePacket))
}) })
It("rejects a duplicate package with PacketNumber less than the LargestObserved", func() { It("rejects a duplicate package with PacketNumber less than the LargestObserved", func() {
for i := 1; i < 5; i++ { for i := 1; i < 5; i++ {
err := handler.ReceivedPacket(protocol.PacketNumber(i)) err := handler.ReceivedPacket(protocol.PacketNumber(i), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
err := handler.ReceivedPacket(2) err := handler.ReceivedPacket(2, false)
Expect(err).To(MatchError(ErrDuplicatePacket)) Expect(err).To(MatchError(ErrDuplicatePacket))
}) })
It("ignores a packet with PacketNumber less than the LeastUnacked of a previously received StopWaiting", func() { It("ignores a packet with PacketNumber less than the LeastUnacked of a previously received StopWaiting", func() {
err := handler.ReceivedPacket(5) err := handler.ReceivedPacket(5, false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedStopWaiting(&frames.StopWaitingFrame{LeastUnacked: 10}) err = handler.ReceivedStopWaiting(&frames.StopWaitingFrame{LeastUnacked: 10})
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedPacket(9) err = handler.ReceivedPacket(9, false)
Expect(err).To(MatchError(ErrPacketSmallerThanLastStopWaiting)) Expect(err).To(MatchError(ErrPacketSmallerThanLastStopWaiting))
Expect(handler.largestInOrderObserved).To(Equal(protocol.PacketNumber(9))) Expect(handler.largestInOrderObserved).To(Equal(protocol.PacketNumber(9)))
}) })
It("does not ignore a packet with PacketNumber equal to LeastUnacked of a previously received StopWaiting", func() { It("does not ignore a packet with PacketNumber equal to LeastUnacked of a previously received StopWaiting", func() {
err := handler.ReceivedPacket(5) err := handler.ReceivedPacket(5, false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedStopWaiting(&frames.StopWaitingFrame{LeastUnacked: 10}) err = handler.ReceivedStopWaiting(&frames.StopWaitingFrame{LeastUnacked: 10})
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedPacket(10) err = handler.ReceivedPacket(10, false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(handler.largestInOrderObserved).To(Equal(protocol.PacketNumber(10))) Expect(handler.largestInOrderObserved).To(Equal(protocol.PacketNumber(10)))
}) })
It("saves the time when each packet arrived", func() { It("saves the time when each packet arrived", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(3)) err := handler.ReceivedPacket(protocol.PacketNumber(3), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(3))) Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(3)))
Expect(handler.receivedTimes[3]).To(BeTemporally("~", time.Now(), 10*time.Millisecond)) Expect(handler.receivedTimes[3]).To(BeTemporally("~", time.Now(), 10*time.Millisecond))
@ -85,10 +85,10 @@ var _ = Describe("receivedPacketHandler", func() {
It("doesn't store more than MaxTrackedReceivedPackets packets", func() { It("doesn't store more than MaxTrackedReceivedPackets packets", func() {
for i := uint32(0); i < protocol.MaxTrackedReceivedPackets; i++ { for i := uint32(0); i < protocol.MaxTrackedReceivedPackets; i++ {
packetNumber := protocol.PacketNumber(1 + 2*i) packetNumber := protocol.PacketNumber(1 + 2*i)
err := handler.ReceivedPacket(packetNumber) err := handler.ReceivedPacket(packetNumber, false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
err := handler.ReceivedPacket(protocol.PacketNumber(3 * protocol.MaxTrackedReceivedPackets)) err := handler.ReceivedPacket(protocol.PacketNumber(3*protocol.MaxTrackedReceivedPackets), false)
Expect(err).To(MatchError(errTooManyOutstandingReceivedPackets)) Expect(err).To(MatchError(errTooManyOutstandingReceivedPackets))
}) })
}) })
@ -100,7 +100,7 @@ var _ = Describe("receivedPacketHandler", func() {
if i == 10 || i == 11 || i == 12 { if i == 10 || i == 11 || i == 12 {
continue continue
} }
err := handler.ReceivedPacket(protocol.PacketNumber(i)) err := handler.ReceivedPacket(protocol.PacketNumber(i), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
err := handler.ReceivedStopWaiting(&frames.StopWaitingFrame{LeastUnacked: protocol.PacketNumber(12)}) err := handler.ReceivedStopWaiting(&frames.StopWaitingFrame{LeastUnacked: protocol.PacketNumber(12)})
@ -116,7 +116,7 @@ var _ = Describe("receivedPacketHandler", func() {
It("increase the ignorePacketsBelow number, even if all packets below the LeastUnacked were already acked", func() { It("increase the ignorePacketsBelow number, even if all packets below the LeastUnacked were already acked", func() {
for i := 1; i < 20; i++ { for i := 1; i < 20; i++ {
err := handler.ReceivedPacket(protocol.PacketNumber(i)) err := handler.ReceivedPacket(protocol.PacketNumber(i), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
Expect(handler.largestInOrderObserved).To(Equal(protocol.PacketNumber(19))) Expect(handler.largestInOrderObserved).To(Equal(protocol.PacketNumber(19)))
@ -137,9 +137,9 @@ var _ = Describe("receivedPacketHandler", func() {
Context("ACK package generation", func() { Context("ACK package generation", func() {
It("generates a simple ACK frame", func() { It("generates a simple ACK frame", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(1)) err := handler.ReceivedPacket(protocol.PacketNumber(1), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedPacket(protocol.PacketNumber(2)) err = handler.ReceivedPacket(protocol.PacketNumber(2), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ack, err := handler.GetAckFrame(true) ack, err := handler.GetAckFrame(true)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -149,9 +149,9 @@ var _ = Describe("receivedPacketHandler", func() {
}) })
It("generates an ACK frame with missing packets", func() { It("generates an ACK frame with missing packets", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(1)) err := handler.ReceivedPacket(protocol.PacketNumber(1), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedPacket(protocol.PacketNumber(4)) err = handler.ReceivedPacket(protocol.PacketNumber(4), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ack, err := handler.GetAckFrame(true) ack, err := handler.GetAckFrame(true)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -163,9 +163,9 @@ var _ = Describe("receivedPacketHandler", func() {
}) })
It("does not generate an ACK if an ACK has already been sent for the largest Packet", func() { It("does not generate an ACK if an ACK has already been sent for the largest Packet", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(1)) err := handler.ReceivedPacket(protocol.PacketNumber(1), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedPacket(protocol.PacketNumber(2)) err = handler.ReceivedPacket(protocol.PacketNumber(2), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ack, err := handler.GetAckFrame(true) ack, err := handler.GetAckFrame(true)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -176,7 +176,7 @@ var _ = Describe("receivedPacketHandler", func() {
}) })
It("does not dequeue an ACK frame if told so", func() { It("does not dequeue an ACK frame if told so", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(2)) err := handler.ReceivedPacket(protocol.PacketNumber(2), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ack, err := handler.GetAckFrame(false) ack, err := handler.GetAckFrame(false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -190,7 +190,7 @@ var _ = Describe("receivedPacketHandler", func() {
}) })
It("returns a cached ACK frame if the ACK was not dequeued", func() { It("returns a cached ACK frame if the ACK was not dequeued", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(2)) err := handler.ReceivedPacket(protocol.PacketNumber(2), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ack, err := handler.GetAckFrame(false) ack, err := handler.GetAckFrame(false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -202,12 +202,12 @@ var _ = Describe("receivedPacketHandler", func() {
}) })
It("generates a new ACK (and deletes the cached one) when a new packet arrives", func() { It("generates a new ACK (and deletes the cached one) when a new packet arrives", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(1)) err := handler.ReceivedPacket(protocol.PacketNumber(1), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ack, _ := handler.GetAckFrame(true) ack, _ := handler.GetAckFrame(true)
Expect(ack).ToNot(BeNil()) Expect(ack).ToNot(BeNil())
Expect(ack.LargestAcked).To(Equal(protocol.PacketNumber(1))) Expect(ack.LargestAcked).To(Equal(protocol.PacketNumber(1)))
err = handler.ReceivedPacket(protocol.PacketNumber(3)) err = handler.ReceivedPacket(protocol.PacketNumber(3), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ack, _ = handler.GetAckFrame(true) ack, _ = handler.GetAckFrame(true)
Expect(ack).ToNot(BeNil()) Expect(ack).ToNot(BeNil())
@ -215,14 +215,14 @@ var _ = Describe("receivedPacketHandler", func() {
}) })
It("generates a new ACK when an out-of-order packet arrives", func() { It("generates a new ACK when an out-of-order packet arrives", func() {
err := handler.ReceivedPacket(protocol.PacketNumber(1)) err := handler.ReceivedPacket(protocol.PacketNumber(1), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedPacket(protocol.PacketNumber(3)) err = handler.ReceivedPacket(protocol.PacketNumber(3), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ack, _ := handler.GetAckFrame(true) ack, _ := handler.GetAckFrame(true)
Expect(ack).ToNot(BeNil()) Expect(ack).ToNot(BeNil())
Expect(ack.AckRanges).To(HaveLen(2)) Expect(ack.AckRanges).To(HaveLen(2))
err = handler.ReceivedPacket(protocol.PacketNumber(2)) err = handler.ReceivedPacket(protocol.PacketNumber(2), false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ack, _ = handler.GetAckFrame(true) ack, _ = handler.GetAckFrame(true)
Expect(ack).ToNot(BeNil()) Expect(ack).ToNot(BeNil())
@ -230,13 +230,13 @@ var _ = Describe("receivedPacketHandler", func() {
}) })
It("does send old ACK ranges after receiving a StopWaiting", func() { It("does send old ACK ranges after receiving a StopWaiting", func() {
err := handler.ReceivedPacket(5) err := handler.ReceivedPacket(5, false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedPacket(10) err = handler.ReceivedPacket(10, false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedPacket(11) err = handler.ReceivedPacket(11, false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedPacket(12) err = handler.ReceivedPacket(12, false)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.ReceivedStopWaiting(&frames.StopWaitingFrame{LeastUnacked: protocol.PacketNumber(11)}) err = handler.ReceivedStopWaiting(&frames.StopWaitingFrame{LeastUnacked: protocol.PacketNumber(11)})
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -250,18 +250,18 @@ var _ = Describe("receivedPacketHandler", func() {
Context("Garbage Collector", func() { Context("Garbage Collector", func() {
PIt("only keeps packets with packet numbers higher than the highestInOrderObserved in packetHistory", func() { PIt("only keeps packets with packet numbers higher than the highestInOrderObserved in packetHistory", func() {
handler.ReceivedPacket(1) handler.ReceivedPacket(1, false)
handler.ReceivedPacket(2) handler.ReceivedPacket(2, false)
handler.ReceivedPacket(4) handler.ReceivedPacket(4, false)
Expect(handler.receivedTimes).ToNot(HaveKey(protocol.PacketNumber(1))) Expect(handler.receivedTimes).ToNot(HaveKey(protocol.PacketNumber(1)))
Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(2))) Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(2)))
Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(4))) Expect(handler.receivedTimes).To(HaveKey(protocol.PacketNumber(4)))
}) })
It("garbage collects packetHistory after receiving a StopWaiting", func() { It("garbage collects packetHistory after receiving a StopWaiting", func() {
handler.ReceivedPacket(1) handler.ReceivedPacket(1, false)
handler.ReceivedPacket(2) handler.ReceivedPacket(2, false)
handler.ReceivedPacket(4) handler.ReceivedPacket(4, false)
swf := frames.StopWaitingFrame{LeastUnacked: 4} swf := frames.StopWaitingFrame{LeastUnacked: 4}
handler.ReceivedStopWaiting(&swf) handler.ReceivedStopWaiting(&swf)
Expect(handler.receivedTimes).ToNot(HaveKey(protocol.PacketNumber(1))) Expect(handler.receivedTimes).ToNot(HaveKey(protocol.PacketNumber(1)))

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"time" "time"
"github.com/lucas-clemente/quic-go/ackhandlerlegacy"
"github.com/lucas-clemente/quic-go/congestion" "github.com/lucas-clemente/quic-go/congestion"
"github.com/lucas-clemente/quic-go/frames" "github.com/lucas-clemente/quic-go/frames"
"github.com/lucas-clemente/quic-go/protocol" "github.com/lucas-clemente/quic-go/protocol"
@ -34,9 +35,9 @@ type sentPacketHandler struct {
largestReceivedPacketWithAck protocol.PacketNumber largestReceivedPacketWithAck protocol.PacketNumber
// TODO: Move into separate class as in chromium // TODO: Move into separate class as in chromium
packetHistory map[protocol.PacketNumber]*Packet packetHistory map[protocol.PacketNumber]*ackhandlerlegacy.Packet
retransmissionQueue []*Packet retransmissionQueue []*ackhandlerlegacy.Packet
stopWaitingManager StopWaitingManager stopWaitingManager StopWaitingManager
bytesInFlight protocol.ByteCount bytesInFlight protocol.ByteCount
@ -58,14 +59,14 @@ func NewSentPacketHandler(stopWaitingManager StopWaitingManager) SentPacketHandl
) )
return &sentPacketHandler{ return &sentPacketHandler{
packetHistory: make(map[protocol.PacketNumber]*Packet), packetHistory: make(map[protocol.PacketNumber]*ackhandlerlegacy.Packet),
stopWaitingManager: stopWaitingManager, stopWaitingManager: stopWaitingManager,
rttStats: rttStats, rttStats: rttStats,
congestion: congestion, congestion: congestion,
} }
} }
func (h *sentPacketHandler) ackPacket(packetNumber protocol.PacketNumber) *Packet { func (h *sentPacketHandler) ackPacket(packetNumber protocol.PacketNumber) *ackhandlerlegacy.Packet {
packet, ok := h.packetHistory[packetNumber] packet, ok := h.packetHistory[packetNumber]
if ok && !packet.Retransmitted { if ok && !packet.Retransmitted {
h.bytesInFlight -= packet.Length h.bytesInFlight -= packet.Length
@ -82,7 +83,7 @@ func (h *sentPacketHandler) ackPacket(packetNumber protocol.PacketNumber) *Packe
return packet return packet
} }
func (h *sentPacketHandler) nackPacket(packetNumber protocol.PacketNumber) (*Packet, error) { func (h *sentPacketHandler) nackPacket(packetNumber protocol.PacketNumber) (*ackhandlerlegacy.Packet, error) {
packet, ok := h.packetHistory[packetNumber] packet, ok := h.packetHistory[packetNumber]
// This means that the packet has already been retransmitted, do nothing. // This means that the packet has already been retransmitted, do nothing.
// We're probably only receiving another NACK for this packet because the // We're probably only receiving another NACK for this packet because the
@ -100,7 +101,7 @@ func (h *sentPacketHandler) nackPacket(packetNumber protocol.PacketNumber) (*Pac
return nil, nil return nil, nil
} }
func (h *sentPacketHandler) queuePacketForRetransmission(packet *Packet) { func (h *sentPacketHandler) queuePacketForRetransmission(packet *ackhandlerlegacy.Packet) {
h.bytesInFlight -= packet.Length h.bytesInFlight -= packet.Length
h.retransmissionQueue = append(h.retransmissionQueue, packet) h.retransmissionQueue = append(h.retransmissionQueue, packet)
packet.Retransmitted = true packet.Retransmitted = true
@ -118,7 +119,7 @@ func (h *sentPacketHandler) queuePacketForRetransmission(packet *Packet) {
} }
} }
func (h *sentPacketHandler) SentPacket(packet *Packet) error { func (h *sentPacketHandler) SentPacket(packet *ackhandlerlegacy.Packet) error {
_, ok := h.packetHistory[packet.PacketNumber] _, ok := h.packetHistory[packet.PacketNumber]
if ok { if ok {
return errDuplicatePacketNumber return errDuplicatePacketNumber
@ -126,7 +127,7 @@ func (h *sentPacketHandler) SentPacket(packet *Packet) error {
now := time.Now() now := time.Now()
h.lastSentPacketTime = now h.lastSentPacketTime = now
packet.sendTime = now packet.SendTime = now
if packet.Length == 0 { if packet.Length == 0 {
return errors.New("SentPacketHandler: packet cannot be empty") return errors.New("SentPacketHandler: packet cannot be empty")
} }
@ -169,7 +170,7 @@ func (h *sentPacketHandler) ReceivedAck(ackFrame *frames.AckFrame, withPacketNum
packet, ok := h.packetHistory[h.LargestAcked] packet, ok := h.packetHistory[h.LargestAcked]
if ok { if ok {
// Update the RTT // Update the RTT
timeDelta := time.Now().Sub(packet.sendTime) timeDelta := time.Now().Sub(packet.SendTime)
// TODO: Don't always update RTT // TODO: Don't always update RTT
h.rttStats.UpdateRTT(timeDelta, ackFrame.DelayTime, time.Now()) h.rttStats.UpdateRTT(timeDelta, ackFrame.DelayTime, time.Now())
if utils.Debug() { if utils.Debug() {
@ -242,7 +243,7 @@ func (h *sentPacketHandler) ProbablyHasPacketForRetransmission() bool {
return len(h.retransmissionQueue) > 0 return len(h.retransmissionQueue) > 0
} }
func (h *sentPacketHandler) DequeuePacketForRetransmission() (packet *Packet) { func (h *sentPacketHandler) DequeuePacketForRetransmission() (packet *ackhandlerlegacy.Packet) {
if !h.ProbablyHasPacketForRetransmission() { if !h.ProbablyHasPacketForRetransmission() {
return nil return nil
} }

View file

@ -3,6 +3,7 @@ package ackhandler
import ( import (
"time" "time"
"github.com/lucas-clemente/quic-go/ackhandlerlegacy"
"github.com/lucas-clemente/quic-go/congestion" "github.com/lucas-clemente/quic-go/congestion"
"github.com/lucas-clemente/quic-go/frames" "github.com/lucas-clemente/quic-go/frames"
"github.com/lucas-clemente/quic-go/protocol" "github.com/lucas-clemente/quic-go/protocol"
@ -54,8 +55,10 @@ type mockStopWaiting struct {
receivedAckForPacketNumber protocol.PacketNumber receivedAckForPacketNumber protocol.PacketNumber
} }
func (m *mockStopWaiting) RegisterPacketForRetransmission(packet *Packet) { panic("not implemented") } func (m *mockStopWaiting) RegisterPacketForRetransmission(packet *ackhandlerlegacy.Packet) {
func (m *mockStopWaiting) GetStopWaitingFrame() *frames.StopWaitingFrame { panic("not implemented") } panic("not implemented")
}
func (m *mockStopWaiting) GetStopWaitingFrame() *frames.StopWaitingFrame { panic("not implemented") }
func (m *mockStopWaiting) SentStopWaitingWithPacket(packetNumber protocol.PacketNumber) { func (m *mockStopWaiting) SentStopWaitingWithPacket(packetNumber protocol.PacketNumber) {
panic("not implemented") panic("not implemented")
} }
@ -90,8 +93,8 @@ var _ = Describe("SentPacketHandler", func() {
Context("registering sent packets", func() { Context("registering sent packets", func() {
It("accepts two consecutive packets", func() { It("accepts two consecutive packets", func() {
packet1 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1} packet1 := ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1}
packet2 := Packet{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, Length: 2} packet2 := ackhandlerlegacy.Packet{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, Length: 2}
err := handler.SentPacket(&packet1) err := handler.SentPacket(&packet1)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.SentPacket(&packet2) err = handler.SentPacket(&packet2)
@ -105,8 +108,8 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("rejects packets with the same packet number", func() { It("rejects packets with the same packet number", func() {
packet1 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1} packet1 := ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1}
packet2 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 2} packet2 := ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 2}
err := handler.SentPacket(&packet1) err := handler.SentPacket(&packet1)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.SentPacket(&packet2) err = handler.SentPacket(&packet2)
@ -117,8 +120,8 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("works with non-consecutive packet numbers", func() { It("works with non-consecutive packet numbers", func() {
packet1 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1} packet1 := ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1}
packet2 := Packet{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, Length: 2} packet2 := ackhandlerlegacy.Packet{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, Length: 2}
err := handler.SentPacket(&packet1) err := handler.SentPacket(&packet1)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.SentPacket(&packet2) err = handler.SentPacket(&packet2)
@ -131,14 +134,14 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("stores the sent time", func() { It("stores the sent time", func() {
packet := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1} packet := ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1}
err := handler.SentPacket(&packet) err := handler.SentPacket(&packet)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(handler.packetHistory[1].sendTime.Unix()).To(BeNumerically("~", time.Now().Unix(), 1)) Expect(handler.packetHistory[1].SendTime.Unix()).To(BeNumerically("~", time.Now().Unix(), 1))
}) })
It("updates the last sent time", func() { It("updates the last sent time", func() {
packet := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1} packet := ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1}
err := handler.SentPacket(&packet) err := handler.SentPacket(&packet)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(handler.lastSentPacketTime.Unix()).To(BeNumerically("~", time.Now().Unix(), 1)) Expect(handler.lastSentPacketTime.Unix()).To(BeNumerically("~", time.Now().Unix(), 1))
@ -148,7 +151,7 @@ var _ = Describe("SentPacketHandler", func() {
Context("DOS mitigation", func() { Context("DOS mitigation", func() {
It("checks the size of the packet history, for unacked packets", func() { It("checks the size of the packet history, for unacked packets", func() {
for i := uint32(1); i < protocol.MaxTrackedSentPackets+10; i++ { for i := uint32(1); i < protocol.MaxTrackedSentPackets+10; i++ {
packet := Packet{PacketNumber: protocol.PacketNumber(i), Frames: []frames.Frame{&streamFrame}, Length: 1} packet := ackhandlerlegacy.Packet{PacketNumber: protocol.PacketNumber(i), Frames: []frames.Frame{&streamFrame}, Length: 1}
err := handler.SentPacket(&packet) err := handler.SentPacket(&packet)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
@ -160,10 +163,10 @@ var _ = Describe("SentPacketHandler", func() {
}) })
Context("ACK processing", func() { Context("ACK processing", func() {
var packets []*Packet var packets []*ackhandlerlegacy.Packet
BeforeEach(func() { BeforeEach(func() {
packets = []*Packet{ packets = []*ackhandlerlegacy.Packet{
{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1}, {PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1},
{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, Length: 1}, {PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, Length: 1},
{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, Length: 1}, {PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, Length: 1},
@ -372,9 +375,9 @@ var _ = Describe("SentPacketHandler", func() {
It("calculates the RTT", func() { It("calculates the RTT", func() {
now := time.Now() now := time.Now()
// First, fake the sent times of the first, second and last packet // First, fake the sent times of the first, second and last packet
handler.packetHistory[1].sendTime = now.Add(-10 * time.Minute) handler.packetHistory[1].SendTime = now.Add(-10 * time.Minute)
handler.packetHistory[2].sendTime = now.Add(-5 * time.Minute) handler.packetHistory[2].SendTime = now.Add(-5 * time.Minute)
handler.packetHistory[6].sendTime = now.Add(-1 * time.Minute) handler.packetHistory[6].SendTime = now.Add(-1 * time.Minute)
// Now, check that the proper times are used when calculating the deltas // Now, check that the proper times are used when calculating the deltas
err := handler.ReceivedAck(&frames.AckFrame{LargestAcked: 1}, 1) err := handler.ReceivedAck(&frames.AckFrame{LargestAcked: 1}, 1)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -389,7 +392,7 @@ var _ = Describe("SentPacketHandler", func() {
It("uses the DelayTime in the ack frame", func() { It("uses the DelayTime in the ack frame", func() {
now := time.Now() now := time.Now()
handler.packetHistory[1].sendTime = now.Add(-10 * time.Minute) handler.packetHistory[1].SendTime = now.Add(-10 * time.Minute)
err := handler.ReceivedAck(&frames.AckFrame{LargestAcked: 1, DelayTime: 5 * time.Minute}, 1) err := handler.ReceivedAck(&frames.AckFrame{LargestAcked: 1, DelayTime: 5 * time.Minute}, 1)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 5*time.Minute, 1*time.Second)) Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 5*time.Minute, 1*time.Second))
@ -398,10 +401,10 @@ var _ = Describe("SentPacketHandler", func() {
}) })
Context("Retransmission handler", func() { Context("Retransmission handler", func() {
var packets []*Packet var packets []*ackhandlerlegacy.Packet
BeforeEach(func() { BeforeEach(func() {
packets = []*Packet{ packets = []*ackhandlerlegacy.Packet{
{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1}, {PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1},
{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, Length: 1}, {PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, Length: 1},
{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, Length: 1}, {PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, Length: 1},
@ -565,9 +568,9 @@ var _ = Describe("SentPacketHandler", func() {
Context("calculating bytes in flight", func() { Context("calculating bytes in flight", func() {
It("works in a typical retransmission scenarios", func() { It("works in a typical retransmission scenarios", func() {
packet1 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1} packet1 := ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, Length: 1}
packet2 := Packet{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, Length: 2} packet2 := ackhandlerlegacy.Packet{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, Length: 2}
packet3 := Packet{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, Length: 3} packet3 := ackhandlerlegacy.Packet{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, Length: 3}
err := handler.SentPacket(&packet1) err := handler.SentPacket(&packet1)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
err = handler.SentPacket(&packet2) err = handler.SentPacket(&packet2)
@ -597,7 +600,7 @@ var _ = Describe("SentPacketHandler", func() {
Expect(handler.BytesInFlight()).To(Equal(protocol.ByteCount(0))) Expect(handler.BytesInFlight()).To(Equal(protocol.ByteCount(0)))
// Retransmission // Retransmission
packet4 := Packet{PacketNumber: 4, Length: 2} packet4 := ackhandlerlegacy.Packet{PacketNumber: 4, Length: 2}
err = handler.SentPacket(&packet4) err = handler.SentPacket(&packet4)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(handler.BytesInFlight()).To(Equal(protocol.ByteCount(2))) Expect(handler.BytesInFlight()).To(Equal(protocol.ByteCount(2)))
@ -624,7 +627,7 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("should call OnSent", func() { It("should call OnSent", func() {
p := &Packet{ p := &ackhandlerlegacy.Packet{
PacketNumber: 1, PacketNumber: 1,
Frames: []frames.Frame{&frames.StreamFrame{StreamID: 5}}, Frames: []frames.Frame{&frames.StreamFrame{StreamID: 5}},
Length: 42, Length: 42,
@ -639,9 +642,9 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("should call OnCongestionEvent", func() { It("should call OnCongestionEvent", func() {
handler.SentPacket(&Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1}) handler.SentPacket(&ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1})
handler.SentPacket(&Packet{PacketNumber: 2, Frames: []frames.Frame{}, Length: 2}) handler.SentPacket(&ackhandlerlegacy.Packet{PacketNumber: 2, Frames: []frames.Frame{}, Length: 2})
handler.SentPacket(&Packet{PacketNumber: 3, Frames: []frames.Frame{}, Length: 3}) handler.SentPacket(&ackhandlerlegacy.Packet{PacketNumber: 3, Frames: []frames.Frame{}, Length: 3})
ack := frames.AckFrame{ ack := frames.AckFrame{
LargestAcked: 3, LargestAcked: 3,
LowestAcked: 1, LowestAcked: 1,
@ -663,7 +666,7 @@ var _ = Describe("SentPacketHandler", func() {
var packetNumber protocol.PacketNumber var packetNumber protocol.PacketNumber
for i := uint8(0); i < protocol.RetransmissionThreshold; i++ { for i := uint8(0); i < protocol.RetransmissionThreshold; i++ {
packetNumber = protocol.PacketNumber(4 + i) packetNumber = protocol.PacketNumber(4 + i)
handler.SentPacket(&Packet{PacketNumber: packetNumber, Frames: []frames.Frame{}, Length: protocol.ByteCount(packetNumber)}) handler.SentPacket(&ackhandlerlegacy.Packet{PacketNumber: packetNumber, Frames: []frames.Frame{}, Length: protocol.ByteCount(packetNumber)})
ack := frames.AckFrame{ ack := frames.AckFrame{
LargestAcked: packetNumber, LargestAcked: packetNumber,
LowestAcked: 1, LowestAcked: 1,
@ -682,13 +685,13 @@ var _ = Describe("SentPacketHandler", func() {
It("allows or denies sending", func() { It("allows or denies sending", func() {
Expect(handler.CongestionAllowsSending()).To(BeTrue()) Expect(handler.CongestionAllowsSending()).To(BeTrue())
err := handler.SentPacket(&Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: protocol.DefaultTCPMSS + 1}) err := handler.SentPacket(&ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: protocol.DefaultTCPMSS + 1})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(handler.CongestionAllowsSending()).To(BeFalse()) Expect(handler.CongestionAllowsSending()).To(BeFalse())
}) })
It("should call OnRetransmissionTimeout", func() { It("should call OnRetransmissionTimeout", func() {
err := handler.SentPacket(&Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1}) err := handler.SentPacket(&ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
handler.lastSentPacketTime = time.Now().Add(-time.Second) handler.lastSentPacketTime = time.Now().Add(-time.Second)
handler.maybeQueuePacketsRTO() handler.maybeQueuePacketsRTO()
@ -728,7 +731,7 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("returns time to RTO", func() { It("returns time to RTO", func() {
err := handler.SentPacket(&Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1}) err := handler.SentPacket(&ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(handler.TimeOfFirstRTO().Sub(time.Now())).To(BeNumerically("~", protocol.DefaultRetransmissionTime, time.Millisecond)) Expect(handler.TimeOfFirstRTO().Sub(time.Now())).To(BeNumerically("~", protocol.DefaultRetransmissionTime, time.Millisecond))
}) })
@ -742,14 +745,14 @@ var _ = Describe("SentPacketHandler", func() {
Context("queuing packets due to RTO", func() { Context("queuing packets due to RTO", func() {
It("does nothing if not required", func() { It("does nothing if not required", func() {
err := handler.SentPacket(&Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1}) err := handler.SentPacket(&ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
handler.maybeQueuePacketsRTO() handler.maybeQueuePacketsRTO()
Expect(handler.retransmissionQueue).To(BeEmpty()) Expect(handler.retransmissionQueue).To(BeEmpty())
}) })
It("queues a packet if RTO expired", func() { It("queues a packet if RTO expired", func() {
p := &Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1} p := &ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1}
err := handler.SentPacket(p) err := handler.SentPacket(p)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
handler.lastSentPacketTime = time.Now().Add(-time.Second) handler.lastSentPacketTime = time.Now().Add(-time.Second)
@ -759,7 +762,7 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("does not queue retransmittedpackets", func() { It("does not queue retransmittedpackets", func() {
p := &Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1, Retransmitted: true} p := &ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1, Retransmitted: true}
err := handler.SentPacket(p) err := handler.SentPacket(p)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
handler.lastSentPacketTime = time.Now().Add(-time.Second) handler.lastSentPacketTime = time.Now().Add(-time.Second)
@ -775,7 +778,7 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("works with HasPacketForRetransmission", func() { It("works with HasPacketForRetransmission", func() {
p := &Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1} p := &ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1}
err := handler.SentPacket(p) err := handler.SentPacket(p)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
handler.lastSentPacketTime = time.Now().Add(-time.Second) handler.lastSentPacketTime = time.Now().Add(-time.Second)
@ -783,7 +786,7 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("works with DequeuePacketForRetransmission", func() { It("works with DequeuePacketForRetransmission", func() {
p := &Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1} p := &ackhandlerlegacy.Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1}
err := handler.SentPacket(p) err := handler.SentPacket(p)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
handler.lastSentPacketTime = time.Now().Add(-time.Second) handler.lastSentPacketTime = time.Now().Add(-time.Second)

View file

@ -1,6 +1,7 @@
package ackhandler package ackhandler
import ( import (
"github.com/lucas-clemente/quic-go/ackhandlerlegacy"
"github.com/lucas-clemente/quic-go/frames" "github.com/lucas-clemente/quic-go/frames"
"github.com/lucas-clemente/quic-go/protocol" "github.com/lucas-clemente/quic-go/protocol"
) )
@ -22,7 +23,7 @@ func NewStopWaitingManager() StopWaitingManager {
} }
// RegisterPacketForRetransmission prepares the StopWaitingFrame, if necessary // RegisterPacketForRetransmission prepares the StopWaitingFrame, if necessary
func (h *stopWaitingManager) RegisterPacketForRetransmission(packet *Packet) { func (h *stopWaitingManager) RegisterPacketForRetransmission(packet *ackhandlerlegacy.Packet) {
// out-of-order retransmission. A StopWaitingFrame with a higher LeastUnacked was already queued (or sent in the past), no need to send another one again // out-of-order retransmission. A StopWaitingFrame with a higher LeastUnacked was already queued (or sent in the past), no need to send another one again
if packet.PacketNumber < h.maxRetransmittedPacketNumber { if packet.PacketNumber < h.maxRetransmittedPacketNumber {
return return

View file

@ -1,6 +1,7 @@
package ackhandler package ackhandler
import ( import (
"github.com/lucas-clemente/quic-go/ackhandlerlegacy"
"github.com/lucas-clemente/quic-go/protocol" "github.com/lucas-clemente/quic-go/protocol"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -18,7 +19,7 @@ var _ = Describe("StopWaitingManager", func() {
It("gets a StopWaitingFrame after a packet has been registered for retransmission", func() { It("gets a StopWaitingFrame after a packet has been registered for retransmission", func() {
leastUnacked := protocol.PacketNumber(10) leastUnacked := protocol.PacketNumber(10)
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: leastUnacked}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: leastUnacked})
swf := manager.GetStopWaitingFrame() swf := manager.GetStopWaitingFrame()
Expect(swf).ToNot(BeNil()) Expect(swf).ToNot(BeNil())
Expect(swf.LeastUnacked).To(Equal(leastUnacked + 1)) Expect(swf.LeastUnacked).To(Equal(leastUnacked + 1))
@ -26,8 +27,8 @@ var _ = Describe("StopWaitingManager", func() {
It("always gets the StopWaitingFrame for the highest retransmitted packet number", func() { It("always gets the StopWaitingFrame for the highest retransmitted packet number", func() {
leastUnacked := protocol.PacketNumber(10) leastUnacked := protocol.PacketNumber(10)
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: leastUnacked}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: leastUnacked})
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: leastUnacked - 1}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: leastUnacked - 1})
swf := manager.GetStopWaitingFrame() swf := manager.GetStopWaitingFrame()
Expect(swf).ToNot(BeNil()) Expect(swf).ToNot(BeNil())
Expect(swf.LeastUnacked).To(Equal(leastUnacked + 1)) Expect(swf.LeastUnacked).To(Equal(leastUnacked + 1))
@ -35,8 +36,8 @@ var _ = Describe("StopWaitingManager", func() {
It("updates the StopWaitingFrame when a packet with a higher packet number is retransmitted", func() { It("updates the StopWaitingFrame when a packet with a higher packet number is retransmitted", func() {
leastUnacked := protocol.PacketNumber(10) leastUnacked := protocol.PacketNumber(10)
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: leastUnacked - 1}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: leastUnacked - 1})
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: leastUnacked}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: leastUnacked})
swf := manager.GetStopWaitingFrame() swf := manager.GetStopWaitingFrame()
Expect(swf).ToNot(BeNil()) Expect(swf).ToNot(BeNil())
Expect(swf.LeastUnacked).To(Equal(leastUnacked + 1)) Expect(swf.LeastUnacked).To(Equal(leastUnacked + 1))
@ -44,17 +45,17 @@ var _ = Describe("StopWaitingManager", func() {
It("does not create a new StopWaitingFrame for an out-of-order retransmission", func() { It("does not create a new StopWaitingFrame for an out-of-order retransmission", func() {
leastUnacked := protocol.PacketNumber(10) leastUnacked := protocol.PacketNumber(10)
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: leastUnacked}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: leastUnacked})
manager.SentStopWaitingWithPacket(12) manager.SentStopWaitingWithPacket(12)
manager.ReceivedAckForPacketNumber(12) manager.ReceivedAckForPacketNumber(12)
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: leastUnacked - 1}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: leastUnacked - 1})
swf := manager.GetStopWaitingFrame() swf := manager.GetStopWaitingFrame()
Expect(swf).To(BeNil()) Expect(swf).To(BeNil())
}) })
Context("ACK handling", func() { Context("ACK handling", func() {
It("removes the current StopWaitingFrame when the first packet it was sent with is ACKed", func() { It("removes the current StopWaitingFrame when the first packet it was sent with is ACKed", func() {
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: 10}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: 10})
manager.SentStopWaitingWithPacket(13) manager.SentStopWaitingWithPacket(13)
manager.SentStopWaitingWithPacket(14) manager.SentStopWaitingWithPacket(14)
manager.SentStopWaitingWithPacket(15) manager.SentStopWaitingWithPacket(15)
@ -64,7 +65,7 @@ var _ = Describe("StopWaitingManager", func() {
}) })
It("removes the current StopWaitingFrame when any packet it was sent with is ACKed", func() { It("removes the current StopWaitingFrame when any packet it was sent with is ACKed", func() {
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: 10}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: 10})
manager.SentStopWaitingWithPacket(13) manager.SentStopWaitingWithPacket(13)
manager.SentStopWaitingWithPacket(14) manager.SentStopWaitingWithPacket(14)
manager.SentStopWaitingWithPacket(15) manager.SentStopWaitingWithPacket(15)
@ -74,7 +75,7 @@ var _ = Describe("StopWaitingManager", func() {
}) })
It("does not remove the current StopWaitingFrame when a packet before the one containing the StopWaitingFrame is ACKed", func() { It("does not remove the current StopWaitingFrame when a packet before the one containing the StopWaitingFrame is ACKed", func() {
manager.RegisterPacketForRetransmission(&Packet{PacketNumber: 10}) manager.RegisterPacketForRetransmission(&ackhandlerlegacy.Packet{PacketNumber: 10})
manager.SentStopWaitingWithPacket(13) manager.SentStopWaitingWithPacket(13)
Expect(manager.GetStopWaitingFrame()).ToNot(BeNil()) Expect(manager.GetStopWaitingFrame()).ToNot(BeNil())
manager.ReceivedAckForPacketNumber(12) manager.ReceivedAckForPacketNumber(12)

View file

@ -16,7 +16,7 @@ type SentPacketHandler interface {
DequeuePacketForRetransmission() (packet *Packet) DequeuePacketForRetransmission() (packet *Packet)
BytesInFlight() protocol.ByteCount BytesInFlight() protocol.ByteCount
GetLargestObserved() protocol.PacketNumber GetLargestAcked() protocol.PacketNumber
CongestionAllowsSending() bool CongestionAllowsSending() bool
CheckForError() error CheckForError() error

View file

@ -1,5 +1,7 @@
package ackhandlerlegacy package ackhandlerlegacy
// TODO: move to ackhandler once we remove support for QUIC 33
import ( import (
"time" "time"
@ -18,7 +20,7 @@ type Packet struct {
MissingReports uint8 MissingReports uint8
Retransmitted bool // has this Packet ever been retransmitted Retransmitted bool // has this Packet ever been retransmitted
sendTime time.Time SendTime time.Time
} }
// GetStreamFramesForRetransmission gets all the streamframes for retransmission // GetStreamFramesForRetransmission gets all the streamframes for retransmission

View file

@ -120,7 +120,7 @@ func (h *sentPacketHandler) SentPacket(packet *Packet) error {
} }
now := time.Now() now := time.Now()
h.lastSentPacketTime = now h.lastSentPacketTime = now
packet.sendTime = now packet.SendTime = now
if packet.Length == 0 { if packet.Length == 0 {
return errors.New("SentPacketHandler: packet cannot be empty") return errors.New("SentPacketHandler: packet cannot be empty")
} }
@ -214,7 +214,7 @@ func (h *sentPacketHandler) ReceivedAck(ackFrameOrig *frames.AckFrame, withPacke
h.LargestObservedEntropy = packet.Entropy h.LargestObservedEntropy = packet.Entropy
// Update the RTT // Update the RTT
timeDelta := time.Now().Sub(packet.sendTime) timeDelta := time.Now().Sub(packet.SendTime)
// TODO: Don't always update RTT // TODO: Don't always update RTT
h.rttStats.UpdateRTT(timeDelta, ackFrame.DelayTime, time.Now()) h.rttStats.UpdateRTT(timeDelta, ackFrame.DelayTime, time.Now())
if utils.Debug() { if utils.Debug() {
@ -308,7 +308,7 @@ func (h *sentPacketHandler) BytesInFlight() protocol.ByteCount {
return h.bytesInFlight return h.bytesInFlight
} }
func (h *sentPacketHandler) GetLargestObserved() protocol.PacketNumber { func (h *sentPacketHandler) GetLargestAcked() protocol.PacketNumber {
return h.LargestObserved return h.LargestObserved
} }

View file

@ -159,7 +159,7 @@ var _ = Describe("SentPacketHandler", func() {
packet := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: true, Length: 1} packet := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: true, Length: 1}
err := handler.SentPacket(&packet) err := handler.SentPacket(&packet)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(handler.packetHistory[1].sendTime.Unix()).To(BeNumerically("~", time.Now().Unix(), 1)) Expect(handler.packetHistory[1].SendTime.Unix()).To(BeNumerically("~", time.Now().Unix(), 1))
}) })
It("updates the last sent time", func() { It("updates the last sent time", func() {
@ -458,9 +458,9 @@ var _ = Describe("SentPacketHandler", func() {
It("calculates the RTT", func() { It("calculates the RTT", func() {
now := time.Now() now := time.Now()
// First, fake the sent times of the first, second and last packet // First, fake the sent times of the first, second and last packet
handler.packetHistory[1].sendTime = now.Add(-10 * time.Minute) handler.packetHistory[1].SendTime = now.Add(-10 * time.Minute)
handler.packetHistory[2].sendTime = now.Add(-5 * time.Minute) handler.packetHistory[2].SendTime = now.Add(-5 * time.Minute)
handler.packetHistory[6].sendTime = now.Add(-1 * time.Minute) handler.packetHistory[6].SendTime = now.Add(-1 * time.Minute)
// Now, check that the proper times are used when calculating the deltas // Now, check that the proper times are used when calculating the deltas
ack := frames.AckFrameLegacy{LargestObserved: 1} ack := frames.AckFrameLegacy{LargestObserved: 1}
err := handler.ReceivedAck(&frames.AckFrame{AckFrameLegacy: &ack}, 1) err := handler.ReceivedAck(&frames.AckFrame{AckFrameLegacy: &ack}, 1)
@ -478,7 +478,7 @@ var _ = Describe("SentPacketHandler", func() {
It("uses the DelayTime in the ack frame", func() { It("uses the DelayTime in the ack frame", func() {
now := time.Now() now := time.Now()
handler.packetHistory[1].sendTime = now.Add(-10 * time.Minute) handler.packetHistory[1].SendTime = now.Add(-10 * time.Minute)
ack := frames.AckFrameLegacy{ ack := frames.AckFrameLegacy{
LargestObserved: 1, LargestObserved: 1,
DelayTime: 5 * time.Minute, DelayTime: 5 * time.Minute,

View file

@ -8,6 +8,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/lucas-clemente/quic-go/ackhandler"
"github.com/lucas-clemente/quic-go/ackhandlerlegacy" "github.com/lucas-clemente/quic-go/ackhandlerlegacy"
"github.com/lucas-clemente/quic-go/flowcontrol" "github.com/lucas-clemente/quic-go/flowcontrol"
"github.com/lucas-clemente/quic-go/frames" "github.com/lucas-clemente/quic-go/frames"
@ -53,9 +54,9 @@ type Session struct {
openStreamsCount uint32 openStreamsCount uint32
streamsMutex sync.RWMutex streamsMutex sync.RWMutex
sentPacketHandler ackhandlerlegacy.SentPacketHandler sentPacketHandler ackhandler.SentPacketHandler
receivedPacketHandler ackhandlerlegacy.ReceivedPacketHandler receivedPacketHandler ackhandler.ReceivedPacketHandler
stopWaitingManager ackhandlerlegacy.StopWaitingManager stopWaitingManager ackhandler.StopWaitingManager
streamFramer *streamFramer streamFramer *streamFramer
flowControlManager flowcontrol.FlowControlManager flowControlManager flowcontrol.FlowControlManager
@ -92,10 +93,23 @@ type Session struct {
// newSession makes a new session // newSession makes a new session
func newSession(conn connection, v protocol.VersionNumber, connectionID protocol.ConnectionID, sCfg *handshake.ServerConfig, streamCallback StreamCallback, closeCallback closeCallback) (packetHandler, error) { func newSession(conn connection, v protocol.VersionNumber, connectionID protocol.ConnectionID, sCfg *handshake.ServerConfig, streamCallback StreamCallback, closeCallback closeCallback) (packetHandler, error) {
stopWaitingManager := ackhandlerlegacy.NewStopWaitingManager()
connectionParametersManager := handshake.NewConnectionParamatersManager() connectionParametersManager := handshake.NewConnectionParamatersManager()
flowControlManager := flowcontrol.NewFlowControlManager(connectionParametersManager) flowControlManager := flowcontrol.NewFlowControlManager(connectionParametersManager)
var stopWaitingManager ackhandler.StopWaitingManager
var sentPacketHandler ackhandler.SentPacketHandler
var receivedPacketHandler ackhandler.ReceivedPacketHandler
if v <= protocol.Version33 {
stopWaitingManager = ackhandlerlegacy.NewStopWaitingManager().(ackhandler.StopWaitingManager)
sentPacketHandler = ackhandlerlegacy.NewSentPacketHandler(stopWaitingManager).(ackhandler.SentPacketHandler)
receivedPacketHandler = ackhandlerlegacy.NewReceivedPacketHandler().(ackhandler.ReceivedPacketHandler)
} else {
stopWaitingManager = ackhandler.NewStopWaitingManager()
sentPacketHandler = ackhandler.NewSentPacketHandler(stopWaitingManager)
receivedPacketHandler = ackhandler.NewReceivedPacketHandler()
}
session := &Session{ session := &Session{
connectionID: connectionID, connectionID: connectionID,
version: v, version: v,
@ -103,8 +117,8 @@ func newSession(conn connection, v protocol.VersionNumber, connectionID protocol
streamCallback: streamCallback, streamCallback: streamCallback,
closeCallback: closeCallback, closeCallback: closeCallback,
streams: make(map[protocol.StreamID]*stream), streams: make(map[protocol.StreamID]*stream),
sentPacketHandler: ackhandlerlegacy.NewSentPacketHandler(stopWaitingManager), sentPacketHandler: sentPacketHandler,
receivedPacketHandler: ackhandlerlegacy.NewReceivedPacketHandler(), receivedPacketHandler: receivedPacketHandler,
stopWaitingManager: stopWaitingManager, stopWaitingManager: stopWaitingManager,
flowControlManager: flowControlManager, flowControlManager: flowControlManager,
receivedPackets: make(chan receivedPacket, protocol.MaxSessionUnprocessedPackets), receivedPackets: make(chan receivedPacket, protocol.MaxSessionUnprocessedPackets),
@ -533,7 +547,7 @@ func (s *Session) sendPacket() error {
} }
stopWaitingFrame := s.stopWaitingManager.GetStopWaitingFrame() stopWaitingFrame := s.stopWaitingManager.GetStopWaitingFrame()
packet, err := s.packer.PackPacket(stopWaitingFrame, controlFrames, s.sentPacketHandler.GetLargestObserved()) packet, err := s.packer.PackPacket(stopWaitingFrame, controlFrames, s.sentPacketHandler.GetLargestAcked())
if err != nil { if err != nil {
return err return err
@ -573,7 +587,7 @@ func (s *Session) sendPacket() error {
} }
func (s *Session) sendConnectionClose(quicErr *qerr.QuicError) error { func (s *Session) sendConnectionClose(quicErr *qerr.QuicError) error {
packet, err := s.packer.PackConnectionClose(&frames.ConnectionCloseFrame{ErrorCode: quicErr.ErrorCode, ReasonPhrase: quicErr.ErrorMessage}, s.sentPacketHandler.GetLargestObserved()) packet, err := s.packer.PackConnectionClose(&frames.ConnectionCloseFrame{ErrorCode: quicErr.ErrorCode, ReasonPhrase: quicErr.ErrorMessage}, s.sentPacketHandler.GetLargestAcked())
if err != nil { if err != nil {
return err return err
} }

View file

@ -51,11 +51,11 @@ func (h *mockSentPacketHandler) SentPacket(packet *ackhandlerlegacy.Packet) erro
func (h *mockSentPacketHandler) ReceivedAck(ackFrame *frames.AckFrame, withPacketNumber protocol.PacketNumber) error { func (h *mockSentPacketHandler) ReceivedAck(ackFrame *frames.AckFrame, withPacketNumber protocol.PacketNumber) error {
return nil return nil
} }
func (h *mockSentPacketHandler) BytesInFlight() protocol.ByteCount { return 0 } func (h *mockSentPacketHandler) BytesInFlight() protocol.ByteCount { return 0 }
func (h *mockSentPacketHandler) GetLargestObserved() protocol.PacketNumber { return 1 } func (h *mockSentPacketHandler) GetLargestAcked() protocol.PacketNumber { return 1 }
func (h *mockSentPacketHandler) CongestionAllowsSending() bool { return true } func (h *mockSentPacketHandler) CongestionAllowsSending() bool { return true }
func (h *mockSentPacketHandler) CheckForError() error { return nil } func (h *mockSentPacketHandler) CheckForError() error { return nil }
func (h *mockSentPacketHandler) TimeOfFirstRTO() time.Time { panic("not implemented") } func (h *mockSentPacketHandler) TimeOfFirstRTO() time.Time { panic("not implemented") }
func (h *mockSentPacketHandler) ProbablyHasPacketForRetransmission() bool { func (h *mockSentPacketHandler) ProbablyHasPacketForRetransmission() bool {
return len(h.retransmissionQueue) > 0 return len(h.retransmissionQueue) > 0