Skip to content

Commit 3f563d3

Browse files
neildgopherbot
authored andcommitted
quic: use an enum for sentPacket state
The sentPacket type tracks the state of a packet sent to the peer. Packets can be in progress, acknowledged, or lost. Track this state with an enum rather than a set of bools, to avoid the possibility of nonsensical states such as a packet being both acknowledged and lost. This also simplifies a following change to add an "unsent" state for intentionally skipped packet numbers. Change-Id: I87c8fc399c72337c033ab7ec5ec8db2c56c732f9 Reviewed-on: https://go-review.googlesource.com/c/net/+/664297 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> Auto-Submit: Damien Neil <[email protected]>
1 parent a3b6e77 commit 3f563d3

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

quic/loss.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,20 @@ func (c *lossState) receiveAckRange(now time.Time, space numberSpace, rangeIndex
246246
// If the latest packet in the ACK frame is newly-acked,
247247
// record the RTT in c.ackFrameRTT.
248248
sent := c.spaces[space].num(end - 1)
249-
if !sent.acked {
249+
if sent.state == sentPacketSent {
250250
c.ackFrameRTT = max(0, now.Sub(sent.time))
251251
}
252252
}
253253
for pnum := start; pnum < end; pnum++ {
254254
sent := c.spaces[space].num(pnum)
255-
if sent.acked || sent.lost {
255+
if sent.state != sentPacketSent {
256256
continue
257257
}
258258
// This is a newly-acknowledged packet.
259259
if pnum > c.spaces[space].maxAcked {
260260
c.spaces[space].maxAcked = pnum
261261
}
262-
sent.acked = true
262+
sent.state = sentPacketAcked
263263
c.cc.packetAcked(now, sent)
264264
ackf(space, sent, packetAcked)
265265
if sent.ackEliciting {
@@ -315,12 +315,12 @@ func (c *lossState) receiveAckEnd(now time.Time, log *slog.Logger, space numberS
315315
func (c *lossState) discardPackets(space numberSpace, log *slog.Logger, lossf func(numberSpace, *sentPacket, packetFate)) {
316316
for i := 0; i < c.spaces[space].size; i++ {
317317
sent := c.spaces[space].nth(i)
318-
if sent.acked || sent.lost {
318+
if sent.state != sentPacketSent {
319319
// This should not be possible, since we only discard packets
320320
// in spaces which have never received an ack, but check anyway.
321321
continue
322322
}
323-
sent.lost = true
323+
sent.state = sentPacketLost
324324
c.cc.packetDiscarded(sent)
325325
lossf(numberSpace(space), sent, packetLost)
326326
}
@@ -335,7 +335,7 @@ func (c *lossState) discardKeys(now time.Time, log *slog.Logger, space numberSpa
335335
// https://www.rfc-editor.org/rfc/rfc9002.html#section-6.4
336336
for i := 0; i < c.spaces[space].size; i++ {
337337
sent := c.spaces[space].nth(i)
338-
if sent.acked || sent.lost {
338+
if sent.state != sentPacketSent {
339339
continue
340340
}
341341
c.cc.packetDiscarded(sent)
@@ -362,7 +362,7 @@ func (c *lossState) detectLoss(now time.Time, lossf func(numberSpace, *sentPacke
362362
for space := numberSpace(0); space < numberSpaceCount; space++ {
363363
for i := 0; i < c.spaces[space].size; i++ {
364364
sent := c.spaces[space].nth(i)
365-
if sent.lost || sent.acked {
365+
if sent.state != sentPacketSent {
366366
continue
367367
}
368368
// RFC 9002 Section 6.1 states that a packet is only declared lost if it
@@ -378,13 +378,13 @@ func (c *lossState) detectLoss(now time.Time, lossf func(numberSpace, *sentPacke
378378
case sent.num <= c.spaces[space].maxAcked && !sent.time.After(lossTime):
379379
// Time threshold
380380
// https://www.rfc-editor.org/rfc/rfc9002.html#section-6.1.2
381-
sent.lost = true
381+
sent.state = sentPacketLost
382382
lossf(space, sent, packetLost)
383383
if sent.inFlight {
384384
c.cc.packetLost(now, space, sent, &c.rtt)
385385
}
386386
}
387-
if !sent.lost {
387+
if sent.state != sentPacketLost {
388388
break
389389
}
390390
}

quic/sent_packet.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ type sentPacket struct {
1919
time time.Time // time sent
2020
ptype packetType
2121

22+
state sentPacketState
2223
ackEliciting bool // https://www.rfc-editor.org/rfc/rfc9002.html#section-2-3.4.1
2324
inFlight bool // https://www.rfc-editor.org/rfc/rfc9002.html#section-2-3.6.1
24-
acked bool // ack has been received
25-
lost bool // packet is presumed lost
2625

2726
// Frames sent in the packet.
2827
//
@@ -36,6 +35,14 @@ type sentPacket struct {
3635
n int // read offset into b
3736
}
3837

38+
type sentPacketState uint8
39+
40+
const (
41+
sentPacketSent = sentPacketState(iota) // sent but neither acked nor lost
42+
sentPacketAcked // acked
43+
sentPacketLost // declared lost
44+
)
45+
3946
var sentPool = sync.Pool{
4047
New: func() any {
4148
return &sentPacket{}

quic/sent_packet_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (s *sentPacketList) num(num packetNumber) *sentPacket {
6767
func (s *sentPacketList) clean() {
6868
for s.size > 0 {
6969
sent := s.p[s.off]
70-
if !sent.acked && !sent.lost {
70+
if sent.state == sentPacketSent {
7171
return
7272
}
7373
sent.recycle()

quic/sent_packet_list_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestSentPacketListSlidingWindow(t *testing.T) {
2626
if got := list.nth(0); got != sent {
2727
t.Fatalf("list.nth(0) != list.num(%v)", prev)
2828
}
29-
sent.acked = true
29+
sent.state = sentPacketAcked
3030
list.clean()
3131
if got := list.num(prev); got != nil {
3232
t.Fatalf("list.num(%v) = packet %v, expected it to be discarded", prev, got.num)
@@ -82,7 +82,7 @@ func TestSentPacketListCleanAll(t *testing.T) {
8282
}
8383
// Mark all the packets as acked.
8484
for i := packetNumber(0); i < count; i++ {
85-
list.num(i).acked = true
85+
list.num(i).state = sentPacketAcked
8686
}
8787
list.clean()
8888
if got, want := list.size, 0; got != want {

0 commit comments

Comments
 (0)