utils

package
v1.15.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SmallBufferPool for small buffers (4KB) - typical for network I/O, headers, small messages
	SmallBufferPool = NewBufferPool(4 * 1024)

	// MediumBufferPool for medium buffers (16KB) - good for most application data
	MediumBufferPool = NewBufferPool(16 * 1024)

	// LargeBufferPool for large buffers (64KB) - for UDP max packet size, large transfers
	LargeBufferPool = NewBufferPool(64 * 1024)
)

Global buffer pools for common sizes

Functions

func GetBufferForSize added in v1.9.0

func GetBufferForSize(size int) []byte

GetBufferForSize returns the most appropriate clean buffer for the given size This helps choose the right pool automatically based on size requirements

func GetDirtyBufferForSize added in v1.13.0

func GetDirtyBufferForSize(size int) []byte

GetDirtyBufferForSize returns an uncleared buffer of the requested size. The caller must fully overwrite indices it reads back. Sizes above the largest pool fall back to a plain make, which is zero-initialized.

func GetLargeBuffer added in v1.9.0

func GetLargeBuffer() []byte

GetLargeBuffer gets a clean 64KB buffer from the large buffer pool

func GetMediumBuffer added in v1.9.0

func GetMediumBuffer() []byte

GetMediumBuffer gets a clean 16KB buffer from the medium buffer pool

func GetSmallBuffer added in v1.9.0

func GetSmallBuffer() []byte

GetSmallBuffer gets a clean 4KB buffer from the small buffer pool

func PutBufferForSize added in v1.9.0

func PutBufferForSize(buf []byte)

PutBufferForSize returns a buffer to the appropriate pool based on its size

func PutLargeBuffer added in v1.9.0

func PutLargeBuffer(buf []byte)

PutLargeBuffer returns a 64KB buffer to the large buffer pool

func PutMediumBuffer added in v1.9.0

func PutMediumBuffer(buf []byte)

PutMediumBuffer returns a 16KB buffer to the medium buffer pool

func PutSmallBuffer added in v1.9.0

func PutSmallBuffer(buf []byte)

PutSmallBuffer returns a 4KB buffer to the small buffer pool

func VarIntDecode added in v1.9.0

func VarIntDecode(flag byte, bbf buf.ByteBuf) uint64

VarIntDecode reads a variable-length encoded value from bbf using flag as the length-class selector previously read from the stream.

func VarIntEncode added in v1.9.0

func VarIntEncode(val uint64) buf.ByteBuf

VarIntEncode returns a newly allocated ByteBuf carrying the variable-length encoding of val.

func VarIntEncodeTo added in v1.13.0

func VarIntEncodeTo(dst buf.ByteBuf, val uint64) buf.ByteBuf

VarIntEncodeTo writes the variable-length encoding of val into dst and returns dst for chaining. The encoded form occupies 1, 3, 5, or 9 bytes depending on the magnitude of val.

Types

type BufferPool added in v1.9.0

type BufferPool struct {
	// contains filtered or unexported fields
}

BufferPool provides a thread-safe pool for byte buffers of different sizes This helps reduce memory allocations and GC pressure for frequently used buffers

func NewBufferPool added in v1.9.0

func NewBufferPool(size int) *BufferPool

NewBufferPool creates a new buffer pool with the specified buffer size

func (*BufferPool) Get added in v1.9.0

func (bp *BufferPool) Get() []byte

Get retrieves a buffer from the pool and clears it to prevent dirty data Returns a clean byte slice of the pool's configured size

func (*BufferPool) GetDirty added in v1.13.0

func (bp *BufferPool) GetDirty() []byte

GetDirty retrieves a buffer without clearing it. Callers must treat the returned slice as uninitialized and only access indices they themselves populate (e.g. via Read). Use Get when the buffer is consumed in full.

func (*BufferPool) GetWithSize added in v1.9.0

func (bp *BufferPool) GetWithSize(size int) []byte

GetWithSize retrieves a buffer and resizes it if needed If the requested size is larger than the pool buffer, it creates a new buffer This provides flexibility while still benefiting from pooling for common sizes

func (*BufferPool) Put added in v1.9.0

func (bp *BufferPool) Put(buf []byte)

Put returns a buffer to the pool for reuse The buffer should be of the same size as the pool's configured size

func (*BufferPool) Size added in v1.9.0

func (bp *BufferPool) Size() int

Size returns the configured buffer size for this pool

type MockQueue added in v1.9.0

type MockQueue struct {
	mock.Mock
}

MockQueue is a mock implementation of Queue for testing

func NewMockQueue added in v1.9.0

func NewMockQueue() *MockQueue

NewMockQueue creates a new MockQueue instance

func (*MockQueue) Pop added in v1.9.0

func (m *MockQueue) Pop() any

Pop mocks the Pop method for removing items from queue

func (*MockQueue) Push added in v1.9.0

func (m *MockQueue) Push(obj any)

Push mocks the Push method for adding items to queue

func (*MockQueue) Size added in v1.9.0

func (m *MockQueue) Size() int

Size mocks the Size method for getting queue length

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue provides a thread-safe FIFO queue implementation Safe for concurrent access from multiple goroutines

func (*Queue) Pop

func (q *Queue) Pop() any

Pop removes and returns the oldest item from the queue Returns nil if the queue is empty Thread-safe for concurrent use

func (*Queue) Push

func (q *Queue) Push(obj any)

Push adds an item to the front of the queue (FIFO behavior) Thread-safe for concurrent use

func (*Queue) Size added in v1.9.0

func (q *Queue) Size() int

Size returns the current number of items in the queue Thread-safe for concurrent use

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL