-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.go
92 lines (74 loc) · 1.52 KB
/
worker.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package flyjapan
import (
"context"
"sync"
)
type Worker interface {
Work(ctx context.Context) (<-chan *Result, <-chan error)
}
type worker struct {
workerOptions
queries []*Query
jobs chan *job
}
func NewWorker(queries []*Query, setters ...WorkerOption) Worker {
worker := &worker{
workerOptions: defaultWorkerOption,
queries: queries,
jobs: make(chan *job),
}
for _, setter := range setters {
setter(&worker.workerOptions)
}
for i := 0; i < worker.MaxWorkers; i++ {
go worker.worker()
}
return worker
}
func (w *worker) Work(ctx context.Context) (<-chan *Result, <-chan error) {
resCh, errCh := make(chan *Result), make(chan error)
var wg sync.WaitGroup
wg.Add(len(w.queries))
go func() {
wg.Wait()
close(resCh)
close(errCh)
}()
for _, query := range w.queries {
go w.addJob(newJob(ctx, &wg, query, resCh, errCh))
}
return resCh, errCh
}
func (w *worker) addJob(job *job) {
w.jobs <- job
}
func (w *worker) worker() {
for job := range w.jobs {
job.do()
}
}
type job struct {
ctx context.Context
wg *sync.WaitGroup
query *Query
resultCh chan *Result
errorCh chan error
}
func newJob(ctx context.Context, wg *sync.WaitGroup, query *Query, resultCh chan *Result, errorCh chan error) *job {
return &job{
ctx: ctx,
wg: wg,
query: query,
resultCh: resultCh,
errorCh: errorCh,
}
}
func (job *job) do() {
defer job.wg.Done()
res, err := job.query.query(job.ctx)
if err != nil {
job.errorCh <- err
return
}
job.resultCh <- res
}