introduce a shortHeaderPacket type and use it in packet packer

This commit is contained in:
Marten Seemann 2022-09-04 17:31:33 +03:00
parent 3e7bad5efc
commit ec9a6e7a99
5 changed files with 218 additions and 212 deletions

View file

@ -53,15 +53,12 @@ var _ = Describe("Connection", func() {
destConnID := protocol.ParseConnectionID([]byte{8, 7, 6, 5, 4, 3, 2, 1})
clientDestConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
getPacket := func(pn protocol.PacketNumber) *packedPacket {
getShortHeaderPacket := func(pn protocol.PacketNumber) shortHeaderPacket {
buffer := getPacketBuffer()
buffer.Data = append(buffer.Data, []byte("foobar")...)
return &packedPacket{
buffer: buffer,
packetContents: &packetContents{
header: &wire.ExtendedHeader{PacketNumber: pn},
length: 6, // foobar
},
return shortHeaderPacket{
Packet: &ackhandler.Packet{PacketNumber: pn},
Buffer: buffer,
}
}
@ -612,9 +609,8 @@ var _ = Describe("Connection", func() {
connRunner.EXPECT().Remove(gomock.Any()).AnyTimes()
cryptoSetup.EXPECT().Close()
conn.sentPacketHandler = sph
p := getPacket(1)
packer.EXPECT().PackPacket(false).Return(p, nil)
packer.EXPECT().PackPacket(false).Return(nil, nil).AnyTimes()
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
runConn()
conn.queueControlFrame(&wire.PingFrame{})
conn.scheduleSending()
@ -1239,18 +1235,18 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SentPacket(gomock.Any())
conn.sentPacketHandler = sph
runConn()
p := getPacket(1)
packer.EXPECT().PackPacket(false).Return(p, nil)
packer.EXPECT().PackPacket(false).Return(nil, nil).AnyTimes()
p := getShortHeaderPacket(1)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(p, nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
sent := make(chan struct{})
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any()).Do(func(packet *packetBuffer) { close(sent) })
tracer.EXPECT().SentShortHeaderPacket(&logging.ShortHeader{
DestConnectionID: p.header.DestConnectionID,
PacketNumber: p.header.PacketNumber,
PacketNumberLen: p.header.PacketNumberLen,
KeyPhase: p.header.KeyPhase,
}, p.buffer.Len(), nil, []logging.Frame{})
DestConnectionID: p.DestConnID,
PacketNumber: p.PacketNumber,
PacketNumberLen: p.PacketNumberLen,
KeyPhase: p.KeyPhase,
}, p.Buffer.Len(), nil, []logging.Frame{})
conn.scheduleSending()
Eventually(sent).Should(BeClosed())
})
@ -1258,7 +1254,7 @@ var _ = Describe("Connection", func() {
It("doesn't send packets if there's nothing to send", func() {
conn.handshakeConfirmed = true
runConn()
packer.EXPECT().PackPacket(false).Return(nil, nil).AnyTimes()
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
conn.receivedPacketHandler.ReceivedPacket(0x035e, protocol.ECNNon, protocol.Encryption1RTT, time.Now(), true)
conn.scheduleSending()
time.Sleep(50 * time.Millisecond) // make sure there are no calls to mconn.Write()
@ -1289,14 +1285,14 @@ var _ = Describe("Connection", func() {
fc := mocks.NewMockConnectionFlowController(mockCtrl)
fc.EXPECT().IsNewlyBlocked().Return(true, protocol.ByteCount(1337))
fc.EXPECT().IsNewlyBlocked()
p := getPacket(1)
packer.EXPECT().PackPacket(false).Return(p, nil)
packer.EXPECT().PackPacket(false).Return(nil, nil).AnyTimes()
p := getShortHeaderPacket(1)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(p, nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
conn.connFlowController = fc
runConn()
sent := make(chan struct{})
sender.EXPECT().Send(gomock.Any()).Do(func(packet *packetBuffer) { close(sent) })
tracer.EXPECT().SentShortHeaderPacket(gomock.Any(), p.length, nil, []logging.Frame{})
tracer.EXPECT().SentShortHeaderPacket(gomock.Any(), p.Buffer.Len(), nil, []logging.Frame{})
conn.scheduleSending()
Eventually(sent).Should(BeClosed())
frames, _ := conn.framer.AppendControlFrames(nil, 1000)
@ -1422,8 +1418,8 @@ var _ = Describe("Connection", func() {
sph.EXPECT().HasPacingBudget()
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(time.Hour))
sph.EXPECT().SendMode().Return(ackhandler.SendAny).Times(3)
packer.EXPECT().PackPacket(false).Return(getPacket(10), nil)
packer.EXPECT().PackPacket(false).Return(getPacket(11), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(10), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(11), nil)
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any()).Times(2)
go func() {
@ -1439,8 +1435,8 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SentPacket(gomock.Any())
sph.EXPECT().HasPacingBudget().Return(true).AnyTimes()
sph.EXPECT().SendMode().Return(ackhandler.SendAny).Times(2)
packer.EXPECT().PackPacket(false).Return(getPacket(10), nil)
packer.EXPECT().PackPacket(false).Return(nil, nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(10), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack)
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any())
go func() {
@ -1457,7 +1453,7 @@ var _ = Describe("Connection", func() {
sph.EXPECT().HasPacingBudget()
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(time.Hour))
sph.EXPECT().SendMode().Return(ackhandler.SendAny)
packer.EXPECT().PackPacket(true).Return(getPacket(10), nil)
packer.EXPECT().PackPacket(true, gomock.Any()).Return(getShortHeaderPacket(10), nil)
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any())
go func() {
@ -1476,7 +1472,7 @@ var _ = Describe("Connection", func() {
sph.EXPECT().HasPacingBudget().Return(true)
sph.EXPECT().SendMode().Return(ackhandler.SendAny)
sph.EXPECT().SendMode().Return(ackhandler.SendAck)
packer.EXPECT().PackPacket(false).Return(getPacket(100), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(100), nil)
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any())
go func() {
@ -1493,12 +1489,12 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
gomock.InOrder(
sph.EXPECT().HasPacingBudget().Return(true),
packer.EXPECT().PackPacket(false).Return(getPacket(100), nil),
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(100), nil),
sph.EXPECT().SentPacket(gomock.Any()),
sph.EXPECT().HasPacingBudget(),
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(pacingDelay)),
sph.EXPECT().HasPacingBudget().Return(true),
packer.EXPECT().PackPacket(false).Return(getPacket(101), nil),
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(101), nil),
sph.EXPECT().SentPacket(gomock.Any()),
sph.EXPECT().HasPacingBudget(),
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(time.Hour)),
@ -1523,9 +1519,9 @@ var _ = Describe("Connection", func() {
sph.EXPECT().HasPacingBudget()
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(time.Hour))
sph.EXPECT().SendMode().Return(ackhandler.SendAny).Times(4)
packer.EXPECT().PackPacket(false).Return(getPacket(1000), nil)
packer.EXPECT().PackPacket(false).Return(getPacket(1001), nil)
packer.EXPECT().PackPacket(false).Return(getPacket(1002), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1000), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1001), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1002), nil)
written := make(chan struct{}, 3)
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any()).DoAndReturn(func(p *packetBuffer) { written <- struct{}{} }).Times(3)
@ -1555,8 +1551,8 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SentPacket(gomock.Any())
sph.EXPECT().HasPacingBudget().Return(true).AnyTimes()
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
packer.EXPECT().PackPacket(false).Return(getPacket(1000), nil)
packer.EXPECT().PackPacket(false).Return(nil, nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1000), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack)
sender.EXPECT().Send(gomock.Any()).DoAndReturn(func(p *packetBuffer) { close(written) })
available <- struct{}{}
Eventually(written).Should(BeClosed())
@ -1578,8 +1574,8 @@ var _ = Describe("Connection", func() {
})
sph.EXPECT().HasPacingBudget().Return(true).AnyTimes()
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
packer.EXPECT().PackPacket(false).Return(getPacket(1000), nil)
packer.EXPECT().PackPacket(false).Return(nil, nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1000), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack)
sender.EXPECT().Send(gomock.Any()).DoAndReturn(func(p *packetBuffer) { close(written) })
conn.scheduleSending()
@ -1592,7 +1588,7 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SentPacket(gomock.Any())
sph.EXPECT().HasPacingBudget().Return(true).AnyTimes()
sph.EXPECT().SendMode().Return(ackhandler.SendAny)
packer.EXPECT().PackPacket(false).Return(getPacket(1000), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1000), nil)
written := make(chan struct{}, 1)
sender.EXPECT().WouldBlock()
sender.EXPECT().WouldBlock().Return(true).Times(2)
@ -1613,8 +1609,8 @@ var _ = Describe("Connection", func() {
sph.EXPECT().HasPacingBudget().Return(true).AnyTimes()
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
sender.EXPECT().WouldBlock().AnyTimes()
packer.EXPECT().PackPacket(false).Return(getPacket(1001), nil)
packer.EXPECT().PackPacket(false).Return(nil, nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1001), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack)
sender.EXPECT().Send(gomock.Any()).DoAndReturn(func(p *packetBuffer) { written <- struct{}{} })
available <- struct{}{}
Eventually(written).Should(Receive())
@ -1628,7 +1624,7 @@ var _ = Describe("Connection", func() {
sph.EXPECT().HasPacingBudget().Return(true)
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
sender.EXPECT().WouldBlock().AnyTimes()
packer.EXPECT().PackPacket(false)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack)
// don't EXPECT any calls to mconn.Write()
go func() {
defer GinkgoRecover()
@ -1653,7 +1649,7 @@ var _ = Describe("Connection", func() {
mtuDiscoverer.EXPECT().ShouldSendProbe(gomock.Any()).Return(true)
ping := ackhandler.Frame{Frame: &wire.PingFrame{}}
mtuDiscoverer.EXPECT().GetPing().Return(ping, protocol.ByteCount(1234))
packer.EXPECT().PackMTUProbePacket(ping, protocol.ByteCount(1234)).Return(getPacket(1), nil)
packer.EXPECT().PackMTUProbePacket(ping, protocol.ByteCount(1234), gomock.Any()).Return(getShortHeaderPacket(1), nil)
go func() {
defer GinkgoRecover()
cryptoSetup.EXPECT().RunHandshake().MaxTimes(1)
@ -1697,8 +1693,8 @@ var _ = Describe("Connection", func() {
sph.EXPECT().HasPacingBudget().Return(true).AnyTimes()
sph.EXPECT().SentPacket(gomock.Any())
conn.sentPacketHandler = sph
packer.EXPECT().PackPacket(false).Return(getPacket(1), nil)
packer.EXPECT().PackPacket(false).Return(nil, nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack)
go func() {
defer GinkgoRecover()
@ -1716,8 +1712,8 @@ var _ = Describe("Connection", func() {
})
It("sets the timer to the ack timer", func() {
packer.EXPECT().PackPacket(false).Return(getPacket(1234), nil)
packer.EXPECT().PackPacket(false).Return(nil, nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(getShortHeaderPacket(1234), nil)
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack)
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLossDetectionTimeout().AnyTimes()
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
@ -1940,19 +1936,14 @@ var _ = Describe("Connection", func() {
conn.sentPacketHandler = sph
done := make(chan struct{})
connRunner.EXPECT().Retire(clientDestConnID)
packer.EXPECT().PackPacket(false).DoAndReturn(func(bool) (*packedPacket, error) {
packer.EXPECT().PackPacket(false, gomock.Any()).DoAndReturn(func(bool, time.Time) (shortHeaderPacket, error) {
frames, _ := conn.framer.AppendControlFrames(nil, protocol.MaxByteCount)
Expect(frames).ToNot(BeEmpty())
Expect(frames[0].Frame).To(BeEquivalentTo(&wire.HandshakeDoneFrame{}))
defer close(done)
return &packedPacket{
packetContents: &packetContents{
header: &wire.ExtendedHeader{},
},
buffer: getPacketBuffer(),
}, nil
return shortHeaderPacket{Buffer: getPacketBuffer(), Packet: &ackhandler.Packet{}}, nil
})
packer.EXPECT().PackPacket(false).AnyTimes()
packer.EXPECT().PackPacket(false, gomock.Any()).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
go func() {
defer GinkgoRecover()
cryptoSetup.EXPECT().RunHandshake()
@ -2082,7 +2073,7 @@ var _ = Describe("Connection", func() {
setRemoteIdleTimeout(5 * time.Second)
conn.lastPacketReceivedTime = time.Now().Add(-5 * time.Second / 2)
sent := make(chan struct{})
packer.EXPECT().PackCoalescedPacket(false).Do(func(bool) (*packedPacket, error) {
packer.EXPECT().PackCoalescedPacket(false).Do(func(bool) (*coalescedPacket, error) {
close(sent)
return nil, nil
})
@ -2095,7 +2086,7 @@ var _ = Describe("Connection", func() {
setRemoteIdleTimeout(time.Hour)
conn.lastPacketReceivedTime = time.Now().Add(-protocol.MaxKeepAliveInterval).Add(-time.Millisecond)
sent := make(chan struct{})
packer.EXPECT().PackCoalescedPacket(false).Do(func(bool) (*packedPacket, error) {
packer.EXPECT().PackCoalescedPacket(false).Do(func(bool) (*coalescedPacket, error) {
close(sent)
return nil, nil
})