The go-pooler
package provides a simple and generic resource pooler for managing reusable resources in Go. It is designed to handle resource acquisition, release, and health checks efficiently, making it suitable for high-concurrency applications.
- Acquiring and Releasing Resources by Key: Manage resources using unique keys.
- Configurable Maximum Number of Open Resources: Limit the number of open resources.
- Periodic Health Checks: Automatically perform health checks and resource cleanup at configurable intervals.
- Pool Statistics: Gather statistics about the pool's usage, such as the number of open resources and wait times.
To install the package, use:
go get github.com/bartventer/go-pooler
To create a new pool, define a resource that implements the Reusable interface and use the NewPool function:
package main
import (
"context"
"github.com/bartventer/go-pooler"
)
type Worker struct{}
type WorkerPool = pooler.Pool[*Worker]
func (w *Worker) Close() error { return nil }
func (w *Worker) PingContext(ctx context.Context) error { return nil }
func WorkerFactory() (*Worker, error) {
return &Worker{}, nil
}
func NewWorkerPool(ctx context.Context, opts ...pooler.Option) *WorkerPool {
return pooler.NewPool(ctx, WorkerFactory, opts...)
}
func main() {
ctx := context.Background()
p := NewWorkerPool(ctx, pooler.WithMaxOpenResources(1))
_, err := p.Acquire(ctx, "key1")
if err != nil {
panic(err)
}
defer p.Release("key1")
// Use the worker...
}
Refer to the GoDoc for detailed documentation and examples.
This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.