-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
54 lines (43 loc) · 1.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"context"
"fmt"
"time"
"github.com/davidroman0O/retrypool"
)
// MyTask represents the data we'll process.
type MyTask struct {
Value int
}
// MyWorker implements the retrypool.Worker interface.
type MyWorker struct {
ID int // this is automatically set by the pool
}
// Run processes a task.
func (w *MyWorker) Run(ctx context.Context, task MyTask) error {
fmt.Printf("Worker %d processing task with value %d\n", w.ID, task.Value)
return nil
}
func main() {
ctx := context.Background()
// Create workers.
workers := []retrypool.Worker[MyTask]{}
for i := 0; i < 5; i++ {
workers = append(workers, &MyWorker{})
}
// Create the pool.
pool := retrypool.New(ctx, workers)
defer pool.Close()
// Submit tasks.
for i := 0; i < 10; i++ {
task := MyTask{Value: i}
err := pool.Submit(task)
if err != nil {
fmt.Printf("Error submitting task: %v\n", err)
}
}
pool.WaitWithCallback(ctx, func(queueSize, processingCount, deadTaskCount int) bool {
return queueSize > 0 || processingCount > 0
}, time.Second/4)
pool.Close()
}