pool

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pool provides a generic resource pool.

Index

Constants

This section is empty.

Variables

View Source
var ErrPoolClosed = errors.New("pgtest: pool closed")

Functions

This section is empty.

Types

type Pool

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

Pool is a generic resource pool.

As the intended use-case for the pool is to maintain resources used by test-cases running in parallel, for simplicity there is currently no maximum number of resources owned by the pool. The thinking being that the maximum number of resources should already be constrained by the '-parallel' flag passed to 'go test' (or GOMAXPROCS by default), so it's not clear if there's a use-case where we'd want to set a different limit. If such a use-case arrives we can always adjust this definition to allow a maxSize to be specified and default to GOMAXPROCS, but for now it is simpler to just let it grow as needed.

func New

func New[T any](resourceConf *ResourceConf[T]) *Pool[T]

func (*Pool[T]) Acquire

func (pool *Pool[T]) Acquire(ctx context.Context) (*Resource[T], error)

Acquire acquires the resource from the pool. This can either be a newly created resource, or a previously created idle resource.

func (*Pool[T]) Close

func (pool *Pool[T]) Close(_ context.Context) error

Close cleans up the pool and prevents new resources from being acquired.

NOTE: for now we're just destroying ALL resources owned by the pool, not just the idle ones. This is primarily for simplicity, since the assumption is that pools will only be closed after all tests are done running. As a result, we can assume that nobody is using resources acquired from the pool when we close the pool. While this is likely not a valid assumption, since we probably want to allow for a graceful shutdown in certain cases, it seems better to stick with the simple approach for now then adjust as needed.

type Resource

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

A Resource is a resource maintained by a Pool.

func (*Resource[T]) Data

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

Data returns the underlying data associated with the resource.

func (*Resource[T]) Hijack

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

Hijack takes control of the resource from the pool.

func (*Resource[T]) Release

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

Release releases the resource back to the pool so it can be re-used.

type ResourceConf

type ResourceConf[T any] struct {
	Create  func(context.Context) (T, error)
	Destroy func(T) error
}

ResourceConf describes the configuration for dealing with resources owned by the pool.

Jump to

Keyboard shortcuts

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