gpool

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: MulanPSL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package gpool provides object-reusable concurrent-safe pool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpireFunc

type ExpireFunc func(interface{})

Destruction function for object.

type NewFunc

type NewFunc func() (interface{}, error)

Creation function for object.

type Pool

type Pool struct {
	TTL     time.Duration               // Time To Live for pool items.
	NewFunc func() (interface{}, error) // Callback function to create pool item.
	// ExpireFunc is the for expired items destruction.
	// This function needs to be defined when the pool items
	// need to perform additional destruction operations.
	// Eg: net.Conn, os.File, etc.
	ExpireFunc func(interface{})
	// contains filtered or unexported fields
}

Pool is an Object-Reusable Pool.

func New

func New(ttl time.Duration, newFunc NewFunc, expireFunc ...ExpireFunc) *Pool

New creates and returns a new object pool. To ensure execution efficiency, the expiration time cannot be modified once it is set.

Note the expiration logic: ttl = 0 : not expired; ttl < 0 : immediate expired after use; ttl > 0 : timeout expired;

func (*Pool) Clear

func (p *Pool) Clear()

Clear clears pool, which means it will remove all items from pool.

func (*Pool) Close

func (p *Pool) Close()

Close closes the pool. If <p> has ExpireFunc, then it automatically closes all items using this function before it's closed. Commonly you do not need call this function manually.

func (*Pool) Get

func (p *Pool) Get() (interface{}, error)

Get picks and returns an item from pool. If the pool is empty and NewFunc is defined, it creates and returns one from NewFunc.

func (*Pool) Put

func (p *Pool) Put(value interface{}) error

Put puts an item to pool.

func (*Pool) Size

func (p *Pool) Size() int

Size returns the count of available items of pool.

Jump to

Keyboard shortcuts

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