-
Notifications
You must be signed in to change notification settings - Fork 7
/
pump.go
249 lines (193 loc) · 4.2 KB
/
pump.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package streams
import (
"sync"
"time"
)
// Pump represent a Message pump.
type Pump interface {
sync.Locker
// Accept takes a message to be processed in the Pump.
Accept(Message) error
// Stop stops the pump.
Stop()
// Close closes the pump.
Close() error
}
// syncPump is an synchronous Message Pump.
type syncPump struct {
sync.Mutex
name string
processor Processor
pipe TimedPipe
mon Monitor
}
// NewSyncPump creates a new synchronous Pump instance.
func NewSyncPump(mon Monitor, node Node, pipe TimedPipe) Pump {
p := &syncPump{
name: node.Name(),
processor: node.Processor(),
pipe: pipe,
mon: mon,
}
return p
}
// Accept takes a message to be processed in the Pump.
func (p *syncPump) Accept(msg Message) error {
p.pipe.Reset()
start := nanotime()
err := p.processor.Process(msg)
if err != nil {
return err
}
latency := time.Duration(nanotime()-start) - p.pipe.Duration()
p.mon.Processed(p.name, latency, -1)
return nil
}
// Stop stops the pump, but does not close it.
func (p *syncPump) Stop() {}
// Close closes the pump.
func (p *syncPump) Close() error {
return p.processor.Close()
}
// asyncPump is an asynchronous Message Pump.
type asyncPump struct {
sync.Mutex
name string
processor Processor
pipe TimedPipe
errFn ErrorFunc
mon Monitor
ch chan Message
wg sync.WaitGroup
}
// NewAsyncPump creates a new asynchronous Pump instance.
func NewAsyncPump(mon Monitor, node Node, pipe TimedPipe, errFn ErrorFunc) Pump {
p := &asyncPump{
name: node.Name(),
processor: node.Processor(),
pipe: pipe,
errFn: errFn,
mon: mon,
ch: make(chan Message, 1000),
}
p.wg.Add(1)
go p.run()
return p
}
func (p *asyncPump) run() {
defer p.wg.Done()
for msg := range p.ch {
p.pipe.Reset()
p.Lock()
start := nanotime()
err := p.processor.Process(msg)
if err != nil {
p.Unlock()
p.errFn(err)
return
}
latency := time.Duration(nanotime()-start) - p.pipe.Duration()
p.Unlock()
p.mon.Processed(p.name, latency, pressure(p.ch))
}
// It is not safe to do anything after the loop
}
// Accept takes a message to be processed in the Pump.
func (p *asyncPump) Accept(msg Message) error {
p.ch <- msg
return nil
}
// Stop stops the pump, but does not close it.
func (p *asyncPump) Stop() {
close(p.ch)
p.wg.Wait()
}
// Close closes the pump.
//
// Stop must be called before closing the pump.
func (p *asyncPump) Close() error {
return p.processor.Close()
}
// pressure calculates how full a channel is.
func pressure(ch chan Message) float64 {
l := float64(len(ch))
c := float64(cap(ch))
return l / c * 100
}
// SourcePump represents a Message pump for sources.
type SourcePump interface {
// Stop stops the source pump from running.
Stop()
// Close closed the source pump.
Close() error
}
// SourcePumps represents a set of source pumps.
type SourcePumps []SourcePump
// StopAll stops all source pumps.
func (p SourcePumps) StopAll() {
for _, sp := range p {
sp.Stop()
}
}
// sourcePump represents a Message pump for sources.
type sourcePump struct {
name string
source Source
pumps []Pump
errFn ErrorFunc
mon Monitor
quit chan struct{}
wg sync.WaitGroup
}
// NewSourcePump creates a new SourcePump.
func NewSourcePump(mon Monitor, name string, source Source, pumps []Pump, errFn ErrorFunc) SourcePump {
p := &sourcePump{
name: name,
source: source,
pumps: pumps,
errFn: errFn,
mon: mon,
quit: make(chan struct{}, 2),
}
p.wg.Add(1)
go p.run()
return p
}
func (p *sourcePump) run() {
defer p.wg.Done()
for {
select {
case <-p.quit:
return
default:
start := nanotime()
msg, err := p.source.Consume()
if err != nil {
go p.errFn(err)
return
}
if msg.Empty() {
continue
}
latency := time.Duration(nanotime() - start)
p.mon.Processed(p.name, latency, -1)
for _, pump := range p.pumps {
err = pump.Accept(msg)
if err != nil {
go p.errFn(err)
return
}
}
}
}
}
// Stop stops the source pump from running.
func (p *sourcePump) Stop() {
p.quit <- struct{}{}
p.wg.Wait()
}
// Close closes the source pump.
func (p *sourcePump) Close() error {
close(p.quit)
return p.source.Close()
}