/
stream.go
174 lines (155 loc) · 3.51 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
// Package stream provides stuff
package stream
import (
"context"
"golang.org/x/sync/errgroup"
)
// ConsumerFunc function type to receive messages.
type ConsumerFunc = func(v interface{}) error
type Consumer interface {
Consume(ConsumerFunc) error
}
type Sender interface {
Send(v interface{}) error
}
// Proc is the interface used by ProcFuncs to Consume and send data to the next
// func.
type Proc interface {
Consumer
Sender
Context() context.Context
}
// proc implements the Proc interface
type proc struct {
ctx context.Context
Consumer
Sender
}
// MakeProc makes a Proc based on a Consumer and Sender.
func MakeProc(ctx context.Context, c Consumer, s Sender) Proc {
return proc{ctx, c, s}
}
func (p proc) Consume(fn func(v interface{}) error) error {
if p.Consumer == nil {
return nil
}
return p.Consumer.Consume(fn)
}
func (p proc) Send(v interface{}) error {
if p.Sender == nil {
return nil
}
return p.Sender.Send(v)
}
func (p proc) Context() context.Context {
return p.ctx
}
type ProcFunc = func(Proc) error
// Line will consume and pass a message sequentually on all ProcFuncs.
func Line(pfns ...ProcFunc) ProcFunc {
if len(pfns) == 0 {
panic("no funcs")
}
if len(pfns) == 1 {
return pfns[0]
}
return func(p Proc) error {
ctx := p.Context()
if ctx == nil {
ctx = context.Background()
}
eg, ctx := errgroup.WithContext(ctx)
last := p // consumer should be nil
for i, fn := range pfns {
l, fn := last, fn // shadow
if i == len(pfns)-1 {
// Last one will consume last to P
eg.Go(func() error {
return fn(proc{ctx, l, p})
})
break
}
ch := NewChan(ctx, 0)
// Consuming from last and sending to channel
np := proc{ctx, l, ch}
eg.Go(func() error {
defer ch.Close()
return fn(np)
})
last = proc{ctx, ch, nil} // don't need a sender here
}
return eg.Wait()
}
}
// Broadcast consumes and passes the consumed message to all pfs ProcFuncs.
func Broadcast(pfns ...ProcFunc) ProcFunc {
return func(p Proc) error {
eg, ctx := errgroup.WithContext(p.Context())
chs := make([]Chan, len(pfns))
for i, fn := range pfns {
ch := NewChan(ctx, 0)
fn := fn
eg.Go(func() error {
return fn(proc{ctx, ch, p})
})
chs[i] = ch
}
eg.Go(func() error {
defer func() {
for _, ch := range chs {
ch.Close()
}
}()
return p.Consume(func(v interface{}) error {
for _, ch := range chs {
if err := ch.Send(v); err != nil {
return err
}
}
return nil
})
})
return eg.Wait()
}
}
// Workers will start N ProcFuncs consuming and sending on same channels.
func Workers(n int, pfns ...ProcFunc) ProcFunc {
pfn := Line(pfns...)
if n <= 0 {
n = 1
}
return func(p Proc) error {
eg, ctx := errgroup.WithContext(p.Context())
for i := 0; i < n; i++ {
eg.Go(func() error {
return pfn(proc{ctx, p, p})
})
}
return eg.Wait()
}
}
// Buffer will create an extra buffered channel.
func Buffer(n int, pfns ...ProcFunc) ProcFunc {
pfn := Line(pfns...)
return func(p Proc) error {
eg, ctx := errgroup.WithContext(p.Context())
ch := NewChan(ctx, n)
eg.Go(func() error {
defer ch.Close()
return p.Consume(ch.Send)
})
eg.Go(func() error {
return pfn(proc{ctx, ch, p})
})
return eg.Wait()
}
}
// Run will run the stream.
func Run(pfns ...ProcFunc) error {
return RunWithContext(context.Background(), pfns...)
}
// RunWithContext runs the stream with a context.
func RunWithContext(ctx context.Context, pfns ...ProcFunc) error {
pfn := Line(pfns...)
return pfn(proc{ctx, nil, nil})
}