-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.go
78 lines (65 loc) · 1.6 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
package helper
import (
"context"
"github.com/Jeffail/tunny"
"github.com/google/uuid"
"gopkg.in/dfsr.v0/callstat"
"gopkg.in/dfsr.v0/dfsr"
"gopkg.in/dfsr.v0/versionvector"
)
type vectorWorkPool struct {
p *tunny.WorkPool
}
type vectorJob struct {
ctx context.Context
group uuid.UUID
tracker dfsr.Tracker
}
func newVectorWorkPool(numWorkers uint, r Reporter) (pool *vectorWorkPool, err error) {
if numWorkers == 0 {
return nil, ErrZeroWorkers
}
workers := make([]tunny.TunnyWorker, 0, numWorkers)
for i := uint(0); i < numWorkers; i++ {
workers = append(workers, &vectorWorker{r: r})
}
p, err := tunny.CreateCustomPool(workers).Open()
if err != nil {
return
}
return &vectorWorkPool{p: p}, nil
}
func (vwp *vectorWorkPool) Vector(ctx context.Context, group uuid.UUID, tracker dfsr.Tracker) (vector *versionvector.Vector, call callstat.Call, err error) {
v, err := vwp.p.SendWork(vectorJob{ctx: ctx, group: group, tracker: tracker})
if err != nil {
return
}
result, ok := v.(*vectorWorkResult)
if !ok {
panic("invalid work result")
}
return result.Vector, result.Call, result.Err
}
func (vwp *vectorWorkPool) Close() {
vwp.p.Close()
}
type vectorWorkResult struct {
Vector *versionvector.Vector
Call callstat.Call
Err error
}
type vectorWorker struct {
r Reporter
}
func (vw *vectorWorker) TunnyReady() bool {
return true
}
func (vw *vectorWorker) TunnyJob(data interface{}) interface{} {
job, ok := data.(vectorJob)
if ok {
var result vectorWorkResult
result.Vector, result.Call, result.Err = vw.r.Vector(job.ctx, job.group, job.tracker)
return &result
}
return nil
}