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
- Variables
- func GetBuffer() *[]byte
- func GetBufferForSize(size int) *[]byte
- func GetLargeBuffer() *[]byte
- func GetSmallBuffer() *[]byte
- func GetXLargeBuffer() *[]byte
- func PutBuffer(b *[]byte)
- func PutBufferForSize(b *[]byte)
- func PutLargeBuffer(b *[]byte)
- func PutSmallBuffer(b *[]byte)
- func PutXLargeBuffer(b *[]byte)
- type BufferPool
- type ConnPool
- type ConnPoolConfig
- type Dialer
- type Stats
Constants ¶
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 ¶
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.
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 GetBufferForSize ¶
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 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) 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.
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.