Source file src/vendor/golang.org/x/net/quic/dgram.go

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

View as plain text