github.com/quic-go/quic-go@v0.44.0/internal/wire/pool.go (about)

     1  package wire
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/quic-go/quic-go/internal/protocol"
     7  )
     8  
     9  var pool sync.Pool
    10  
    11  func init() {
    12  	pool.New = func() interface{} {
    13  		return &StreamFrame{
    14  			Data:     make([]byte, 0, protocol.MaxPacketBufferSize),
    15  			fromPool: true,
    16  		}
    17  	}
    18  }
    19  
    20  func GetStreamFrame() *StreamFrame {
    21  	f := pool.Get().(*StreamFrame)
    22  	return f
    23  }
    24  
    25  func putStreamFrame(f *StreamFrame) {
    26  	if !f.fromPool {
    27  		return
    28  	}
    29  	if protocol.ByteCount(cap(f.Data)) != protocol.MaxPacketBufferSize {
    30  		panic("wire.PutStreamFrame called with packet of wrong size!")
    31  	}
    32  	pool.Put(f)
    33  }