/
blocks.go
392 lines (337 loc) · 7.63 KB
/
blocks.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package blocks
import (
"fmt"
"github.com/nytlabs/streamtools/st/loghub"
"time"
)
type Msg struct {
Msg interface{}
Route string
}
type AddChanMsg struct {
Route string
Channel chan *Msg
}
type QueryMsg struct {
Route string
RespChan chan interface{}
}
type BlockChans struct {
InChan chan *Msg
QueryChan chan *QueryMsg
AddChan chan *AddChanMsg
DelChan chan *Msg
ErrChan chan error
QuitChan chan bool
}
type LogStreams struct {
log chan interface{}
ui chan interface{}
}
type Block struct {
Id string // the name of the block specifed by the user (like MyBlock)
Kind string // the kind of block this is (like count, toFile, fromSQS)
Desc string // the description of block ('counts the number of messages it has seen')
inRoutes map[string]chan interface{}
queryRoutes map[string]chan chan interface{}
broadcast chan interface{}
quit chan interface{}
doesBroadcast bool
BlockChans
LogStreams
}
type BlockDef struct {
Type string
Desc string
InRoutes []string
QueryRoutes []string
OutRoutes []string
}
type BlockInterface interface {
Setup()
Run()
CleanUp()
Build(BlockChans)
Quit() chan interface{}
Broadcast() chan interface{}
InRoute(string) chan interface{}
QueryRoute(string) chan chan interface{}
GetBlock() *Block
GetDef() *BlockDef
Log(interface{})
Error(interface{})
SetId(string)
}
func (b *Block) Build(c BlockChans) {
// fuck can I do this all in one?
b.InChan = c.InChan
b.QueryChan = c.QueryChan
b.AddChan = c.AddChan
b.DelChan = c.DelChan
b.ErrChan = c.ErrChan
b.QuitChan = c.QuitChan
// route maps
b.inRoutes = make(map[string]chan interface{}) // necessary to stop locking...
b.queryRoutes = make(map[string]chan chan interface{})
// broadcast channel
b.broadcast = make(chan interface{}, 10) // necessary to stop locking...
// quit chan
b.quit = make(chan interface{})
b.ui = make(chan interface{})
b.log = make(chan interface{})
}
func (b *Block) SetId(Id string) {
b.Id = Id
}
func (b *Block) InRoute(routeName string) chan interface{} {
route := make(chan interface{}, 1000)
b.inRoutes[routeName] = route
return route
}
func (b *Block) QueryRoute(routeName string) chan chan interface{} {
route := make(chan chan interface{}, 1000)
b.queryRoutes[routeName] = route
return route
}
func (b *Block) Broadcast() chan interface{} {
b.doesBroadcast = true
return b.broadcast
}
func (b *Block) Quit() chan interface{} {
return b.quit
}
func (b *Block) GetBlock() *Block {
return b
}
func (b *Block) GetDef() *BlockDef {
inRoutes := []string{}
queryRoutes := []string{}
outRoutes := []string{}
for k, _ := range b.inRoutes {
inRoutes = append(inRoutes, k)
}
for k, _ := range b.queryRoutes {
queryRoutes = append(queryRoutes, k)
}
if b.doesBroadcast {
outRoutes = []string{"out"}
}
return &BlockDef{
Type: b.Kind,
Desc: b.Desc,
InRoutes: inRoutes,
QueryRoutes: queryRoutes,
OutRoutes: outRoutes,
}
}
func (b *Block) CleanUp() {
for route := range b.inRoutes {
defer close(b.inRoutes[route])
}
for route := range b.queryRoutes {
defer close(b.queryRoutes[route])
}
defer close(b.InChan)
defer close(b.QueryChan)
defer close(b.AddChan)
defer close(b.DelChan)
defer close(b.ErrChan)
defer close(b.QuitChan)
defer close(b.broadcast)
go func(id string) {
loghub.Log <- &loghub.LogMsg{
Type: loghub.INFO,
Data: fmt.Sprintf("Block %s Quitting...", b.Id),
Id: id,
}
}(b.Id)
}
func (b *Block) Error(msg interface{}) {
go func(id string) {
loghub.Log <- &loghub.LogMsg{
Type: loghub.ERROR,
Data: msg,
Id: id,
}
}(b.Id)
}
func (b *Block) Log(msg interface{}) {
go func(id string) {
loghub.Log <- &loghub.LogMsg{
Type: loghub.INFO,
Data: msg,
Id: id,
}
}(b.Id)
}
func BlockRoutine(bi BlockInterface) {
var dropped int64
dropTicker := time.NewTicker(time.Duration(1 * time.Second))
dropTicker.Stop()
outChans := make(map[string]chan *Msg)
b := bi.GetBlock()
bi.Setup()
go bi.Run()
for {
select {
case <-dropTicker.C:
go func(id string, count int64) {
loghub.Log <- &loghub.LogMsg{
Type: loghub.ERROR,
Data: fmt.Sprintf("Dropped messages: %d (cannot keep up with stream)", count),
Id: id,
}
}(b.Id, dropped)
if dropped == 0 {
dropTicker.Stop()
}
dropped = 0
case msg := <-b.InChan:
_, ok := b.inRoutes[msg.Route]
if !ok {
break
}
// every in channel is buffered a 1000 messages.
// if we cannot immediately send to that in channel we place the msg
// in a go routine and notify the user that the block routine's
// buffer has overflowed. this still allows for unrecoverable
// overflows (for example: a stuck run() function), but at least it
// offloads to memory/cpu instead of blocking.
select {
case b.inRoutes[msg.Route] <- msg.Msg:
default:
if dropped == 0 {
dropTicker.Stop()
dropTicker = time.NewTicker(1 * time.Second)
}
dropped++
}
if msg.Route == "rule" {
go func(id string) {
loghub.UI <- &loghub.LogMsg{
Type: loghub.RULE_UPDATED,
Data: map[string]interface{}{},
Id: id,
}
}(b.Id)
}
case msg := <-b.QueryChan:
if msg.Route == "ping" {
msg.RespChan <- "OK"
continue
}
_, ok := b.queryRoutes[msg.Route]
if !ok {
break
}
select {
case b.queryRoutes[msg.Route] <- msg.RespChan:
default:
go func() {
b.queryRoutes[msg.Route] <- msg.RespChan
}()
}
case msg := <-b.AddChan:
outChans[msg.Route] = msg.Channel
case msg := <-b.DelChan:
delete(outChans, msg.Route)
case msg := <-b.broadcast:
for _, v := range outChans {
v <- &Msg{
Msg: msg,
Route: "",
}
}
case <-b.QuitChan:
b.quit <- true
b.CleanUp()
return
}
}
}
type Connection struct {
Id string
ToRoute string
BlockChans
LogStreams
}
func (c *Connection) SetId(Id string) {
c.Id = Id
}
func (c *Connection) Build(chans BlockChans) {
c.InChan = chans.InChan
c.QueryChan = chans.QueryChan
c.AddChan = chans.AddChan
c.DelChan = chans.DelChan
c.QuitChan = chans.QuitChan
}
func (c *Connection) CleanUp() {
defer close(c.InChan)
defer close(c.QueryChan)
defer close(c.AddChan)
defer close(c.DelChan)
defer close(c.QuitChan)
loghub.Log <- &loghub.LogMsg{
Type: loghub.INFO,
Data: fmt.Sprintf("Connection %s Quitting...", c.Id),
Id: c.Id,
}
}
func ConnectionRoutine(c *Connection) {
var last interface{}
var rate float64
outChans := make(map[string]chan *Msg)
times := make([]int64, 100, 100)
timesIdx := len(times)
rateReport := time.NewTicker(200 * time.Millisecond)
for {
select {
case <-rateReport.C:
if timesIdx == len(times) {
rate = 0
} else {
rate = 1000000000.0 * float64(len(times)-timesIdx) / float64(time.Now().UnixNano()-times[timesIdx])
}
go func(id string, r float64) {
loghub.UI <- &loghub.LogMsg{
Type: loghub.UPDATE_RATE,
Data: map[string]interface{}{
"Rate": r,
},
Id: id,
}
}(c.Id, rate)
case msg := <-c.InChan:
last = msg.Msg
for _, v := range outChans {
v <- &Msg{
Msg: msg.Msg,
Route: c.ToRoute,
}
}
times = times[1:]
times = append(times, time.Now().UnixNano())
if timesIdx > 0 {
timesIdx--
}
case msg := <-c.QueryChan:
switch msg.Route {
case "rate":
msg.RespChan <- map[string]interface{}{
"Rate": rate,
}
case "last":
msg.RespChan <- map[string]interface{}{
"Last": last,
}
}
case msg := <-c.AddChan:
outChans[msg.Route] = msg.Channel
case msg := <-c.DelChan:
delete(outChans, msg.Route)
case <-c.QuitChan:
c.CleanUp()
return
}
}
}