-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.go
61 lines (54 loc) · 1.07 KB
/
line.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 factory
import (
"math"
"sync"
)
// Line 工作流水线
type Line struct {
master *Master
action func(interface{})
waitGroup *sync.WaitGroup
}
func (l *Line) Wait() {
l.waitGroup.Wait()
}
func (l *Line) Submit(args interface{}) {
if l.master.getWorker().assign(l, args) {
return
}
tryNum := int(float64(l.master.Running()) * 0.5)
for n := 0; n < tryNum; n++ {
if l.master.getWorker().assign(l, args) {
return
}
}
addSize := math.Max(2, float64(l.master.Running())*0.25)
l.master.AdjustSize(l.master.Running() + int(addSize))
l.Submit(args)
}
func (l *Line) Execute(args interface{}) {
l.waitGroup.Add(1)
l.action(args)
}
func (l *Line) SetPanicHandler(handler func(interface{})) {
oldAction := l.action
l.action = func(i interface{}) {
defer func() {
if p := recover(); p != nil {
handler(p)
}
}()
oldAction(i)
}
}
func NewLine(master *Master, action func(interface{})) *Line {
wg := new(sync.WaitGroup)
return &Line{
master: master,
action: func(i interface{}) {
defer wg.Done()
action(i)
},
waitGroup: wg,
}
}