...

Source file src/golang.org/x/net/internal/quic/dgram.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  	"net/netip"
    11  	"sync"
    12  )
    13  
    14  type datagram struct {
    15  	b    []byte
    16  	addr netip.AddrPort
    17  }
    18  
    19  var datagramPool = sync.Pool{
    20  	New: func() any {
    21  		return &datagram{
    22  			b: make([]byte, maxUDPPayloadSize),
    23  		}
    24  	},
    25  }
    26  
    27  func newDatagram() *datagram {
    28  	m := datagramPool.Get().(*datagram)
    29  	m.b = m.b[:cap(m.b)]
    30  	return m
    31  }
    32  
    33  func (m *datagram) recycle() {
    34  	if cap(m.b) != maxUDPPayloadSize {
    35  		return
    36  	}
    37  	datagramPool.Put(m)
    38  }
    39  

View as plain text