objectpool

package
v2.7.3 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package objectpool provides Pool of interface PoolObject

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CreateObjectFn

type CreateObjectFn func() (PoolObject, error)

CreateObjectFn create new object, it must return a health object or err

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool manage the PoolObject

func New

func New(initSize, maxSize int, init func() (PoolObject, error)) *Pool

New returns a new pool

func NewWithSpec

func NewWithSpec(spec *Spec) *Pool

NewWithSpec returns a new pool

func (*Pool) Close

func (p *Pool) Close()

Close closes the pool and clean all the objects

func (*Pool) Get

func (p *Pool) Get(ctx context.Context, new CreateObjectFn) (PoolObject, error)

Get returns an object from the pool,

if there's an available object, it will return it directly; if there's no free object, it will create a one if the pool is not full; if the pool is full, it will block until an object is returned to the pool.

func (*Pool) Put

func (p *Pool) Put(obj PoolObject)

Put return the object to the pool

type PoolObject

type PoolObject interface {
	Destroy()          // destroy the object
	HealthCheck() bool // check the object is health or not
}

PoolObject is an interface that about definition of object that managed by pool

type Spec

type Spec struct {
	InitSize     int            // initial size
	MaxSize      int            // max size
	Init         CreateObjectFn // create init size object, it must return a health object or err
	CheckWhenGet bool           // whether to health check when get PoolObject
	CheckWhenPut bool           // whether to health check when put PoolObject
}

Spec Pool's spec

func (*Spec) Validate

func (s *Spec) Validate() error

Validate validate

Jump to

Keyboard shortcuts

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