-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
73 lines (60 loc) · 1.23 KB
/
pool.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package workerx
import (
"sync"
)
// WorkerPool is a limited worker pool.
type WorkerPool[T any] struct {
wg *sync.WaitGroup
limit uint
size uint
tasks chan T
before, after func(T)
process func(T) error
handleErr func(T, error)
}
// NewWorkerPool creates a new worker pool.
func NewWorkerPool[T any](
limit uint,
process func(T) error,
opts ...Option[T],
) *WorkerPool[T] {
worker := &WorkerPool[T]{
wg: &sync.WaitGroup{},
limit: limit,
size: 0,
tasks: make(chan T, limit),
before: func(_ T) {},
process: process,
after: func(_ T) {},
handleErr: func(_ T, _ error) {},
}
for _, opt := range opts {
opt(worker)
}
return worker
}
// Add adds task and starts one more worker process if necessary.
func (w *WorkerPool[T]) Add(task T) {
if w.size < w.limit {
w.wg.Add(1)
w.size++
go w.run()
}
w.tasks <- task
}
// Close closes worker and waits for all processed to be done.
func (w *WorkerPool[T]) Close() {
close(w.tasks)
w.wg.Wait()
}
func (w *WorkerPool[T]) run() {
defer w.wg.Done()
for task := range w.tasks {
w.before(task)
err := w.process(task)
if err != nil {
w.handleErr(task, err)
}
w.after(task)
}
}