Documentation ¶
Index ¶
Constants ¶
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 ¶
var ErrExpired = errors.New("resPool: getting expired resource")
ErrExpired error: getting expired resource.
var ErrLack = errors.New("lack of goroutines, because exceeded maxGoroutinesAmount limit.")
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.
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 ¶
NewGoPool creates a new *GoPool. If maxGoroutinesAmount<=0, will use default value. If maxGoroutineIdleDuration<=0, will use default value.
func (*GoPool) Go ¶
Go executes the function via a goroutine. If returns non-nil, the function cannot be executed because exceeded maxGoroutinesAmount limit.
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).
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
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 ¶
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) Close ¶
func (w *Workshop) Close()
Close wait for all the work to be completed and close the workshop.
func (*Workshop) Fire ¶
Fire marks the worker to reduce a job. If the worker does not belong to the workshop, close the worker.
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