Documentation
¶
Overview ¶
Package pool provides a generic connection pool implementation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPoolClosed is returned when an operation is attempted on a closed pool. ErrPoolClosed = fmt.Errorf("pool has been closed") // ErrPoolFull is returned when the pool has reached its maximum capacity and // cannot create new clients. ErrPoolFull = fmt.Errorf("pool is full") )
Functions ¶
This section is empty.
Types ¶
type ClosableClient ¶
type ClosableClient interface {
// Close terminates the client's connection.
Close() error
// IsHealthy returns true if the client's connection is active and usable.
IsHealthy(ctx context.Context) bool
}
ClosableClient defines the interface for clients that can be managed by the connection pool. Implementations must provide methods for closing the connection and checking its health.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager provides a way to manage multiple named connection pools. It allows for registering, retrieving, and closing pools in a centralized manner, which is useful for applications that need to connect to multiple upstream services.
func NewManager ¶
func NewManager() *Manager
NewManager creates and returns a new pool Manager for managing multiple named connection pools.
func (*Manager) CloseAll ¶
func (m *Manager) CloseAll()
CloseAll iterates through all registered pools in the manager and closes them, releasing all their associated resources. This is typically called during a graceful shutdown of the application.
func (*Manager) Deregister ¶
Deregister closes and removes a pool from the manager.
func (*Manager) Register ¶
Register adds a new pool to the manager under a given name. If a pool with the same name already exists, the old pool is closed before the new one is registered. This ensures that there are no resource leaks from orphaned pools.
Parameters:
- name: The name to register the pool under.
- pool: The pool to register.
type Pool ¶
type Pool[T ClosableClient] interface { // Get retrieves a client from the pool. If no idle clients are available and // the pool is not full, it may create a new one. Get(ctx context.Context) (T, error) // Put returns a client to the pool, making it available for reuse. Put(T) // Close terminates all clients in the pool and prevents new ones from being // created. Close() error // Len returns the number of idle clients currently in the pool. Len() int }
Pool defines the interface for a generic connection pool. It supports getting and putting clients, closing the pool, and querying its size. The type parameter T is constrained to types that implement the ClosableClient interface.
func Get ¶
func Get[T ClosableClient](m *Manager, name string) (Pool[T], bool)
Get retrieves a typed pool from the manager by name. It uses a type parameter `T` to ensure that the returned pool is of the expected type.
Parameters:
- m: The Manager instance.
- name: The name of the pool to retrieve.
Returns the typed `Pool` and a boolean indicating whether the pool was found and of the correct type.
func New ¶
func New[T ClosableClient]( factory func(context.Context) (T, error), minSize, maxSize int, _ time.Duration, disableHealthCheck bool, ) (Pool[T], error)
New creates a new connection pool with the specified factory and size constraints. The pool is initialized with a minimum number of clients and can grow up to a maximum size.
The type parameter `T` is constrained to types that implement the `ClosableClient` interface.
Parameters:
- factory: A function that creates new clients.
- minSize: The initial number of clients to create.
- maxSize: The maximum number of clients the pool can hold.
- idleTimeout: (Not yet used) Intended for future implementation of idle connection handling.
Returns a new `Pool` instance or an error if the configuration is invalid.
type UntypedPool ¶
type UntypedPool interface {
io.Closer
// Len returns the number of idle clients currently in the pool.
Len() int
}
UntypedPool defines a non-generic interface for a pool, allowing for management of pools of different types in a single collection.