-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
77 lines (64 loc) · 1.97 KB
/
worker.go
File metadata and controls
77 lines (64 loc) · 1.97 KB
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
package parallel
import (
"fmt"
"github.com/0xSplits/workit/handler"
"github.com/0xSplits/workit/registry"
"github.com/xh3b4sd/logger"
"github.com/xh3b4sd/tracer"
)
type Config struct {
// Han is the list of worker handlers implementing the actual business logic
// as distinct execution pipelines. The worker handlers configured here may be
// wrapped in administrative handler implementations to e.g. instrument
// handler execution latency and handler error rates. All worker handlers
// provided here will be executed concurrently within their own isolated
// failure domain.
Han []handler.Cooler
// Log is a standard logger interface to forward structured log messages to
// any output interface e.g. stdout.
Log logger.Interface
// Reg is the metrics interface used to wrap the internally managed handlers
// for instrumentation purposes. The metrics handlers created by this registry
// will record all worker handler execution metrics.
Reg *registry.Registry
}
type Worker struct {
han []handler.Interface
log logger.Interface
reg *registry.Registry
rdy chan struct{}
}
func New(c Config) *Worker {
if len(c.Han) == 0 {
tracer.Panic(tracer.Mask(fmt.Errorf("%T.Han must not be empty", c)))
}
if c.Log == nil {
tracer.Panic(tracer.Mask(fmt.Errorf("%T.Log must not be empty", c)))
}
if c.Reg == nil {
tracer.Panic(tracer.Mask(fmt.Errorf("%T.Reg must not be empty", c)))
}
// Verify early on that no handler leaf is ever nil.
for i, x := range c.Han {
if x == nil {
tracer.Panic(tracer.Mask(fmt.Errorf("%T.Han[%d] must not be empty", c, i)))
}
}
// Wrap the list of injected worker handlers into their own metrics handler,
// so that we can instrument the runtime latency and error rates of every
// single worker handler provided.
var han []handler.Interface
for _, x := range c.Han {
han = append(han, c.Reg.New(x))
}
var rdy chan struct{}
{
rdy = make(chan struct{})
}
return &Worker{
han: han,
log: c.Log,
reg: c.Reg,
rdy: rdy,
}
}