pool

package
Version: v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultReadBufferGCInterval is the default interval that a Read will
	// perform a sweep to see which expired buffer.Reads can be released to
	// the runtime.
	DefaultReadBufferGCInterval = 15 * time.Second

	// DefaultReadBufferExpiryInterval is the default, minimum interval that
	// must elapse before a Read will release a buffer.Read. The maximum
	// time before the buffer can be released is equal to the expiry
	// interval plus the gc interval.
	DefaultReadBufferExpiryInterval = 30 * time.Second
)
View Source
const (
	// DefaultWriteBufferGCInterval is the default interval that a Write
	// will perform a sweep to see which expired buffer.Writes can be
	// released to the runtime.
	DefaultWriteBufferGCInterval = 15 * time.Second

	// DefaultWriteBufferExpiryInterval is the default, minimum interval
	// that must elapse before a Write will release a buffer.Write.  The
	// maximum time before the buffer can be released is equal to the expiry
	// interval plus the gc interval.
	DefaultWriteBufferExpiryInterval = 30 * time.Second
)
View Source
const DefaultWorkerTimeout = 90 * time.Second

DefaultWorkerTimeout is the default duration after which a worker goroutine will exit to free up resources after having received no newly submitted tasks.

Variables

View Source
var ErrWorkerPoolExiting = errors.New("worker pool exiting")

ErrWorkerPoolExiting signals that a shutdown of the Worker has been requested.

Functions

This section is empty.

Types

type Read

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

Read is a worker pool specifically designed for sharing access to buffer.Read objects amongst a set of worker goroutines. This enables an application to limit the total number of buffer.Read objects allocated at any given time.

func NewRead

func NewRead(readBufferPool *ReadBuffer, numWorkers int,
	workerTimeout time.Duration) *Read

NewRead creates a new Read pool, using an underlying ReadBuffer pool to recycle buffer.Read objects across the lifetime of the Read pool's workers.

func (*Read) Start

func (r *Read) Start() error

Start safely spins up the Read pool.

func (*Read) Stop

func (r *Read) Stop() error

Stop safely shuts down the Read pool.

func (*Read) Submit

func (r *Read) Submit(inner func(*buffer.Read) error) error

Submit accepts a function closure that provides access to the fresh buffer.Read object. The function's execution will be allocated to one of the underlying Worker pool's goroutines.

type ReadBuffer

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

ReadBuffer is a pool of buffer.Read items, that dynamically allocates and reclaims buffers in response to load.

func NewReadBuffer

func NewReadBuffer(gcInterval, expiryInterval time.Duration) *ReadBuffer

NewReadBuffer returns a freshly instantiated ReadBuffer, using the given gcInterval and expieryInterval.

func (*ReadBuffer) Return

func (p *ReadBuffer) Return(buf *buffer.Read)

Return returns the buffer.Read to the pool, so that it can be cycled or released.

func (*ReadBuffer) Take

func (p *ReadBuffer) Take() *buffer.Read

Take returns a fresh buffer.Read to the caller.

type Recycle

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

Recycle is a generic queue for recycling objects implementing the Recycler interface. It is backed by an underlying queue.GCQueue, and invokes the Recycle method on returned objects before returning them to the queue.

func NewRecycle

func NewRecycle(newItem func() interface{}, returnQueueSize int,
	gcInterval, expiryInterval time.Duration) *Recycle

NewRecycle initializes a fresh Recycle instance.

func (*Recycle) Return

func (r *Recycle) Return(item Recycler)

Return returns an item implementing the Recycler interface to the pool. The Recycle method is invoked before returning the item to improve performance and utilization under load.

func (*Recycle) Take

func (r *Recycle) Take() interface{}

Take returns an element from the pool.

type Recycler

type Recycler interface {
	// Recycle resets the object to its default state.
	Recycle()
}

Recycler is an interface that allows an object to be reclaimed without needing to be returned to the runtime.

type Worker

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

Worker maintains a pool of goroutines that process submitted function closures, and enable more efficient reuse of expensive state.

func NewWorker

func NewWorker(cfg *WorkerConfig) *Worker

NewWorker initializes a new Worker pool using the provided WorkerConfig.

func (*Worker) Start

func (w *Worker) Start() error

Start safely spins up the Worker pool.

func (*Worker) Stop

func (w *Worker) Stop() error

Stop safely shuts down the Worker pool.

func (*Worker) Submit

func (w *Worker) Submit(fn func(WorkerState) error) error

Submit accepts a function closure to the worker pool. The returned error will be either the result of the closure's execution or an ErrWorkerPoolExiting if a shutdown is requested.

type WorkerConfig

type WorkerConfig struct {
	// NewWorkerState allocates a new state for a worker goroutine.
	// This method is called each time a new worker goroutine is
	// spawned by the pool.
	NewWorkerState func() WorkerState

	// NumWorkers is the maximum number of workers the Worker pool
	// will permit to be allocated. Once the maximum number is
	// reached, any newly submitted tasks are forced to be processed
	// by existing worker goroutines.
	NumWorkers int

	// WorkerTimeout is the duration after which a worker goroutine
	// will exit after having received no newly submitted tasks.
	WorkerTimeout time.Duration
}

WorkerConfig parameterizes the behavior of a Worker pool.

type WorkerState

type WorkerState interface {
	// Reset clears any internal state that may have been dirtied in
	// processing a prior task.
	Reset()

	// Cleanup releases any shared state before a worker goroutine
	// exits.
	Cleanup()
}

WorkerState is an interface used by the Worker to abstract the lifecycle of internal state used by a worker goroutine.

type Write

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

Write is a worker pool specifically designed for sharing access to buffer.Write objects amongst a set of worker goroutines. This enables an application to limit the total number of buffer.Write objects allocated at any given time.

func NewWrite

func NewWrite(writeBufferPool *WriteBuffer, numWorkers int,
	workerTimeout time.Duration) *Write

NewWrite creates a Write pool, using an underlying Writebuffer pool to recycle buffer.Write objects across the lifetime of the Write pool's workers.

func (*Write) Start

func (w *Write) Start() error

Start safely spins up the Write pool.

func (*Write) Stop

func (w *Write) Stop() error

Stop safely shuts down the Write pool.

func (*Write) Submit

func (w *Write) Submit(inner func(*bytes.Buffer) error) error

Submit accepts a function closure that provides access to a fresh bytes.Buffer backed by a buffer.Write object. The function's execution will be allocated to one of the underlying Worker pool's goroutines.

type WriteBuffer

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

WriteBuffer is a pool of recycled buffer.Write items, that dynamically allocates and reclaims buffers in response to load.

func NewWriteBuffer

func NewWriteBuffer(gcInterval, expiryInterval time.Duration) *WriteBuffer

NewWriteBuffer returns a freshly instantiated WriteBuffer, using the given gcInterval and expiryIntervals.

func (*WriteBuffer) Return

func (p *WriteBuffer) Return(buf *buffer.Write)

Return returns the buffer.Write to the pool, so that it can be recycled or released.

func (*WriteBuffer) Take

func (p *WriteBuffer) Take() *buffer.Write

Take returns a fresh buffer.Write to the caller.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL