Overview
Workers
Workers is a lightweight Go library for executing multiple tasks concurrently, with support for either a dynamic or fixed-limit number of workers.
Basic Usage Example
Code
package main
import (
"context"
"fmt"
"log"
"sync"
"github.com/ygrebnov/workers"
)
// fibonacci calculates Fibonacci numbers.
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
}
func main() {
// Create a new controller with a dynamic number of workers.
// Type parameter is used to specify the type of task result.
w := workers.New[string](context.Background(), &workers.Config{StartImmediately: true})
wg := sync.WaitGroup{}
// Receive and print results or handle errors in a separate goroutine.
go func() {
for range 10 {
select {
case result := <-w.GetResults():
fmt.Println(result)
case err := <-w.GetErrors():
fmt.Println("error executing task:", err)
}
wg.Done()
}
}()
for i := 20; i >= 11; i-- {
wg.Add(1)
// Add ten tasks calculating the Fibonacci number for a given index.
// A task is a function that takes a context and returns a string.
err := w.AddTask(func(ctx context.Context) string {
return fmt.Sprintf("Calculated Fibonacci for: %d, result: %d.", i, fibonacci(i))
})
if err != nil {
log.Fatalf("failed to add task: %v", err)
}
}
wg.Wait() // Wait for all tasks to finish.
// Close channels.
close(w.GetResults())
close(w.GetErrors())
}
In this example, Fibonacci numbers are calculated concurrently for integers from 20 down to 11. The fibonacci() function implements a basic recursive algorithm.
Explanation
-
A workers controller is created with
workers.New()
.- It is configured via
workers.Config
to start executing tasks immediately upon being added (see Configuration for details on configuration options). - The generic type parameter (
string
) specifies the type of the task’s result.
- It is configured via
-
Tasks are submitted through the
AddTask()
method, which accepts a function matching the signaturefunc(context.Context) string
. (See Tasks for more details on supported function signatures.) -
Task results and errors are delivered via channels:
GetResults()
returns the results channel.GetErrors()
returns the errors channel.- Results and errors are processed using a
select
loop. - After processing, both channels are explicitly closed (see Results for more details on task execution results).
Example Output
Calculated Fibonacci for: 20, result: 6765.
Calculated Fibonacci for: 18, result: 2584.
Calculated Fibonacci for: 19, result: 4181.
Calculated Fibonacci for: 17, result: 1597.
Calculated Fibonacci for: 16, result: 987.
Calculated Fibonacci for: 15, result: 610.
Calculated Fibonacci for: 14, result: 377.
Calculated Fibonacci for: 12, result: 144.
Calculated Fibonacci for: 13, result: 233.
Calculated Fibonacci for: 11, result: 89.
Additional Resources
- See more Examples for advanced usage patterns.
- Project repository: github.com/ygrebnov/workers.