resource

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("manager closed")

Functions

This section is empty.

Types

type CreateFunc

type CreateFunc[T Resource] func(ctx context.Context) (T, error)

type DestroyFunc

type DestroyFunc[T Resource] func(resource T) error

type Handle

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

func (*Handle[T]) Access

func (h *Handle[T]) Access() T

func (*Handle[T]) Destroy

func (h *Handle[T]) Destroy() error

func (*Handle[T]) Release

func (h *Handle[T]) Release()

type Manager

type Manager[T Resource] interface {
	// Acquire a managed resource from the manager, may block until the resource is available or the provided context is canceled.
	Acquire(context.Context) (*Handle[T], error)
	// Release signals to the manager that the resource is no longer in use by the caller, but can be used by others
	Release(*Handle[T])
	// Destroy signals to the manager that the resource needs to be cleaned up, usually because it is broken
	Destroy(*Handle[T]) error
	// Close blocks future calls to Acquire and destroys all allocated resources
	Close() error
}

type Pool

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

Pool is a Manager that will maintain up to the specified quantity of the managed resource.

func NewPool

func NewPool[T Resource](create CreateFunc[T], destroy DestroyFunc[T], capacity int) (*Pool[T], error)

func (*Pool[T]) Acquire

func (p *Pool[T]) Acquire(ctx context.Context) (*Handle[T], error)

Acquire will wait until capacity is available on the pool or the provided context is canceled.

  • If the context is canceled before capacity is available, ctx.Err() will be returned.
  • If capacity becomes available and there is already a resource allocated, it will be returned.
  • If capacity becomes available and the resource needs to be allocated, then the proveded CreateFunc will be called. On success, the created resource will be returned. On failure, the error from the CreateFunc will be returned, and the acquired capacity will be released.

func (*Pool[T]) Close

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

Close blocks future calls to Acquire and destroys all allocated resources

func (*Pool[T]) Destroy

func (p *Pool[T]) Destroy(h *Handle[T]) error

Destroy will pass the given resource to the provided DestroyFunc and remove it from the pool, freeing up capacity for a new resource to be allocated.

func (*Pool[T]) Release

func (p *Pool[T]) Release(h *Handle[T])

Release will place the given resource back into the pool, making it available to other callers of Acquire

type Resource

type Resource = any

type Shareable

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

Shareable is a Manager for a shareable resource, it maintains allocation of a single resource and allows multiple callers to acquire it.

func NewShareable

func NewShareable[T Resource](create CreateFunc[T], destroy DestroyFunc[T]) *Shareable[T]

func (*Shareable[T]) Acquire

func (s *Shareable[T]) Acquire(ctx context.Context) (*Handle[T], error)

Acquire will allocate the shareable resource or return it if it is already allocated

func (*Shareable[T]) Close

func (s *Shareable[T]) Close() error

Close blocks future calls to Acquire and destroys the managed resource

func (*Shareable[T]) Destroy

func (s *Shareable[T]) Destroy(h *Handle[T]) error

Destroy will deallocate the resource, the next call to Acquire will allocate a new resource

func (*Shareable[T]) Release

func (s *Shareable[T]) Release(_ *Handle[T])

Release is for Shareable resources is a no-op

Jump to

Keyboard shortcuts

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