pool

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCircuitOpen is returned when the circuit breaker is open
	ErrCircuitOpen = errors.New("circuit breaker is open")

	// ErrTooManyRequests is returned when half-open circuit has too many requests
	ErrTooManyRequests = errors.New("too many requests in half-open state")
)
View Source
var (
	// ErrPoolClosed is returned when operations are attempted on a closed pool
	ErrPoolClosed = errors.New("pool is closed")

	// ErrPoolExhausted is returned when the pool is at capacity and cannot create new connections
	ErrPoolExhausted = errors.New("pool exhausted")

	// ErrContextDeadline is returned when context deadline is exceeded
	ErrContextDeadline = errors.New("context deadline exceeded")

	// ErrInvalidConfig is returned when pool configuration is invalid
	ErrInvalidConfig = errors.New("invalid pool configuration")
)

Functions

This section is empty.

Types

type CircuitBreaker

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

CircuitBreaker implements the circuit breaker pattern

func NewCircuitBreaker

func NewCircuitBreaker(config CircuitBreakerConfig) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(ctx context.Context, fn func(context.Context) error) error

Execute runs the given function with circuit breaker protection

func (*CircuitBreaker) ExecuteWithFallback

func (cb *CircuitBreaker) ExecuteWithFallback(ctx context.Context, fn func(context.Context) error, fallback func(context.Context) error) error

ExecuteWithFallback runs the function with a fallback on circuit open

func (*CircuitBreaker) GetState

func (cb *CircuitBreaker) GetState() State

GetState returns the current state

func (*CircuitBreaker) GetStats

func (cb *CircuitBreaker) GetStats() CircuitBreakerStats

GetStats returns circuit breaker statistics

func (*CircuitBreaker) Reset

func (cb *CircuitBreaker) Reset()

Reset resets the circuit breaker to closed state

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	// FailureThreshold is the number of failures before opening the circuit
	FailureThreshold int

	// SuccessThreshold is the number of successes in half-open before closing
	SuccessThreshold int

	// Timeout for operations
	Timeout time.Duration

	// OpenDuration is how long the circuit stays open
	OpenDuration time.Duration

	// HalfOpenMaxRequests is the max concurrent requests in half-open state
	HalfOpenMaxRequests int

	// OnStateChange is called when the circuit breaker changes state
	OnStateChange func(from, to State)
}

CircuitBreakerConfig holds circuit breaker configuration

func DefaultCircuitBreakerConfig

func DefaultCircuitBreakerConfig() CircuitBreakerConfig

DefaultCircuitBreakerConfig returns default circuit breaker configuration

type CircuitBreakerStats

type CircuitBreakerStats struct {
	State                string
	TotalRequests        int64
	TotalFailures        int64
	TotalSuccesses       int64
	TotalTimeouts        int64
	TotalCircuitOpens    int64
	ConsecutiveFailures  int64
	ConsecutiveSuccesses int64
	LastFailureTime      time.Time
	LastStateChange      time.Time
}

CircuitBreakerStats holds circuit breaker statistics

type Config

type Config struct {
	// MinSize is the minimum number of connections to maintain
	MinSize int

	// MaxSize is the maximum number of connections allowed
	MaxSize int

	// MaxIdleTime is the maximum time a connection can be idle before being closed
	MaxIdleTime time.Duration

	// MaxLifetime is the maximum lifetime of a connection
	MaxLifetime time.Duration

	// HealthCheckInterval is how often to check connection health
	HealthCheckInterval time.Duration

	// AcquireTimeout is the maximum time to wait for a connection
	AcquireTimeout time.Duration

	// Factory creates new connections
	Factory Factory
}

Config holds pool configuration

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns default pool configuration

type Connection

type Connection interface {
	// IsValid checks if the connection is still valid
	IsValid() bool

	// Close closes the underlying connection
	Close() error

	// GetID returns the connection ID
	GetID() string
}

Connection represents a pooled connection

type Factory

type Factory func(ctx context.Context) (Connection, error)

Factory creates new connections

type Pool

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

Pool manages a pool of connections

func NewPool

func NewPool(config Config) (*Pool, error)

NewPool creates a new connection pool

func (*Pool) Acquire

func (p *Pool) Acquire(ctx context.Context) (Connection, error)

Acquire gets a connection from the pool

func (*Pool) Close

func (p *Pool) Close() error

Close closes the pool and all connections

func (*Pool) GetStats

func (p *Pool) GetStats() Stats

GetStats returns pool statistics

type PoolWithCircuitBreaker

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

PoolWithCircuitBreaker wraps a pool with circuit breaker protection

func NewPoolWithCircuitBreaker

func NewPoolWithCircuitBreaker(poolConfig Config, cbConfig CircuitBreakerConfig) (*PoolWithCircuitBreaker, error)

NewPoolWithCircuitBreaker creates a pool with circuit breaker protection

func (*PoolWithCircuitBreaker) Acquire

func (pcb *PoolWithCircuitBreaker) Acquire(ctx context.Context) (Connection, error)

Acquire gets a connection with circuit breaker protection

func (*PoolWithCircuitBreaker) Close

func (pcb *PoolWithCircuitBreaker) Close() error

Close closes the pool and circuit breaker

func (*PoolWithCircuitBreaker) GetCircuitBreakerStats

func (pcb *PoolWithCircuitBreaker) GetCircuitBreakerStats() CircuitBreakerStats

GetCircuitBreakerStats returns circuit breaker statistics

func (*PoolWithCircuitBreaker) GetPoolStats

func (pcb *PoolWithCircuitBreaker) GetPoolStats() Stats

GetPoolStats returns pool statistics

type State

type State int32

State represents the state of a circuit breaker

const (
	// StateClosed allows requests to pass through
	StateClosed State = iota

	// StateOpen blocks all requests
	StateOpen

	// StateHalfOpen allows limited requests to test recovery
	StateHalfOpen
)

func (State) String

func (s State) String() string

String returns the string representation of the state

type Stats

type Stats struct {
	// Current number of connections
	Size int32

	// Number of idle connections
	Idle int32

	// Number of active connections
	Active int32

	// Total connections created
	TotalCreated int64

	// Total connections closed
	TotalClosed int64

	// Total acquire operations
	TotalAcquires int64

	// Total acquire timeouts
	TotalTimeouts int64

	// Total health check failures
	TotalHealthCheckFailures int64
}

Stats holds pool statistics

Jump to

Keyboard shortcuts

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