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 Pooli ¶
type Pooli interface {
Add(job)
Close()
SetClosed()
}
Pooli an interface to pool types. All pools returned by this module will be implement Pooli.
func NewPool ¶
NewPool creates a standard worker pool
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
Click to show internal directories.
Click to hide internal directories.