-
Notifications
You must be signed in to change notification settings - Fork 20
/
chain.go
179 lines (154 loc) · 3.21 KB
/
chain.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
package main
import (
"context"
"log"
"math/rand"
"sync"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
numWorkers := 10
start := time.Now()
chain := NewChain(ctx, generator(ctx, 100))
chain.
Throttle(5, true).
Pipe(func(in interface{}) interface{} {
v := in.(int)
// time.Sleep(time.Duration(rand.Intn(50)+50) * time.Millisecond)
time.Sleep(100 * time.Millisecond)
return v * 2
}).
// Pool(numWorkers, true, func(in interface{}) interface{} {
// v := in.(int)
// // Simulate slow worker
// time.Sleep(time.Duration(rand.Intn(150)+150) * time.Millisecond)
// return v - 50
// }).
Pipe(func(in interface{}) interface{} {
v := in.(int)
// Simulate slow output, which will throttle the input
time.Sleep(50 * time.Millisecond)
// time.Sleep(time.Duration(rand.Intn(150)+150) * time.Millisecond)
return v + 10
}).
Drain(func(in interface{}) {
log.Println(in)
})
log.Printf("time taken for %d worker %v\n", numWorkers, time.Since(start))
// Without throttling
// time taken for 1 worker 22.658940917s
// time taken for 5 worker 4.667852926s
// time taken for 10 worker 2.331470883s
}
type GenericFunc func(interface{}) interface{}
type Stream interface {
Pipe(GenericFunc) Stream
Throttle() Stream
Pool() Stream
Drain(func(interface{}))
}
type Chain struct {
Ctx context.Context
Queue chan interface{}
}
func (c *Chain) Pipe(fn GenericFunc) *Chain {
outStream := make(chan interface{})
go func() {
defer close(outStream)
for i := range c.Queue {
select {
case <-c.Ctx.Done():
return
case outStream <- fn(i):
}
}
}()
return &Chain{
Ctx: c.Ctx,
Queue: outStream,
}
}
func (c *Chain) Throttle(threshold int, debug bool) *Chain {
if threshold == 0 {
threshold = 10
}
outStream := make(chan interface{}, threshold)
go func() {
defer close(outStream)
for i := range c.Queue {
select {
case <-c.Ctx.Done():
return
case outStream <- i:
if debug {
log.Println("capacity:", len(outStream))
}
}
}
}()
return &Chain{
Ctx: c.Ctx,
Queue: outStream,
}
}
func (c *Chain) Pool(numWorkers int, debug bool, fn GenericFunc) *Chain {
if numWorkers == 0 {
numWorkers = 10
}
outStream := make(chan interface{})
var wg sync.WaitGroup
wg.Add(numWorkers)
worker := func(index int, in chan interface{}) {
defer wg.Done()
for i := range in {
select {
case <-c.Ctx.Done():
return
case outStream <- fn(i):
log.Println("worker", index)
}
}
}
for i := 0; i < numWorkers; i++ {
go worker(i, c.Queue)
}
go func() {
wg.Wait()
close(outStream)
}()
return &Chain{
Ctx: c.Ctx,
Queue: outStream,
}
}
func (c *Chain) Drain(fn func(interface{})) {
for i := range c.Queue {
fn(i)
}
}
func NewChain(ctx context.Context, in chan interface{}) *Chain {
return &Chain{
Ctx: ctx,
Queue: in,
}
}
func generator(ctx context.Context, limit int) chan interface{} {
outStream := make(chan interface{})
go func() {
defer close(outStream)
for i := 0; i < limit; i++ {
select {
case <-ctx.Done():
return
case outStream <- i:
}
}
}()
return outStream
}