forked from jacexh/ultron
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheventbus.go
154 lines (133 loc) · 3.49 KB
/
eventbus.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package ultron
import (
"context"
"runtime/debug"
"sync"
"sync/atomic"
"github.com/wosai/ultron/v2/pkg/statistics"
"go.uber.org/zap"
)
type (
// ReportHandleFunc 聚合报告处理函数
ReportHandleFunc func(context.Context, statistics.SummaryReport)
// reportBus 聚合报告事件总线
reportBus interface {
subscribeReport(ReportHandleFunc)
publishReport(statistics.SummaryReport)
}
// ResultHandleFunc 请求结果处理函数
ResultHandleFunc func(context.Context, statistics.AttackResult)
// resultBus 压测结果事件总线
resultBus interface {
subscribeResult(ResultHandleFunc)
publishResult(statistics.AttackResult)
}
eventbus struct {
cancel context.CancelFunc
reportBus chan statistics.SummaryReport
resultBuses []chan statistics.AttackResult
reportHandlers []ReportHandleFunc
resultHandlers []ResultHandleFunc
numberOfSubchannels uint32
counterForSubchannels uint32
closed uint32
once sync.Once
wg sync.WaitGroup
}
)
var (
defaultEventBus *eventbus
_ reportBus = (*eventbus)(nil)
_ resultBus = (*eventbus)(nil)
)
func newEventBus() *eventbus {
bus := &eventbus{
reportBus: make(chan statistics.SummaryReport, 3), // 低频通道
reportHandlers: make([]ReportHandleFunc, 0),
resultHandlers: make([]ResultHandleFunc, 0),
numberOfSubchannels: 30,
}
bus.resultBuses = make([]chan statistics.AttackResult, bus.numberOfSubchannels)
for i := 0; i < int(bus.numberOfSubchannels); i++ {
bus.resultBuses[i] = make(chan statistics.AttackResult, 200)
}
return bus
}
func (bus *eventbus) subscribeReport(fn ReportHandleFunc) {
if fn == nil {
return
}
bus.reportHandlers = append(bus.reportHandlers, fn)
}
func (bus *eventbus) publishReport(report statistics.SummaryReport) {
if atomic.LoadUint32(&bus.closed) == 0 {
bus.reportBus <- report
}
}
func (bus *eventbus) subscribeResult(fn ResultHandleFunc) {
if fn == nil {
return
}
bus.resultHandlers = append(bus.resultHandlers, fn)
}
func (bus *eventbus) publishResult(ret statistics.AttackResult) {
if atomic.LoadUint32(&bus.closed) == 0 {
v := atomic.AddUint32(&bus.counterForSubchannels, 1)
bus.resultBuses[int((v-1)%bus.numberOfSubchannels)] <- ret
}
}
func (bus *eventbus) start() {
bus.once.Do(func() {
ctx, cancel := context.WithCancel(context.Background())
bus.cancel = cancel
bus.wg.Add(1)
go func() {
defer func() {
if rec := recover(); rec != nil {
debug.PrintStack()
Logger.DPanic("report bus is quit", zap.Any("recover", rec))
}
bus.wg.Done()
bus.close()
}()
for report := range bus.reportBus {
for _, fn := range bus.reportHandlers {
fn(ctx, report)
}
}
}()
for _, sub := range bus.resultBuses {
bus.wg.Add(1)
go func(c <-chan statistics.AttackResult) {
defer func() {
if rec := recover(); rec != nil {
debug.PrintStack()
Logger.DPanic("one result bus is quit", zap.Any("recover", rec))
}
bus.wg.Done()
bus.close()
}()
for result := range c {
for _, handler := range bus.resultHandlers {
handler(ctx, result)
}
}
}(sub)
}
})
}
func (bus *eventbus) close() {
if atomic.CompareAndSwapUint32(&bus.closed, 0, 1) {
if bus.cancel != nil {
bus.cancel()
}
close(bus.reportBus)
for _, sub := range bus.resultBuses {
close(sub)
}
bus.wg.Wait()
}
}
func init() {
defaultEventBus = newEventBus()
}