pool

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package pool provides connection pooling for client scenarios.

This package implements connection reuse for high-throughput client deployments where connections are long-lived and reused frequently.

Package pool provides buffer pool for high-performance I/O operations.

Index

Constants

View Source
const (
	// SmallBufferSize is for lightweight connections (8KB)
	SmallBufferSize = 8 * 1024
	// DefaultBufferSize is the default buffer size (64KB)
	DefaultBufferSize = 64 * 1024
	// LargeBufferSize is for high-throughput scenarios (256KB)
	LargeBufferSize = 256 * 1024
	// XLargeBufferSize is for large response scenarios (512KB)
	XLargeBufferSize = 512 * 1024
)

Variables

View Source
var (
	// ErrPoolClosed is returned when the pool is closed.
	ErrPoolClosed = errors.New("connection pool closed")
	// ErrPoolExhausted is returned when the pool has no available connections.
	ErrPoolExhausted = errors.New("connection pool exhausted")
)

Errors returned by the connection pool.

View Source
var (
	// SmallPool is a 8KB buffer pool for lightweight connections
	SmallPool = NewBufferPool(SmallBufferSize)
	// DefaultPool is a 64KB buffer pool
	DefaultPool = NewBufferPool(DefaultBufferSize)
	// LargePool is a 256KB buffer pool
	LargePool = NewBufferPool(LargeBufferSize)
	// XLargePool is a 512KB buffer pool for large responses
	XLargePool = NewBufferPool(XLargeBufferSize)
)

Global pools for common use

Functions

func GetBuffer

func GetBuffer() *[]byte

GetBuffer gets a buffer from the default pool.

func GetBufferForSize

func GetBufferForSize(size int) *[]byte

GetBufferForSize returns a buffer from the appropriate pool based on size. This allows dynamic buffer selection based on expected data size.

func GetLargeBuffer

func GetLargeBuffer() *[]byte

GetLargeBuffer gets a large buffer from the large pool.

func GetSmallBuffer

func GetSmallBuffer() *[]byte

GetSmallBuffer gets a small buffer from the small pool.

func GetXLargeBuffer

func GetXLargeBuffer() *[]byte

GetXLargeBuffer gets an extra large buffer from the xlarge pool.

func PutBuffer

func PutBuffer(b *[]byte)

PutBuffer returns a buffer to the default pool.

func PutBufferForSize

func PutBufferForSize(b *[]byte)

PutBufferForSize returns a buffer to the appropriate pool based on its capacity.

func PutLargeBuffer

func PutLargeBuffer(b *[]byte)

PutLargeBuffer returns a large buffer to the large pool.

func PutSmallBuffer

func PutSmallBuffer(b *[]byte)

PutSmallBuffer returns a small buffer to the small pool.

func PutXLargeBuffer

func PutXLargeBuffer(b *[]byte)

PutXLargeBuffer returns an extra large buffer to the xlarge pool.

Types

type BufferPool

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

BufferPool manages reusable byte slices.

func NewBufferPool

func NewBufferPool(size int) *BufferPool

NewBufferPool creates a new buffer pool with specified buffer size.

func (*BufferPool) Get

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

Get retrieves a buffer from the pool.

func (*BufferPool) Put

func (p *BufferPool) Put(b *[]byte)

Put returns a buffer to the pool.

func (*BufferPool) Size

func (p *BufferPool) Size() int

Size returns the buffer size of this pool.

type ConnPool

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

ConnPool is a pool of reusable connections.

func NewConnPool

func NewConnPool(factory func() (net.Conn, error), cfg ConnPoolConfig) *ConnPool

NewConnPool creates a new connection pool.

func (*ConnPool) Close

func (p *ConnPool) Close() error

Close closes all connections in the pool.

func (*ConnPool) Get

func (p *ConnPool) Get(ctx context.Context) (net.Conn, error)

Get retrieves a connection from the pool or creates a new one.

func (*ConnPool) GetStats

func (p *ConnPool) GetStats() Stats

GetStats returns current pool statistics.

func (*ConnPool) Prune

func (p *ConnPool) Prune() int

Prune removes expired connections from the pool.

func (*ConnPool) Put

func (p *ConnPool) Put(conn net.Conn)

Put returns a connection to the pool.

type ConnPoolConfig

type ConnPoolConfig struct {
	// MaxIdle is the maximum number of idle connections.
	// Default is 10.
	MaxIdle int

	// MaxAge is the maximum age of a connection.
	// Connections older than this are closed.
	// Default is 0 (no limit).
	MaxAge time.Duration

	// DialTimeout is the timeout for creating new connections.
	// Default is 10 seconds.
	DialTimeout time.Duration

	// KeepAlive is the keep-alive interval for connections.
	// Default is 30 seconds.
	KeepAlive time.Duration
}

ConnPoolConfig holds the configuration for the connection pool.

func DefaultConnPoolConfig

func DefaultConnPoolConfig() ConnPoolConfig

DefaultConnPoolConfig returns the default configuration.

type Dialer

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

Dialer creates a connection pool for a specific address.

func NewDialer

func NewDialer(network, address string, cfg ConnPoolConfig) *Dialer

NewDialer creates a new dialer with connection pooling.

func (*Dialer) Dial

func (d *Dialer) Dial(ctx context.Context) (net.Conn, error)

Dial creates a new connection (not pooled).

func (*Dialer) NewPool

func (d *Dialer) NewPool() *ConnPool

NewPool creates a connection pool for this dialer.

type Stats

type Stats struct {
	Created   int64
	Reused    int64
	Closed    int64
	Idle      int
	MaxIdle   int
	WaitCount int64
	WaitTime  time.Duration
}

Stats returns pool statistics.

Jump to

Keyboard shortcuts

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