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 interface{}, 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 ¶
The following is a sample application using the work pool.
// theWork is the customer work type for using the pool. type theWork struct{} // Work implements the DoWorker interface. func (*theWork) Work(ctx string, id int) { fmt.Printf("%s : Performing Work\n", ctx) } // ExampleNewDoPool provides a basic example for using a DoPool. func ExampleNewDoPool() { // Create a new do pool. p, err := pool.New(ctx, "TheWork", 3, func() time.Duration { return time.Minute }) if err != nil { fmt.Println(err) return } // Pass in some work to be performed. p.Do("TEST", &theWork{}) p.Do("TEST", &theWork{}) p.Do("TEST", &theWork{}) // Wait to the work to be processed. time.Sleep(1 * time.Second) // Shutdown the pool. p.Shutdown(ctx) }
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 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 ( "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(traceID string, 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("TEST", &theWork{}) p.Do("TEST", &theWork{}) p.Do("TEST", &theWork{}) // Wait to the work to be processed. time.Sleep(100 * time.Millisecond) // Shutdown the pool. p.Shutdown() }
Output:
func (*Pool) DoCancel ¶
DoCancel waits for the goroutine pool to take the work to be executed or gives up if the Context is cancelled. Only use when you want to throw away work and not push back.
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.