-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
61 lines (53 loc) · 944 Bytes
/
process.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
package pipkin
import "context"
type Process struct {
ctx context.Context
units []*Unit
cancel func()
}
func (p *Process) Use(unit ...*Unit) {
max := uint8(0)
for _, u := range unit {
if u.id > max {
max = u.id
}
}
if len(p.units) < int(max+1) {
us := make([]*Unit, max+1)
copy(us, p.units)
p.units = us
}
for _, u := range unit {
if u.id == 0 {
continue
}
p.units[u.id] = u
}
}
func (p *Process) UseErrUnit(unit *Unit) {
if unit.id != 0 {
panic("invalid error handler unit")
}
if p.units == nil {
p.units = make([]*Unit, 1)
}
p.units[0] = unit
}
func (p *Process) Run() {
for _, u := range p.units {
u.p = p
u.ctx = p.ctx
for i := uint8(0); i < u.concurrent; i++ {
go u.runLoop()
}
}
}
func (p *Process) Cancel() {
p.cancel()
}
func NewProcess(ctx context.Context) *Process {
p := &Process{}
p.ctx, p.cancel = context.WithCancel(ctx)
p.UseErrUnit(DefaultErrUnit)
return p
}