Help us shape the future of Go! Take the 2026 Go Developer Survey

pool

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2019 License: AGPL-3.0 Imports: 3 Imported by: 0

README

go-pool

This package contains a static and a dynamic pool which have:

  • Standard interface
  • Timeout for Get()
  • Callbacks for Get() and Put()
  • Persistence (items are not garbage-collected)

The static pool grows to its size after N many calls to Get(), where N equals the pool size. This is useful for cases where the number of items is also static, for example a worker pool where N many workers are always used.

The dynamic pool grows to its size if the rate of calls to Get() exceeds the rate of calls to Put(). Else, the pool size can, for example, remain at one if there's a single caller (in which case the same item is reused repeatedly).

Neither pool implements pruning or draining.

These pools were inspired by http://openmymind.net/Introduction-To-Go-Channels/, the built-in sync.Pool, and https://github.com/go-sql-driver/mysql.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTimeout   = errors.New("Timeout")
	ErrOverflow  = errors.New("Overflow")
	ErrUnderflow = errors.New("Underflow")
)

Functions

This section is empty.

Types

type DynamicPool

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

func NewDynamicPool

func NewDynamicPool(size uint, newFunc func() (interface{}, error), putFunc func(interface{})) *DynamicPool

func (*DynamicPool) Free

func (p *DynamicPool) Free() uint

func (*DynamicPool) Get

func (p *DynamicPool) Get(timeout time.Duration) (interface{}, error)

func (*DynamicPool) Put

func (p *DynamicPool) Put(v interface{}) error

func (*DynamicPool) Size

func (p *DynamicPool) Size() uint

type Pool

type Pool interface {
	Size() uint
	Free() uint
	Get(timeout time.Duration) (interface{}, error)
	Put(v interface{}) error
}

type StaticPool

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

func NewStaticPool

func NewStaticPool(size uint, newFunc func() (interface{}, error), putFunc func(interface{})) *StaticPool

func (*StaticPool) Free

func (p *StaticPool) Free() uint

func (*StaticPool) Get

func (p *StaticPool) Get(timeout time.Duration) (interface{}, error)

func (*StaticPool) Put

func (p *StaticPool) Put(v interface{}) error

func (*StaticPool) Size

func (p *StaticPool) Size() uint

Jump to

Keyboard shortcuts

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