2021-09-25 14:39:16 +02:00
|
|
|
package workgroups_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-29 11:42:17 +02:00
|
|
|
"errors"
|
2021-09-25 14:39:16 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"runtime"
|
2022-03-29 11:42:17 +02:00
|
|
|
"time"
|
2021-09-25 14:39:16 +02:00
|
|
|
|
|
|
|
"go.xsfx.dev/workgroups"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Example() {
|
|
|
|
d, ctx := workgroups.NewDispatcher(
|
|
|
|
context.Background(),
|
|
|
|
runtime.GOMAXPROCS(0), // This starts as much worker as maximal processes are allowed for go.
|
2021-10-07 13:55:22 +02:00
|
|
|
10, // Capacity of the queue.
|
2021-09-25 14:39:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
work := func(ctx context.Context) error {
|
|
|
|
// Check if context already expired.
|
|
|
|
// Return if its the case, else just go forward.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return fmt.Errorf("got error from context: %w", ctx.Err())
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some wannebe work... printing something.
|
|
|
|
fmt.Print("hello world from work")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starting up the workers.
|
2021-09-27 10:44:58 +02:00
|
|
|
d.Start()
|
2021-09-25 14:39:16 +02:00
|
|
|
|
|
|
|
// Feeding the workers some work.
|
2021-09-27 10:44:58 +02:00
|
|
|
d.Append(workgroups.NewJob(ctx, work))
|
2021-09-25 14:39:16 +02:00
|
|
|
|
|
|
|
// Closing the channel for work.
|
|
|
|
d.Close()
|
|
|
|
|
|
|
|
// Waiting to finnish everything.
|
|
|
|
if err := d.Wait(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// hello world from work
|
|
|
|
}
|
2022-03-29 11:42:17 +02:00
|
|
|
|
|
|
|
func ExampleRetry() {
|
|
|
|
d, ctx := workgroups.NewDispatcher(
|
|
|
|
context.Background(),
|
|
|
|
runtime.GOMAXPROCS(0), // This starts as much worker as maximal processes are allowed for go.
|
|
|
|
10, // Capacity of the queue.
|
|
|
|
)
|
|
|
|
|
|
|
|
// Just returning some error. So it can retry.
|
|
|
|
failFunc := func() error {
|
|
|
|
fmt.Print("fail ")
|
|
|
|
|
|
|
|
return errors.New("fail") //nolint:goerr113
|
|
|
|
}
|
|
|
|
|
|
|
|
work := func(ctx context.Context) error {
|
|
|
|
// Check if context already expired.
|
|
|
|
// Return if its the case, else just go forward.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return fmt.Errorf("got error from context: %w", ctx.Err())
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := failFunc(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starting up the workers.
|
|
|
|
d.Start()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Feeding the workers some work.
|
|
|
|
d.Append(
|
|
|
|
workgroups.NewJob(
|
|
|
|
ctx,
|
|
|
|
workgroups.Retry(ctx, time.Second/2)(work), // This will retry after a half second.
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Closing the channel for work.
|
|
|
|
d.Close()
|
|
|
|
|
|
|
|
// Waiting to finnish everything.
|
|
|
|
if err := d.Wait(); err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// fail fail error on waiting: got error from context: context deadline exceeded
|
|
|
|
}
|