golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/dgram.go (about)

     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  	"net/netip"
    11  	"sync"
    12  )
    13  
    14  type datagram struct {
    15  	b         []byte
    16  	localAddr netip.AddrPort
    17  	peerAddr  netip.AddrPort
    18  	ecn       ecnBits
    19  }
    20  
    21  // Explicit Congestion Notification bits.
    22  //
    23  // https://www.rfc-editor.org/rfc/rfc3168.html#section-5
    24  type ecnBits byte
    25  
    26  const (
    27  	ecnMask   = 0b000000_11
    28  	ecnNotECT = 0b000000_00
    29  	ecnECT1   = 0b000000_01
    30  	ecnECT0   = 0b000000_10
    31  	ecnCE     = 0b000000_11
    32  )
    33  
    34  var datagramPool = sync.Pool{
    35  	New: func() any {
    36  		return &datagram{
    37  			b: make([]byte, maxUDPPayloadSize),
    38  		}
    39  	},
    40  }
    41  
    42  func newDatagram() *datagram {
    43  	m := datagramPool.Get().(*datagram)
    44  	*m = datagram{
    45  		b: m.b[:cap(m.b)],
    46  	}
    47  	return m
    48  }
    49  
    50  func (m *datagram) recycle() {
    51  	if cap(m.b) != maxUDPPayloadSize {
    52  		return
    53  	}
    54  	datagramPool.Put(m)
    55  }