Documentation
¶
Overview ¶
Package pooligo provides a basic worker-pool. This can be used to restrict the number of threads created by an application while providing multi-threaded capabilities.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CtxPool ¶
type CtxPool struct {
// contains filtered or unexported fields
}
CtxPool struct representing a standard worker-pool but with a parent context. If you do not need to explicitly pass in a context, use `Pool` (from `NewPool()`)
func NewCtxPool ¶
NewCtxPool creates a standard worker pool with a parent context
type Flexpool ¶
type Flexpool struct {
// contains filtered or unexported fields
}
Flexpool is an extension to the standard worker pool. It can be grown and shrunk after creation
func NewFlexPool ¶
NewFlexPool creates a standard worker pool
func (*Flexpool) Close ¶
func (p *Flexpool) Close()
Close will stop any workers doing any jobs added after calling Close. Any jobs being processed will be completed.
type Pooli ¶
type Pooli interface {
// Add will add a job to the worker pool. This will be process asychronously.
Add(job)
// Close will stop any new jobs being added and/or processed. Close will not
// force stop any currently running jobs, unless specific pool types allow
// and specify.
Close()
// Size returns the current size of the worker pool.
Size() int
// contains filtered or unexported methods
}
Pooli an interface to pool types. All pools returned by this module will be implement Pooli.
func NewPool ¶
NewPool creates a standard worker pool. This pool will run, processing jobs asynchronously until the application exits. If you need to stop the pool based on a context.Context being closed, use `NewCtxPool()`
Example ¶
package main
import (
"fmt"
"sync"
"time"
)
var (
exampleWG sync.WaitGroup
)
// delayprinter is the struct used to perform asynchonous jobs on.
// This will be used as an example of using a worker pool
type delayprinter struct {
id int
delay time.Duration
}
// Action is the function called by the workers from the pool
func (d *delayprinter) Action() {
time.Sleep(d.delay)
fmt.Printf("Performed action on ID: %d\n", d.id)
exampleWG.Done()
}
func main() {
// Create two delayprinters
d1 := &delayprinter{1, time.Second * 2}
d2 := &delayprinter{2, time.Second}
// Create a worker pool
pool := NewPool(10, 5)
exampleWG.Add(2)
// Add the jobs to the worker pool
pool.Add(d1)
pool.Add(d2)
// Wait for the goroutines to complete
exampleWG.Wait()
}
Output: Performed action on ID: 2 Performed action on ID: 1