1
0

handle uninitialized state

This commit is contained in:
Arpad Ryszka 2026-05-10 21:17:54 +02:00
parent 087b58e5dd
commit 6b7b9dc977
2 changed files with 40 additions and 4 deletions

View File

@ -9,13 +9,13 @@ fmt: $(sources)
go fmt
check: $(sources)
go test -count 1
go test -v -count 1
race: $(source)
go test -count 1 -race
go test -v -count 1 -race
.cover: $(sources)
go test -count 1 -coverprofile .cover
go test -v -count 1 -coverprofile .cover
cover: .cover
go tool cover -func .cover

38
lib.go
View File

@ -158,10 +158,13 @@ type Pool[R any] struct {
pool pool[R]
}
// ErrNoItems is returned on Get calls when the pool does not enough items and it was initialized without an
// ErrNoItems is returned on Get calls when the pool does not have enough items and it was initialized without an
// allocate function.
var ErrNoItems = errors.New("not enough items in the pool")
// ErrUninitialized is returned when the pool was not initialized.
var ErrUninitialized = errors.New("pool uninitialized")
// String returns the string representation of the EventType binary flag, including all the flags that are set.
func (et EventType) String() string {
var s []string
@ -283,6 +286,10 @@ func Make[R any](alloc func() (R, error), free func(R), o Options) Pool[R] {
// function returns an error, it returns the available items and that error. If events were configured, GetN
// triggers a GetOperation event.
func (p Pool[R]) GetN(n int) ([]R, error) {
if p.pool.state == nil {
return nil, ErrUninitialized
}
return p.pool.get(n)
}
@ -290,6 +297,11 @@ func (p Pool[R]) GetN(n int) ([]R, error) {
// 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.
func (p Pool[R]) Get() (R, error) {
if p.pool.state == nil {
var r R
return r, ErrUninitialized
}
r, err := p.pool.get(1)
if err != nil {
var rr R
@ -306,6 +318,10 @@ func (p Pool[R]) Get() (R, error) {
// 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.
func (p Pool[R]) Put(i ...R) {
if p.pool.state == nil {
return
}
p.pool.put(i...)
}
@ -315,11 +331,19 @@ func (p Pool[R]) Put(i ...R) {
// shrinking algorithm, it may work more consistently, if the algorithm is notified about the change in the
// active items. The other purpose is to allow the pool to reflect these cases in the events and the stats.
func (p Pool[R]) DropN(n int) {
if p.pool.state == nil {
return
}
p.pool.drop(n)
}
// Drop is like DropN, but for a single item.
func (p Pool[R]) Drop() {
if p.pool.state == nil {
return
}
p.pool.drop(1)
}
@ -327,17 +351,29 @@ func (p Pool[R]) Drop() {
// It can be useful in scenarios where prewarming or preparing for a sudden traffic spike is necessary. If
// events were configured, it triggers a LoadOperation event.
func (p Pool[R]) Load(i []R) {
if p.pool.state == nil {
return
}
p.pool.load(i)
}
// 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.
func (p Pool[R]) Stats() Stats {
if p.pool.state == nil {
return Stats{}
}
return p.pool.stats()
}
// 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.
func (p Pool[R]) Free() {
if p.pool.state == nil {
return
}
p.pool.freePool()
}