pool

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2023 License: MIT Imports: 10 Imported by: 24

Documentation

Index

Constants

View Source
const (
	// DefaultMaxGoroutinesAmount is the default maximum amount of goroutines.
	DefaultMaxGoroutinesAmount = 256 * 1024
	// DefaultMaxGoroutineIdleDuration is the default maximum idle duration of a goroutine.
	DefaultMaxGoroutineIdleDuration = 10 * time.Second
)

Variables

View Source
var ErrExpired = errors.New("resPool: getting expired resource")

ErrExpired error: getting expired resource.

View Source
var ErrLack = errors.New("lack of goroutines, because exceeded maxGoroutinesAmount limit")
View Source
var (
	// ErrWorkshopClosed error: 'workshop is closed'
	ErrWorkshopClosed = fmt.Errorf("%s", "workshop is closed")
)

Functions

This section is empty.

Types

type Avatar

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

Avatar links a Resource with a mutex, to be held during all calls into the Avatar.

func (*Avatar) Free

func (avatar *Avatar) Free(err error)

Free releases self to the ResPool. If error is not nil, close it.

func (*Avatar) ResPool

func (avatar *Avatar) ResPool() ResPool

ResPool returns ResPool to which it belongs.

type GoPool

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

GoPool executes concurrently incoming function via a pool of goroutines in FILO order, i.e. the most recently stopped goroutine will execute the next incoming function.

Such a scheme keeps CPU caches hot (in theory).

func NewGoPool

func NewGoPool(maxGoroutinesAmount int, maxGoroutineIdleDuration time.Duration) *GoPool

NewGoPool creates a new *GoPool. If maxGoroutinesAmount<=0, will use default value. If maxGoroutineIdleDuration<=0, will use default value.

func (*GoPool) Go

func (gp *GoPool) Go(fn func()) error

Go executes the function via a goroutine. If returns non-nil, the function cannot be executed because exceeded maxGoroutinesAmount limit.

func (*GoPool) MaxGoroutineIdle

func (gp *GoPool) MaxGoroutineIdle() time.Duration

MaxGoroutineIdle returns the max idle duration of the goroutine.

func (*GoPool) MaxGoroutinesAmount

func (gp *GoPool) MaxGoroutinesAmount() int

MaxGoroutinesAmount returns the max amount of goroutines.

func (*GoPool) MustGo

func (gp *GoPool) MustGo(fn func(), ctx ...context.Context) error

MustGo always try to use goroutine callbacks until execution is complete or the context is canceled.

func (*GoPool) Stop

func (gp *GoPool) Stop()

Stop starts GoPool. If calling 'Go' after calling 'Stop', will no longer reuse goroutine.

func (*GoPool) TryGo

func (gp *GoPool) TryGo(fn func())

TryGo tries to execute the function via goroutine. If there are no concurrent resources, execute it synchronously.

type ResPool

type ResPool interface {
	// Name returns the name.
	Name() string
	// Get returns a object in Resource type.
	Get() (Resource, error)
	// GetContext returns a object in Resource type.
	// Support context cancellation.
	GetContext(context.Context) (Resource, error)
	// Put gives a resource back to the ResPool.
	// If error is not nil, close the avatar.
	Put(Resource, error)
	// Callback callbacks your handle function, returns the error of getting resource or handling.
	// Support recover panic.
	Callback(func(Resource) error) error
	// Callback callbacks your handle function, returns the error of getting resource or handling.
	// Support recover panic and context cancellation.
	CallbackContext(context.Context, func(Resource) error) error
	// SetMaxLifetime sets the maximum amount of time a resource may be reused.
	//
	// Expired resource may be closed lazily before reuse.
	//
	// If d <= 0, resource are reused forever.
	SetMaxLifetime(d time.Duration)
	// SetMaxIdle sets the maximum number of resources in the idle
	// resource pool.
	//
	// If SetMaxIdle is greater than 0 but less than the new MaxIdle
	// then the new MaxIdle will be reduced to match the SetMaxIdle limit
	//
	// If n <= 0, no idle resources are retained.
	SetMaxIdle(n int)
	// SetMaxOpen sets the maximum number of open resources.
	//
	// If MaxIdle is greater than 0 and the new MaxOpen is less than
	// MaxIdle, then MaxIdle will be reduced to match the new
	// MaxOpen limit
	//
	// If n <= 0, then there is no limit on the number of open resources.
	// The default is 0 (unlimited).
	SetMaxOpen(n int)
	// Close closes the ResPool, releasing any open resources.
	//
	// It is rare to close a ResPool, as the ResPool handle is meant to be
	// long-lived and shared between many goroutines.
	Close() error
	// Stats returns resource statistics.
	Stats() ResPoolStats
}

