...

Source file src/golang.org/x/net/internal/quic/sent_packet.go

Documentation: golang.org/x/net/internal/quic

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.21
     6  
     7  package quic
     8  
     9  import (
    10  	"sync"
    11  	"time"
    12  )
    13  
    14  // A sentPacket tracks state related to an in-flight packet we sent,
    15  // to be committed when the peer acks it or resent if the packet is lost.
    16  type sentPacket struct {
    17  	num  packetNumber
    18  	size int       // size in bytes
    19  	time time.Time // time sent
    20  
    21  	ackEliciting bool // https://www.rfc-editor.org/rfc/rfc9002.html#section-2-3.4.1
    22  	inFlight     bool // https://www.rfc-editor.org/rfc/rfc9002.html#section-2-3.6.1
    23  	acked        bool // ack has been received
    24  	lost         bool // packet is presumed lost
    25  
    26  	// Frames sent in the packet.
    27  	//
    28  	// This is an abbreviated version of the packet payload, containing only the information
    29  	// we need to process an ack for or loss of this packet.
    30  	// For example, a CRYPTO frame is recorded as the frame type (0x06), offset, and length,
    31  	// but does not include the sent data.
    32  	//
    33  	// This buffer is written by packetWriter.append* and read by Conn.handleAckOrLoss.
    34  	b []byte
    35  	n int // read offset into b
    36  }
    37  
    38  var sentPool = sync.Pool{
    39  	New: func() any {
    40  		return &sentPacket{}
    41  	},
    42  }
    43  
    44  func newSentPacket() *sentPacket {
    45  	sent := sentPool.Get().(*sentPacket)
    46  	sent.reset()
    47  	return sent
    48  }
    49  
    50  // recycle returns a sentPacket to the pool.
    51  func (sent *sentPacket) recycle() {
    52  	sentPool.Put(sent)
    53  }
    54  
    55  func (sent *sentPacket) reset() {
    56  	*sent = sentPacket{
    57  		b: sent.b[:0],
    58  	}
    59  }
    60  
    61  // The append* methods record information about frames in the packet.
    62  
    63  func (sent *sentPacket) appendNonAckElicitingFrame(frameType byte) {
    64  	sent.b = append(sent.b, frameType)
    65  }
    66  
    67  func (sent *sentPacket) appendAckElicitingFrame(frameType byte) {
    68  	sent.ackEliciting = true
    69  	sent.inFlight = true
    70  	sent.b = append(sent.b, frameType)
    71  }
    72  
    73  func (sent *sentPacket) appendInt(v uint64) {
    74  	sent.b = appendVarint(sent.b, v)
    75  }
    76  
    77  func (sent *sentPacket) appendOffAndSize(start int64, size int) {
    78  	sent.b = appendVarint(sent.b, uint64(start))
    79  	sent.b = appendVarint(sent.b, uint64(size))
    80  }
    81  
    82  // The next* methods read back information about frames in the packet.
    83  
    84  func (sent *sentPacket) next() (frameType byte) {
    85  	f := sent.b[sent.n]
    86  	sent.n++
    87  	return f
    88  }
    89  
    90  func (sent *sentPacket) nextInt() uint64 {
    91  	v, n := consumeVarint(sent.b[sent.n:])
    92  	sent.n += n
    93  	return v
    94  }
    95  
    96  func (sent *sentPacket) nextRange() (start, end int64) {
    97  	start = int64(sent.nextInt())
    98  	end = start + int64(sent.nextInt())
    99  	return start, end
   100  }
   101  
   102  func (sent *sentPacket) done() bool {
   103  	return sent.n == len(sent.b)
   104  }
   105  

View as plain text