Documentation ¶
Overview ¶
Package pool manages a pool of routines to perform work. It does so my providing a Do function that will block when the pool is busy. This also allows the pool to monitor and report pushback. The pool also supports the dynamic re-sizing of the number of routines in the pool.
Worker
type Worker interface { Work(ctx context.Context, id int) }
The Worker interface is how you can provide work to the pool. A user-defined type implements this interface, then values of that type can be passed into the Do function.
Sample Application ¶
https://github.com/ardanlabs/kit/blob/master/examples/pool/main.go
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilMinRoutines = errors.New("Invalid (nil) minimum number of routines") ErrNilMaxRoutines = errors.New("Invalid (nil) maximum number of routines") ErrInvalidMinRoutines = errors.New("Invalid minimum number of routines") ErrInvalidMaxRoutines = errors.New("Invalid maximum number of routines") ErrInvalidAdd = errors.New("Invalid number of routines to add") ErrInvalidMetricHandler = errors.New("Invalid metric handler") ErrInvalidMetricInterval = errors.New("Invalid metric interval") )
Set of error variables for start up.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { MinRoutines func() int // Initial and minimum number of routines always in the pool. MaxRoutines func() int // Maximum number of routines we will ever grow the pool to. OptEvent }
Config provides configuration for the pool.
type OptEvent ¶
type OptEvent struct {
Event func(ctx context.Context, event string, format string, a ...interface{})
}
OptEvent defines an handler used to provide events.
type Pool ¶
type Pool struct { Config Name string // Name of this pool. // contains filtered or unexported fields }
Pool provides a pool of routines that can execute any Worker tasks that are submitted.
func New ¶
New creates a new Pool.
Example ¶
ExampleNew provides a basic example for using a pool.
package main import ( "context" "fmt" "time" "github.com/ardanlabs/kit/pool" ) // theWork is the customer work type for using the pool. type theWork struct { privateID int } // Work implements the DoWorker interface. func (p *theWork) Work(ctx context.Context, id int) { fmt.Printf("Performing Work with privateID %d\n", p.privateID) } func main() { // Create a configuration. cfg := pool.Config{ MinRoutines: func() int { return 3 }, MaxRoutines: func() int { return 4 }, } // Create a new pool. p, err := pool.New("TheWork", cfg) if err != nil { fmt.Println(err) return } // Pass in some work to be performed. p.Do(context.TODO(), &theWork{}) p.Do(context.TODO(), &theWork{}) p.Do(context.TODO(), &theWork{}) // Wait to the work to be processed. time.Sleep(100 * time.Millisecond) // Shutdown the pool. p.Shutdown() }
Output:
type Stat ¶
type Stat struct { Routines int64 // Current number of routines. Pending int64 // Pending number of routines waiting to submit work. Active int64 // Active number of routines in the work pool. Executed int64 // Number of pieces of work executed. MaxRoutines int64 // High water mark of routines the pool has been at. }
Stat contains information about the pool.