forked from johnjohnsp1/capsule8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
753 lines (640 loc) · 15.2 KB
/
stream.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// Copyright 2017 Capsule8, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package stream implements lazy functional streams using Go channels.
//
// Streams consist of a single generator, zero or more operators, and a
// single terminator. Every stream shares a stop channel, which the owner
// may use to cleanly shut down the entire stream.
//
// Streams exploit Go's lightweight channels and goroutines in order to
// implement concurrency without locking shared data structures.
//
// This is intentionally reminiscent of the Java 8 Streams API
//
package stream
import (
"math"
"reflect"
"time"
"unicode/utf8"
api "github.com/capsule8/capsule8/api/v0"
"github.com/capsule8/capsule8/pkg/config"
)
// Stream represents a stream consisting of a generator, zero or more
// operators, and zero or one terminators. Consumers receive stream elements
// from the Data channel and may terminate the stream by closing the Ctrl
// channel.
type Stream struct {
// Ctrl is the control channel to the stream. Consumers may close
// it to shut down the stream.
Ctrl chan<- interface{}
// Data is the data channel of the stream. Consumers may receive
// from it to consume stream elements.
Data <-chan interface{}
}
// Close terminates the stream
func (s *Stream) Close() {
close(s.Ctrl)
}
// Next receives the next element in the stream. It returns that element
// and a boolean indicating whether it was received successfully or whether
// the stream was closed.
func (s *Stream) Next() (interface{}, bool) {
e, ok := <-s.Data
return e, ok
}
// ----------------------------------------------------------------------------
// Generators return an output stream
// ----------------------------------------------------------------------------
// Generators wait on the control channel for it to be closed. Since the entire
// stream shares the same control channel, any downstream consumer can close it
// and shut the entire stream down. The generator should be the only component
// of the stream waiting on it, so that it can cleanly close the stream from
// the source.
// Null creates a generator that doesn't create any output elements.
func Null() *Stream {
ctrl := make(chan interface{})
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
defer close(data)
for {
select {
case _, ok := <-ctrl:
if !ok {
return
}
}
}
}()
return &Stream{
Ctrl: ctrl,
Data: data,
}
}
// Chargen creates a generator that produces a stream of single-character
// strings from a pattern reminiscent of RFC864 chargen TCP/UDP services.
func Chargen() *Stream {
ctrl := make(chan interface{})
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
defer close(data)
firstChar := byte(' ')
lastChar := byte('~')
c := firstChar
for {
select {
case _, ok := <-ctrl:
if !ok {
return
}
default:
r, _ := utf8.DecodeRune([]byte{c})
data <- string(r)
c++
if c > lastChar {
c = firstChar
}
}
}
}()
return &Stream{
Ctrl: ctrl,
Data: data,
}
}
// Iota creates a generator that produces the given count of int elements
// (or infinite, if not specified). Additional optional arguments specify
// the start and step value.
func Iota(args ...uint64) *Stream {
ctrl := make(chan interface{})
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
defer close(data)
var (
count uint64 = math.MaxUint64
start uint64
step uint64 = 1
)
if len(args) > 0 {
count = args[0]
}
if len(args) > 1 {
start = args[1]
}
if len(args) > 2 {
step = args[2]
}
var value = start
for count > 0 {
select {
case _, ok := <-ctrl:
if !ok {
return
}
case data <- value:
}
count--
value += step
}
}()
return &Stream{
Ctrl: ctrl,
Data: data,
}
}
// Ticker creates a generator that produces a time.Time element every
// 'tick' of the specified duration.
func Ticker(d time.Duration) *Stream {
ctrl := make(chan interface{})
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
defer close(data)
ticker := time.NewTicker(d)
for {
select {
case _, ok := <-ctrl:
if !ok {
ticker.Stop()
return
}
case tick, ok := <-ticker.C:
if ok {
data <- tick
} else {
return
}
}
}
}()
return &Stream{
Ctrl: ctrl,
Data: data,
}
}
// ----------------------------------------------------------------------------
// Operators take an input stream and return an output stream
// ----------------------------------------------------------------------------
//
// Operators inherit their control and data channels from upstream. Unlike
// generators, they do not wait on the control channel. When their upstream
// data channel closes, the operator closes its downstream data channel also.
//
// DoFunc is the signature of a function that is called by Do
type DoFunc func(interface{})
// Do adds an operator in the stream that calls the given function for
// every element.
func Do(in *Stream, f DoFunc) *Stream {
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
defer close(data)
for {
select {
case e, ok := <-in.Data:
if ok {
f(e)
data <- e
} else {
return
}
}
}
}()
return &Stream{
Ctrl: in.Ctrl,
Data: data,
}
}
// MapFunc is the signature of a function that is called by Map
type MapFunc func(interface{}) interface{}
// Map adds an operator in the stream that calls the given function for
// every element and forwards along the returned value.
func Map(in *Stream, f MapFunc) *Stream {
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
defer close(data)
for {
select {
case e, ok := <-in.Data:
if ok {
data <- f(e)
} else {
return
}
}
}
}()
return &Stream{
Ctrl: in.Ctrl,
Data: data,
}
}
// FilterFunc is the signature of a function that is called by Filter
type FilterFunc func(interface{}) bool
// Filter adds a filter to a stream. For each element in the stream, a false
// return from the filter function causes the element to be discarded.
func Filter(in *Stream, f FilterFunc) *Stream {
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
defer close(data)
for {
select {
case e, ok := <-in.Data:
if ok {
if f(e) {
data <- e
}
} else {
return
}
}
}
}()
return &Stream{
Ctrl: in.Ctrl,
Data: data,
}
}
// Join combines multiple input Streams into a single output Stream. Closing
// the Join closes the input streams as well.
func Join(in ...*Stream) *Stream {
ctrl := make(chan interface{})
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
cases := make([]reflect.SelectCase, len(in)+1)
cases[0] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ctrl),
}
for i := range in {
// Data channels are odd
cases[1+i] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(in[i].Data),
}
}
activeInputs := len(in)
for activeInputs > 0 {
chosen, recv, recvOK := reflect.Select(cases)
if chosen > 0 && recvOK {
// Input element received
data <- recv.Interface()
} else if chosen == 0 && !recvOK {
// Control channel closed, relay upstream
for i := range in {
close(in[i].Ctrl)
}
cases[chosen].Chan = reflect.ValueOf(nil)
} else {
// Input stream closed
cases[chosen].Chan = reflect.ValueOf(nil)
activeInputs--
}
}
close(data)
}()
return &Stream{
Ctrl: ctrl,
Data: data,
}
}
// DemuxFunc takes an input element and returns an output index to send it to.
// It may return a negative index in order to signal that the Demux operator
// should drop the element instead of forwarding it to an output stream.
/*
type DemuxFunc func(interface{}) int
func Demux(done <-chan struct{}, in <-chan interface{}, n uint, f DemuxFunc) {
}
*/
// Split splits the input stream by the given filter function
func Split(in *Stream, f FilterFunc) (*Stream, *Stream) {
dataTrue := make(chan interface{}, config.Sensor.ChannelBufferLength)
dataFalse := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
defer close(dataTrue)
defer close(dataFalse)
for {
select {
case e, ok := <-in.Data:
if ok {
if f(e) {
dataTrue <- e
} else {
dataFalse <- e
}
} else {
return
}
}
}
}()
streamTrue := &Stream{
Ctrl: in.Ctrl,
Data: dataTrue,
}
streamFalse := &Stream{
Ctrl: in.Ctrl,
Data: dataFalse,
}
return streamTrue, streamFalse
}
// Tee copies the input stream to a pair of output streams (like a T-shaped
// junction)
func Tee(in *Stream) (*Stream, *Stream) {
dataOne := make(chan interface{}, config.Sensor.ChannelBufferLength)
dataTwo := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
defer close(dataOne)
defer close(dataTwo)
for {
select {
case e, ok := <-in.Data:
if ok {
dataOne <- e
dataTwo <- e
} else {
return
}
}
}
}()
streamOne := &Stream{
Ctrl: in.Ctrl,
Data: dataOne,
}
streamTwo := &Stream{
Ctrl: in.Ctrl,
Data: dataTwo,
}
return streamOne, streamTwo
}
// Copy duplicates elements from the input stream into the given number of
// output streams.
func Copy(in *Stream, n int) []*Stream {
data := make([]chan interface{}, n)
out := make([]*Stream, n)
for i := 0; i < n; i++ {
data[i] = make(chan interface{}, config.Sensor.ChannelBufferLength)
out[i] = &Stream{
Ctrl: in.Ctrl,
Data: data[i],
}
}
go func() {
for {
select {
case e, ok := <-in.Data:
if ok {
for _, d := range data {
d <- e
}
} else {
for _, d := range data {
close(d)
}
return
}
}
}
}()
return out
}
// Buffer stores up to the given number of elements from the input Stream
// before blocking
func Buffer(in *Stream, size int) *Stream {
data := make(chan interface{}, size)
go func() {
defer close(data)
for {
select {
case e, ok := <-in.Data:
if ok {
data <- e
} else {
return
}
}
}
}()
return &Stream{
Ctrl: in.Ctrl,
Data: data,
}
}
// Overflow drops elements instead of blocking on the output Stream
func Overflow(in *Stream) *Stream {
data := make(chan interface{})
go func() {
defer close(data)
for {
select {
case e, ok := <-in.Data:
if ok {
// Non-blocking send or drop
select {
case data <- e:
default:
}
} else {
return
}
}
}
}()
return &Stream{
Ctrl: in.Ctrl,
Data: data,
}
}
// OnOffValve adds a simple on/off valve operator onto the stream. It
// defaults to off to prevent a race condition from accepting input before
// being able to be switched off.
func OnOffValve(in *Stream) (*Stream, chan<- bool) {
ctrl := make(chan bool)
data := make(chan interface{})
go func() {
defer close(data)
//
// We default to off to prevent a race condition among our
// control channel and input stream in the case that closed is
// the desired initial state for the valve.
//
cases := []reflect.SelectCase{
{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ctrl),
},
{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(nil),
},
}
for {
chosen, recv, recvOK := reflect.Select(cases)
if chosen == 0 {
if recvOK {
enable := recv.Interface().(bool)
var v interface{}
if enable {
v = in.Data
} else {
v = nil
}
cases[1].Chan = reflect.ValueOf(v)
} else {
// Closing the control channel leaves
// the valve in its current state
// permanently.
cases[0].Chan = reflect.ValueOf(nil)
}
} else {
if recvOK {
data <- recv.Interface()
} else {
return
}
}
}
}()
s := &Stream{
Ctrl: in.Ctrl,
Data: data,
}
return s, ctrl
}
// Throttle limits the number of events emitted by the stream
func Throttle(in *Stream, mod api.ThrottleModifier) *Stream {
data := make(chan interface{})
// Convert `IntervalType` to a `Duration`
var interval time.Duration
switch mod.IntervalType {
case api.ThrottleModifier_MILLISECOND:
interval = time.Millisecond
case api.ThrottleModifier_SECOND:
interval = time.Second
case api.ThrottleModifier_MINUTE:
interval = time.Minute
case api.ThrottleModifier_HOUR:
interval = time.Hour
}
go func() {
defer close(data)
for {
select {
case e, ok := <-in.Data:
if ok {
data <- e
} else {
return
}
}
time.Sleep(time.Duration(mod.Interval) * interval)
}
}()
return &Stream{
Ctrl: in.Ctrl,
Data: data,
}
}
// Limit limits the number of results returned
func Limit(in *Stream, mod api.LimitModifier) *Stream {
data := make(chan interface{})
go func() {
defer close(data)
// Keep a count of how many results have been returned
var count int64
for {
// Only send a result up to the specified `Limit`
if count < mod.Limit {
select {
case e, ok := <-in.Data:
if ok {
data <- e
} else {
return
}
}
count++
} else {
break
}
}
}()
return &Stream{
Ctrl: in.Ctrl,
Data: data,
}
}
// ----------------------------------------------------------------------------
// Terminators accept an input stream and return a terminal value. They are
// typically used to aggregate a value over the entire stream.
// ----------------------------------------------------------------------------
// ReduceFunc is the signature of a function called by Reduce.
type ReduceFunc func(interface{}, interface{}) interface{}
// Reduce adds a terminator onto the stream that accumulates a value using
// the given function and then returns it once the input stream terminates.
func Reduce(in *Stream, initVal interface{}, f ReduceFunc) chan interface{} {
data := make(chan interface{}, config.Sensor.ChannelBufferLength)
go func() {
acc := initVal
for {
select {
case e, ok := <-in.Data:
if ok {
acc = f(acc, e)
} else {
data <- acc
close(data)
return
}
}
}
}()
return data
}
// ForEach adds a terminator onto the stream that consumes each element and
// runs the given function on them.
func ForEach(in *Stream, f DoFunc) chan interface{} {
done := make(chan interface{})
go func() {
for {
select {
case e, ok := <-in.Data:
if ok {
f(e)
} else {
close(done)
return
}
}
}
}()
return done
}
// Wait adds a terminator onto the stream that throws away all elements and
// just waits for stream termination.
func Wait(in *Stream) chan interface{} {
done := make(chan interface{})
go func() {
for {
select {
case _, ok := <-in.Data:
if !ok {
close(done)
return
}
}
}
}()
return done
}