1
0
pool/lib.go

295 lines
12 KiB
Go
Raw Normal View History

2026-03-14 21:33:59 +01:00
// Package pool provides a resource pool implementation that is safe to access from multiple goroutines.
//
// The pool supports finalizing items when shrinking the pool. It helps monitoring the pool usage and state with
// events and statistics. While it implements max idle size and timeout based shrinking algorithms to release
// idle resources from the pool, it also provides a zero-config adaptive algorithm for this purpose that can
// automatically adapt to changing resource usage characteristics. It also accepts custom algorithm
// implementations.
2026-03-03 19:50:09 +01:00
package pool
import (
2026-03-05 12:34:35 +01:00
"code.squareroundforest.org/arpio/syncbus"
"code.squareroundforest.org/arpio/times"
2026-03-03 19:50:09 +01:00
"errors"
2026-03-05 12:34:35 +01:00
"fmt"
"strings"
2026-03-03 19:50:09 +01:00
"time"
)
2026-03-14 21:33:59 +01:00
// Stats provides information about the pool state.
2026-03-03 19:50:09 +01:00
type Stats struct {
2026-03-14 21:33:59 +01:00
// Idle is the number of resources currently held by the pool.
Idle int
// Active is the nubmer of resources that are currently in use as known by the pool.
2026-03-03 19:50:09 +01:00
Active int
2026-03-14 21:33:59 +01:00
// Get is the number of get operations during the entire life cycle of the pool.
Get int
// Put is the number of put operations during the entire life cycle of the pool.
Put int
// Alloc is the number of allocations executed by the pool during the entire life cycle of the pool.
Alloc int
// Load is the total number of items explicitly added to the pool via the Load method.
Load int
// Free is the number of deallocations executed by the pool during the entire life cycle of the pool.
Free int
2026-03-03 19:50:09 +01:00
}
2026-03-14 21:33:59 +01:00
// EventType is a binary flag categorizing the reason of an event. The types of events can be combined together,
// e.g. if a get operation requires an allocate operation, then the event type will be
// GetOperation|AllocateOperation.
2026-03-03 19:50:09 +01:00
type EventType int
const (
2026-03-14 21:33:59 +01:00
// None can be used to mask out all event types and not receiving any events.
None EventType = 0
// GetOperation is the type of events sent after a get operation.
2026-03-03 19:50:09 +01:00
GetOperation EventType = 1 << iota
2026-03-14 21:33:59 +01:00
// PutOperation is the type of events sent after a put operation.
2026-03-03 19:50:09 +01:00
PutOperation
2026-03-14 21:33:59 +01:00
// AllocateOperation is the type of events sent after an allocate operation.
2026-03-03 19:50:09 +01:00
AllocateOperation
2026-03-14 21:33:59 +01:00
// LoadOperation is the type of events sent after a load operation.
LoadOperation
// FreeOperation is the type of events sent after a free operation.
2026-03-03 19:50:09 +01:00
FreeOperation
2026-03-14 21:33:59 +01:00
// AllocateError is the type of events sent after a failed allocation. The error will not be included
// with the event, but it will be returned by the failed Get function call.
2026-03-03 19:50:09 +01:00
AllocateError
2026-03-05 12:34:35 +01:00
2026-03-14 21:33:59 +01:00
// AllEvents can be used as a mask that includes all the event types.
2026-03-05 12:34:35 +01:00
AllEvents = GetOperation | PutOperation | AllocateOperation | FreeOperation | AllocateError
2026-03-03 19:50:09 +01:00
)
2026-03-14 21:33:59 +01:00
// Event values are sent by the pool after various operations, if it is configured to use a channel the send the
// events to.
2026-03-03 19:50:09 +01:00
type Event struct {
2026-03-14 21:33:59 +01:00
// Type is the binary flag depicting the type of the event.
Type EventType
// Stats contains the statistics about the pool at the time of the event.
2026-03-03 19:50:09 +01:00
Stats Stats
}
2026-03-14 21:33:59 +01:00
// Algo implementations control when the shrinking of the idle items in the pool happens. The pool can be used
// with the implementations provided by this package or custom ones. The implementation can hold internal state,
// the pool guarantees that the algo instance is accessed only from one goroutine at a time.
2026-03-03 19:50:09 +01:00
type Algo interface {
2026-03-14 21:33:59 +01:00
// Target is called on every Put operation with the current pool state as the input. It is expected to
// return the target idle count, as dictated by the implementing algorithm. Optionally, a timeout value
// can be returned (nextCheck), and if it is a positive value, the pool will call Target again after the
// defined time expires to see if the next target idle count. In each case, when Target returns a
// smaller number than the current idle count, it shrinks the pool to the defined target.
//
// When using nextCheck, not every returned nextCheck results in calling Target by the pool, only the
// ones that were set after the previous one expired.
//
// Implementations should consider that while the recommended way of using the pool is to only call Put
// with items that were received by calling Get, the pool itself doesn't prohibit calling Put with
// 'foreign' items.
2026-03-14 19:03:18 +01:00
Target(Stats) (target int, nextCheck time.Duration)
2026-03-14 21:33:59 +01:00
// Load is called by the pool when Pool.Load is used, passing in the number of items that were loaded as
// the result of a 'prewarm' or other preallocation. The number of loaded items is passed in as
// argument, which can be used to adjust the algorithm's internal state. If the implemented algorithm is
// stateless, or it is not sensitive to loading items this way, Load can be implemented as a noop.
2026-03-14 19:03:18 +01:00
Load(int)
2026-03-03 19:50:09 +01:00
}
2026-03-14 21:33:59 +01:00
// Options can be used to configure the pool. Some of the options are provided to support testing various
// scenarios.
2026-03-03 19:50:09 +01:00
type Options struct {
2026-03-14 21:33:59 +01:00
// Events is a channel that, when set, the pool is using for sending events. The channel needs to be
// used together with a non-default event mask set. When using events, we should consider to use a
// buffered channel. Events can be dropped if the consumer is blocked and the channel is not ready to
// communicate at the time of the event.
2026-03-03 19:50:09 +01:00
Events chan<- Event
2026-03-14 21:33:59 +01:00
// EventMask is a binary flag that defines which events will be sent to the provided channel. The
// default is no events.
2026-03-03 19:50:09 +01:00
EventMask EventType
2026-03-14 21:33:59 +01:00
// Algo is the algorithm implementation used for shrinking the pool. The default is Adaptive().
Algo Algo
// Clock is an optional clock meant to be used with testing. The main purpose is to avoid time sensitive
// tests running for a too long time.
Clock times.Clock
// TestBus is an optional signal bus meant to be used with testing. The main purpose is to ensure that
// specific blocks of code are executed in a predefiend order during concurrent tests.
TestBus *syncbus.SyncBus
2026-03-03 19:50:09 +01:00
}
2026-03-14 21:33:59 +01:00
// Pool is a synchronized resource pool of resources, that are considered expensive to allocate. Initialize the
// pool with the Make() function. Methods of uninitialized Pool instances may block forever. For the usage of
// the pool, see the docs of its method, initialization options and the provided algorithms.
2026-03-03 19:50:09 +01:00
type Pool[R any] struct {
pool pool[R]
}
2026-03-14 21:33:59 +01:00
// ErrEmpty is returned on Get calls when the pool is empty and it was initialized without an allocate function.
2026-03-05 12:34:35 +01:00
var ErrEmpty = errors.New("empty pool")
2026-03-14 21:33:59 +01:00
// String returns the string representation of the EventType binary flag, including all the flags that are set.
2026-03-05 12:34:35 +01:00
func (et EventType) String() string {
var s []string
if et&GetOperation != 0 {
s = append(s, "get")
}
if et&PutOperation != 0 {
s = append(s, "put")
}
if et&AllocateOperation != 0 {
s = append(s, "allocate")
}
2026-03-14 21:33:59 +01:00
if et&LoadOperation != 0 {
s = append(s, "load")
}
2026-03-05 12:34:35 +01:00
if et&FreeOperation != 0 {
s = append(s, "free")
}
if et&AllocateError != 0 {
s = append(s, "allocerr")
}
if len(s) == 0 {
return "none"
}
return strings.Join(s, "|")
}
2026-03-14 21:33:59 +01:00
// String returns the string representation of an Event value, including the type and statistics.
2026-03-05 12:34:35 +01:00
func (ev Event) String() string {
return fmt.Sprintf("%v; %v", ev.Type, ev.Stats)
}
2026-03-14 21:33:59 +01:00
// String returns the string representation of a set of statistics about the pool.
2026-03-05 12:34:35 +01:00
func (s Stats) String() string {
return fmt.Sprintf(
2026-03-14 21:33:59 +01:00
"idle: %d, active: %d, get: %d, put: %d, alloc: %d, load: %d, free: %d",
2026-03-05 12:34:35 +01:00
s.Idle,
s.Active,
s.Get,
s.Put,
s.Alloc,
2026-03-14 21:33:59 +01:00
s.Load,
2026-03-05 12:34:35 +01:00
s.Free,
)
}
2026-03-03 19:50:09 +01:00
2026-03-14 21:33:59 +01:00
// Adaptive creates a zero-config pool shrink algorithm instance. It is the default algorithm used by the pool.
//
// It is based on exponential moving average of the active items and the deviation of it. This way it can react
// to, and to some extent overbuild, on the perceived stress. It decays the number of idle items gradually, and
// on very sudden drops in traffic, it ensures the eventual release of all pooled items with a background job,
// that is timed based on the duration of the last active usage session, which is the time while there were
// active items. Together with the pool implementation, it always reuses the most recent items, as in LIFO for
// Get and FIFO for Free.
//
// We need to be aware of some potential caveats due to its zero-config nature. It doesn't use any absolute
// values, like a timing parameter. It only considers the sequence of the pool states. This can result in
// behaviour that, without understanding this zero-config nature, might be unexpected. A trivial example is that
// the algorithm doesn't differentiate between periodic long grow/shrink/steady patterns and periodic short
// spikes/steady patterns. In short, it can happen that: __/\__/\__/\__ = _|_|_|_. Instead, it optimizes for
// having enough but no more 'capacity' (idle items) for the predicated 'load' (active items).
2026-03-03 19:50:09 +01:00
func Adaptive() Algo {
return makeAdaptiveAlgo()
}
2026-03-14 21:33:59 +01:00
// MaxTimeout creates a pool shrink algorithm instance, that releases items whenever the number of idle items
// would be greater than max, and it also releases those items that were idle for too long. Together with the
// pool, it ensures that the Get operation is LIFO and the Free operation is FIFO.
//
// If max <= 0, the max pool size is not enforced. If to <= 0, the timeout is not enforced.
2026-03-03 19:50:09 +01:00
func MaxTimeout(max int, to time.Duration) Algo {
return makeMaxTimeout(max, to)
}
2026-03-14 21:33:59 +01:00
// Max is like MaxTimeout, but without the mas idle time.
2026-03-03 19:50:09 +01:00
func Max(max int) Algo {
return makeMaxTimeout(max, 0)
}
2026-03-14 21:33:59 +01:00
// Timeout is like MaxTimeout, but without the max pool size.
2026-03-03 19:50:09 +01:00
func Timeout(to time.Duration) Algo {
return makeMaxTimeout(0, to)
}
2026-03-14 21:33:59 +01:00
// NoShrink is a noop shrink algorithm, it doesn't release any idle items. The user code can decide whether to
// put back items in the pool or not. It might be useful in certain testing scenarios.
2026-03-03 19:50:09 +01:00
func NoShrink() Algo {
return makeMaxTimeout(0, 0)
}
2026-03-14 21:33:59 +01:00
// Make initializes a Pool instance.
//
// The paramter alloc is used on Get operations when the pool is empty. If alloc is nil, and the pool is empty
// at the time of calling Get, Get will return ErrEmpty. If alloc returns an error, the same error is returned
// by Get. If events were configured, alloc triggers AllocateOperation event. This event is typically the same
// as the GetOperation event.
//
// The parameter free is called when an item is released from the pool, with the item being released as the
// argument. It can be nil for resource types that don't need explicit deallocation. If events were configured,
// releasing an item triggers a FreeOperation event, regardless if the free parameter is nil.
2026-03-03 19:50:09 +01:00
func Make[R any](alloc func() (R, error), free func(R), o Options) Pool[R] {
return Pool[R]{pool: makePool(alloc, free, o)}
}
2026-03-14 21:33:59 +01:00
// Get returns an item from the pool. If the pool is empty and no allocation function was configured, it returns
// ErrEmpty. If the pool is empty, and the allocation function returns an error, it returns that error. If
// events were configured, Get triggers a GetOperation event.
2026-03-03 19:50:09 +01:00
func (p Pool[R]) Get() (R, error) {
return p.pool.get()
}
2026-03-14 21:33:59 +01:00
// Put stores an item in the pool. If events were configured, it triggers a PutOperation event.
//
// It is recommended to use it only with items that were received by the Get method. While it is allowed to put
// other items in the pool, it may change the way the shrinking algorithm works. E.g. it can be considered as a
// sudden drop in the number of active items. If the pool needs to be prewarmed, or prepared for an expected
// spike of traffic, consider using the Load method.
2026-03-03 19:50:09 +01:00
func (p Pool[R]) Put(i R) {
p.pool.put(i)
}
2026-03-14 21:33:59 +01:00
// Load can be used to populate the pool with items that were not allocated as the result of the Get operation.
// It can be useful in scenarios where prewarming or preparation for an expected sudden traffic spike is
// expected. If events were configured, it triggers a LoadOperation event.
2026-03-14 19:03:18 +01:00
func (p Pool[R]) Load(i []R) {
p.pool.load(i)
}
2026-03-14 21:33:59 +01:00
// Stats returns statistics about the current state of the pool. It contains the current number of active/idle
// items, and perpetual counters for the various pool operations.
2026-03-03 19:50:09 +01:00
func (p Pool[R]) Stats() Stats {
return p.pool.stats()
}
2026-03-14 21:33:59 +01:00
// Free releases all idle items in the pool. While the pool stays operational, Free is meant to be used when the
// pool is not required anymore.
2026-03-03 19:50:09 +01:00
func (p Pool[R]) Free() {
p.pool.freePool()
}