resourcepool

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2021 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Resource

type Resource struct {
	// The number of times this resource was fetched from the pool.
	Uses int

	// The time.Now().Unix() for when this object was created.
	CreatedAt int64

	// The created resource.
	Resource interface{}

	// The pool this resource came from.
	Pool *ResourcePool
}

A resource record.

func (*Resource) Close added in v0.0.7

func (r *Resource) Close() error

func (*Resource) Destroy

func (r *Resource) Destroy() error

type ResourceManager

type ResourceManager interface {
	// Create a resource and return it.
	Create() (interface{}, error)

	// Check if a resource should be destroyed.
	Check(interface{}) error

	// Destroy a resource.
	Destroy(interface{}) error
}

How to move a resource through its lifetime. Create, Check, and Destroy.

type ResourcePool

type ResourcePool struct {
	MaxUses         int
	MaxAge          int64
	ResourceManager ResourceManager
	FreeResources   chan *Resource
	CreateResources chan int
}

func NewResourcePool

func NewResourcePool(
	resourceManager ResourceManager,
	maxInstances int,
	maxUses int,
	maxAge int64,
) (*ResourcePool, error)

func (*ResourcePool) DestroyResource

func (pool *ResourcePool) DestroyResource(r *Resource) error

func (*ResourcePool) GetResource

func (pool *ResourcePool) GetResource() (*Resource, error)

func (*ResourcePool) ReturnResource

func (pool *ResourcePool) ReturnResource(r *Resource) error

func (*ResourcePool) UseResource

func (pool *ResourcePool) UseResource(f func(*Resource) error) error

Jump to

Keyboard shortcuts

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