This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfan-in.go
632 lines (584 loc) · 17 KB
/
fan-in.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
/*
Copyright IBM Corporation All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Package fan is a type-flexible fan-in pattern implementation
This package provides convenient and efficient mechanisms for combining many channels
with the same element type into one channel with that element type.
For any primitive type in Go, this package provides helper methods to "fan-in" many
channels with that primitive as the element type. For instance, if you have several
channels of integers:
var a, b, c chan int // assume these are created elsewhere and are in use
// We can close this done channel to stop the fan-in operation before all of the
// input channels close
done := make(chan struct{})
// all values sent on a, b, and c will be readable from combined, which will only
// close when either all of a, b, and c close OR done closes
combined := fan.Ints().FanIn(done, a, b, c).(<-chan int)
For non-primitive types, you can achieve good performance by providing an anonymous function
that type-asserts the channels to the appropriate element type (avoiding reflection on the
hot path):
type MyCustomType struct {
Foo, Bar int
Baz string
}
var a, b, c chan MyCustomType // assume these are created elsewhere and are in use
// We can close this done channel to stop the fan-in operation before all of the
// input channels close
done := make(chan struct{})
// all values sent on a, b, and c will be readable from combined, which will only
// close when either all of a, b, and c close OR done closes
combined := fan.Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan MyCustomType):
if !more {
return true
}
out.(chan MyCustomType) <- element
}
return false
}
}.FanIn(done, a, b, c).(<-chan MyCustomType)
This small bit of boilerplate captures the necessary type information to avoid performing
any reflection while passing data read from the channels, resulting in the same throughput
as a custom implementation for your type.
All SelectFunc implementations look essentially the same, with the only difference being
the element type of the channels in the two type assertions.
If your use-case is not performance-critical, we also provide a reflection-based fallback
implementation which is used when no SelectFunc is provided. See [benchmarks](#benchmarks)
to understand the performance effect of this implementation.
To use the inefficient reflection-based approach on a custom type, you can do:
type MyCustomType struct {
Foo, Bar int
Baz string
}
var a, b, c chan MyCustomType // assume these are created elsewhere and are in use
// We can close this done channel to stop the fan-in operation before all of the
// input channels close
done := make(chan struct{})
// all values sent on a, b, and c will be readable from combined, which will only
// close when either all of a, b, and c close OR done closes
combined := fan.Config{}.FanIn(done, a, b, c).(<-chan MyCustomType)
*/
package fan
import (
"fmt"
"reflect"
"sync"
)
// Interfaces returns a config intended to fan-in channels with the empty interface
// as their element type.
func Interfaces() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan interface{}):
if !more {
return true
}
out.(chan interface{}) <- element
}
return false
},
}
}
// Strings returns a config intended to fan-in channels with string
// as their element type.
func Strings() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan string):
if !more {
return true
}
out.(chan string) <- element
}
return false
},
}
}
// ByteSlices returns a config intended to fan-in channels with byte slice
// as their element type.
func ByteSlices() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan []byte):
if !more {
return true
}
out.(chan []byte) <- element
}
return false
},
}
}
// Uintptrs returns a config intended to fan-in channels with uintptr
// as their element type.
func Uintptrs() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan uintptr):
if !more {
return true
}
out.(chan uintptr) <- element
}
return false
},
}
}
// Bools returns a config intended to fan-in channels with bool
// as their element type.
func Bools() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan bool):
if !more {
return true
}
out.(chan bool) <- element
}
return false
},
}
}
// Bytes returns a config intended to fan-in channels with byte
// as their element type.
func Bytes() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan byte):
if !more {
return true
}
out.(chan byte) <- element
}
return false
},
}
}
// Runes returns a config intended to fan-in channels with rune
// as their element type.
func Runes() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan rune):
if !more {
return true
}
out.(chan rune) <- element
}
return false
},
}
}
// Complex64s returns a config intended to fan-in channels with complex64
// as their element type.
func Complex64s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan complex64):
if !more {
return true
}
out.(chan complex64) <- element
}
return false
},
}
}
// Complex128s returns a config intended to fan-in channels with complex128
// as their element type.
func Complex128s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan complex128):
if !more {
return true
}
out.(chan complex128) <- element
}
return false
},
}
}
// Float32s returns a config intended to fan-in channels with float32
// as their element type.
func Float32s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan float32):
if !more {
return true
}
out.(chan float32) <- element
}
return false
},
}
}
// Float64s returns a config intended to fan-in channels with float64
// as their element type.
func Float64s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan float64):
if !more {
return true
}
out.(chan float64) <- element
}
return false
},
}
}
// Ints returns a config intended to fan-in channels with int
// as their element type.
func Ints() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan int):
if !more {
return true
}
out.(chan int) <- element
}
return false
},
}
}
// Uints returns a config intended to fan-in channels with uint
// as their element type.
func Uints() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan uint):
if !more {
return true
}
out.(chan uint) <- element
}
return false
},
}
}
// Int8s returns a config intended to fan-in channels with int8
// as their element type.
func Int8s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan int8):
if !more {
return true
}
out.(chan int8) <- element
}
return false
},
}
}
// Uint8s returns a config intended to fan-in channels with uint8
// as their element type.
func Uint8s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan uint8):
if !more {
return true
}
out.(chan uint8) <- element
}
return false
},
}
}
// Int16s returns a config intended to fan-in channels with int16
// as their element type.
func Int16s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan int16):
if !more {
return true
}
out.(chan int16) <- element
}
return false
},
}
}
// Uint16s returns a config intended to fan-in channels with uint16
// as their element type.
func Uint16s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan uint16):
if !more {
return true
}
out.(chan uint16) <- element
}
return false
},
}
}
// Int32s returns a config intended to fan-in channels with int32
// as their element type.
func Int32s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan int32):
if !more {
return true
}
out.(chan int32) <- element
}
return false
},
}
}
// Uint32s returns a config intended to fan-in channels with uint32
// as their element type.
func Uint32s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan uint32):
if !more {
return true
}
out.(chan uint32) <- element
}
return false
},
}
}
// Int64s returns a config intended to fan-in channels with int64
// as their element type.
func Int64s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan int64):
if !more {
return true
}
out.(chan int64) <- element
}
return false
},
}
}
// Uint64s returns a config intended to fan-in channels with uint64
// as their element type.
func Uint64s() Config {
return Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan uint64):
if !more {
return true
}
out.(chan uint64) <- element
}
return false
},
}
}
// SelectFunc is a function that implements the core logic of a fan-in implementation for a particular
// type. They should contain a single select statement that listens on the `done`
// channel and the `in` channel. They must type-assert the `in` channel to be a
// channel of the proper input type. When they receive an element on the `in` channel
// they must send it on the `out` channel (also type-asserted). They should return
// true *only* if they receive a value from the `done` channel or if their `in` channel
// is closed. All implementations look essentially like this:
//
// func(done <-chan struct{}, in, out interface{}) bool {
// select {
// case <-done:
// return true
// case element, more := <-in.(<-chan int):
// if !more {
// return true
// }
// out.(chan int) <- element
// }
// return false
// }
//
// The only variation is the type of channel that `in` and `out` are asserted to be.
type SelectFunc func(done <-chan struct{}, in, out interface{}) (shouldStop bool)
// Config is the configuration for fanning in channels of a particular element type.
type Config struct {
// SelectFunc is a function that (if set) will be used to listen on a channel and
// send data on another channel. If it is not provided, a reflect-based default
// will be used. This has a significant performance penalty, but it will work for
// all types.
//
// To properly implement a SelectFunc, you must specialize it to the type of data
// that you will be fanning over the channels. See the docs on the SelectFunc type
// for examples
SelectFunc
}
// reflectiveSelectFunc is the default implementation of the Fan's SelectFunc. It expects
// slightly different input parameters than the one that a user provides. In particular, the
// concrete type of `in` and `out` should be reflect.Values in instead of being concrete channel
// types. This is to save calling reflect.ValueOf on each of them during every loop iteration.
//
// This function implements exactly the same logic as the example SelectFunc in the docs except
// it works for any channel type. You do pay a pretty stiff performance penalty though.
func reflectiveSelectFunc(done <-chan struct{}, in, out interface{}) (shouldStop bool) {
const (
DoneChanClosed = 0
InputChanRead = 1
)
selectConfig := []reflect.SelectCase{
DoneChanClosed: reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(done),
},
InputChanRead: reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: in.(reflect.Value),
},
}
switch caseChosen, elem, more := reflect.Select(selectConfig); caseChosen {
case DoneChanClosed:
return true
case InputChanRead:
if !more {
return true
}
out.(reflect.Value).Send(elem)
}
return false
}
// FanIn accepts a done channel and a variable number of channels. It returns a
// receive-only channel of the same type as the input channels, which must be type-asserted
// by the caller in order to be usable. While the done channel is not closed, values sent over the input
// channels will become available on the returned channel. When all input channels
// close or the done channel closes, the output channel will close.
//
// This will panic if no channels are provided, if values other than channels are provided,
// if send-only channels are provided, or if the provided channels are the not
// the same element type (though a mixture of receive-only and bidirectional channels with the
// same element type is fine).
func (c Config) FanIn(done <-chan struct{}, channels ...interface{}) interface{} {
if len(channels) < 1 {
panic(fmt.Errorf("concurrent.FanIn() called with no channels provided"))
}
elementType := reflect.TypeOf(nil)
// make sure all channels are the same type and are actually channels
for i, channel := range channels {
t := reflect.TypeOf(channel)
// panic if it's not a channel
if t.Kind() != reflect.Chan {
panic(fmt.Errorf("channels[%d] is not a channel, is %v", i, t.Kind()))
}
// panic if we can't receive
if t.ChanDir() != reflect.BothDir && t.ChanDir() != reflect.RecvDir {
panic(fmt.Errorf("channels[%d] does not support receive, has dir %v", i, t.ChanDir()))
}
// if we are processing the element type of the first channel, set the element type
// that we will assume for the rest of the channels
if elementType == reflect.TypeOf(nil) {
elementType = t.Elem()
} else if elementType != t.Elem() {
// if this is not the first channel, this channel's element type needs to match that of the
// first channel we processed.
panic(fmt.Errorf("channels[%d] has element type %v, which does not match previous element type %v", i, t.Elem(), elementType))
}
}
output := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, elementType), 0)
var wg sync.WaitGroup
wg.Add(len(channels))
// launch a worker goroutine for each input channel
for _, channel := range channels {
go func(loopBody SelectFunc, done <-chan struct{}, inChan, outChan interface{}) {
// ensure that the inChan to each fan-in worker is receive-only
inChan = reflect.ValueOf(inChan).Convert(reflect.ChanOf(reflect.RecvDir, elementType)).Interface()
// if no select function provided, fall back on a reflection-based implementation
if loopBody == nil {
loopBody = reflectiveSelectFunc
inChan = reflect.ValueOf(inChan)
outChan = reflect.ValueOf(outChan)
}
defer wg.Done()
for {
if loopBody(done, inChan, outChan) {
break
}
}
}(c.SelectFunc, done, channel, output.Interface())
}
// make sure we close our output channel when our waitgroup finishes
go func() {
defer output.Close()
wg.Wait()
}()
// return output as receive-only
return output.Convert(reflect.ChanOf(reflect.RecvDir, elementType)).Interface()
}