ResPool is a high availability/high concurrent resource pool, which automatically manages the number of resources. So it is similar to database/sql's db pool.

ResPool is a pool of zero or more underlying avatar(resource). It's safe for concurrent use by multiple goroutines. ResPool creates and frees resource automatically; it also maintains a free pool of idle avatar(resource).

func NewResPool

func NewResPool(name string, newfunc func(context.Context) (Resource, error)) ResPool

NewResPool creates ResPool.

type ResPoolStats

type ResPoolStats struct {
	// OpenResources is the number of open resources to the resource.
	OpenResources   int
	FreeResources   int
	ClosedResources uint64
}

ResPoolStats contains resource statistics.

type ResPools

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

ResPools stores ResPool

func NewResPools

func NewResPools() *ResPools

NewResPools creates a new ResPools.

func (*ResPools) Clean

func (c *ResPools) Clean()

Clean delects and close all the ResPools.

func (*ResPools) Del

func (c *ResPools) Del(name string)

Del delects ResPool by name, and close the ResPool.

func (*ResPools) Get

func (c *ResPools) Get(name string) (ResPool, bool)

Get gets ResPool by name.

func (*ResPools) GetAll

func (c *ResPools) GetAll() []ResPool

GetAll gets all the ResPools.

func (*ResPools) Set

func (c *ResPools) Set(pool ResPool)

Set stores ResPool. If the same name exists, will close and cover it.

type Resource

type Resource interface {
	// SetAvatar stores the contact with resPool
	// Do not call it yourself, it is only called by (*ResPool).get, and will only be called once
	SetAvatar(*Avatar)
	// GetAvatar gets the contact with resPool
	// Do not call it yourself, it is only called by (*ResPool).Put
	GetAvatar() *Avatar
	// Close closes the original source
	// No need to call it yourself, it is only called by (*Avatar).close
	Close() error
}

Resource is a resource that can be stored in the ResPool.

type Worker

type Worker interface {
	Health() bool
	Close() error
}

Worker woker interface Note: Worker can not be implemented using empty structures(struct{})!

type Workshop

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

Workshop working workshop

func NewWorkshop

func NewWorkshop(maxQuota int, maxIdleDuration time.Duration, newWorkerFunc func() (Worker, error)) *Workshop

NewWorkshop creates a new workshop(non-blocking asynchronous multiplex resource pool). If maxQuota<=0, will use default value. If maxIdleDuration<=0, will use default value. Note: Worker can not be implemented using empty structures(struct{})!

func (*Workshop) Callback

func (w *Workshop) Callback(fn func(Worker) error) (err error)

Callback assigns a healthy worker to execute the function.

func (*Workshop) Close

func (w *Workshop) Close()

Close wait for all the work to be completed and close the workshop.

func (*Workshop) Fire

func (w *Workshop) Fire(worker Worker)

Fire marks the worker to reduce a job. If the worker does not belong to the workshop, close the worker.

func (*Workshop) Hire

func (w *Workshop) Hire() (Worker, error)

Hire hires a healthy worker and marks the worker to add a job.

func (*Workshop) Stats

func (w *Workshop) Stats() WorkshopStats

Stats returns the current workshop stats.

type WorkshopStats

type WorkshopStats struct {
	Worker  int32  // The current total number of workers
	Idle    int32  // The current total number of idle workers
	Created uint64 // Total created workers
	Doing   int32  // The total number of tasks in progress
	Done    uint64 // The total number of tasks completed
	MaxLoad int32  // In the current load balancing, the maximum number of tasks
	MinLoad int32  // In the current load balancing, the minimum number of tasks
}

WorkshopStats workshop stats

Jump to

Keyboard shortcuts

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