poolba

package
v0.0.0-rc10 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConstructorFunc

type ConstructorFunc[T any] func(context.Context) (T, error)

ConstructorFunc will be called for any uninitialized resource upon fetch from pool

type DestructorFunc

type DestructorFunc[T any] func(T) error

DestructorFunc will be called for any closed resource before it will be returned to pool

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

Pool represents thread-safe generic resource pool of fixed size

func PoolOf

func PoolOf[T any](size int, opts ...PoolOpt[T]) (*Pool[T], error)

PoolOf returns new generic resource pool of given size

func (*Pool[T]) Borrow

func (p *Pool[T]) Borrow(ctx context.Context) (*Resource[T], error)

Borrow withdraws resource from pool. Result resource cannot be acquired by another thread, so it must be eventually returned back to pool via Vacay method. This method will block code execution until resource is available, given context canceled or pool closed

func (*Pool[T]) Close

func (p *Pool[T]) Close() (err error)

Close closes pool entirely and destructs all initialized resources. Pool cannot be used after close

type PoolOpt

type PoolOpt[T any] func(pool *Pool[T]) error

func WithConstructor

func WithConstructor[T any](fn ConstructorFunc[T]) PoolOpt[T]

func WithDestructor

func WithDestructor[T any](fn DestructorFunc[T]) PoolOpt[T]

type Resource

type Resource[T any] struct {
	// contains filtered or unexported fields
}

Resource represents generic reusable resource

func (*Resource[T]) Close

func (r *Resource[T]) Close() error

Close deinitializes resource and returns it back to pool. Be aware that resource can to closed exactly once to avoid data race conditions and resource leaks

func (*Resource[T]) Vacay

func (r *Resource[T]) Vacay()

Vacay returns resource back to pool. It can be reused without initialization later by another thread. Be aware that resource can be vacated exactly once to avoid data race conditions and resource leaks

func (*Resource[T]) Value

func (r *Resource[T]) Value() T

Value returns actual user defined generic resource

Jump to

Keyboard shortcuts

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