Fan-in and fan-out — merging and broadcasting on channels

Contents

  1. 1. Fan-in
    1. 1.1. Goroutine version
    2. 1.2. Recursive version
    3. 1.3. Reflect version
  2. 2. Fan-out
    1. 2.1. Synchronous version
    2. 2.2. Async goroutine version
    3. 2.3. Reflect version

Two common channel patterns: fan-in (merge many channels of the same element type into one) and fan-out (broadcast one stream to many outputs).

Fan-in

Drain every input into a single out channel; close out when all inputs finish.

Goroutine version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func fanIn(chans ...<-chan interface{}) <-chan interface{} {
out := make(chan interface{})

go func() {
var wg sync.WaitGroup
wg.Add(len(chans))
for _, ch := range chans {
go func(ch <-chan interface{}) {
for v := range ch {
out <- v
}
wg.Done()
}(ch)
}
// Wait for all forwarders before closing out
wg.Wait()
close(out)
}()
return out
}

WaitGroup avoids closing out while a goroutine might still send (out <- v), which would panic.

Recursive version

Split inputs in half and merge recursively. mergeTwo uses a nil channel so select blocks reads when a side is done.

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
func fanInRecur(chans ...<-chan interface{}) <-chan interface{} {
switch len(chans) {
case 0:
c := make(chan interface{})
close(c)
// Nothing to merge: closed channel (receive OK, send blocked)
return c
case 1:
return chans[0]
case 2:
return mergeTwo(chans[0], chans[1])
default:
// Halve and recurse
m := len(chans) / 2
return mergeTwo(
fanInRecur(chans[:m]...),
fanInRecur(chans[m:]...))
}
}

func mergeTwo(a, b <-chan interface{}) <-chan interface{} {
c := make(chan interface{})
go func() {
defer close(c)
for a != nil || b != nil { // while at least one side is readable
select {
case v, ok := <-a:
if !ok { // a closed — nil blocks this case
a = nil
continue
}
c <- v
case v, ok := <-b:
if !ok { // b closed — nil blocks this case
b = nil
continue
}
c <- v
}
}
}()
return c
}

Reflect version

Build reflect.SelectCase slices for a multi-case receive loop.

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
func fanInReflect(chans ...<-chan interface{}) <-chan interface{} {
out := make(chan interface{})
go func() {
defer close(out)
// Build select cases
var cases []reflect.SelectCase
for _, c := range chans {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(c),
})
}

// Loop until every channel is drained
for len(cases) > 0 {
i, v, ok := reflect.Select(cases)
if !ok {
// Channel closed — drop from cases
cases = append(cases[:i], cases[i+1:]...)
continue
}
out <- v.Interface()
}
}()
return out
}

Benchmarks:

Fan-in implementation benchmark comparison

Fan-out

Synchronous version

Send each value to every output in turn; close all outputs when the input closes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func fanOut(ch <-chan interface{}, out []chan interface{}) {
go func() {
defer func() { // Close all outputs on exit
for i := range out {
close(out[i])
}
}()

for v := range ch { // Read input
v := v
for i := range out {
i := i
out[i] <- v // Blocking send to each output
}
}
}()
}

Async goroutine version

Spawning a goroutine per send avoids blocking the reader on slow outputs—but watch shutdown:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func fanOut(ch <-chan interface{}, out []chan interface{}) {
go func() {
defer func() { // Close all outputs on exit
for i := range out {
close(out[i])
}
}()

for v := range ch { // Read input
v := v
for i := range out {
i := i
// Async send
go func(){}
out[i] <- v
}()
}
}
}()
}

On exit you may close outputs while a send goroutine still runs; each out[i] needs a reader or the send blocks. A WaitGroup waits for in-flight sends before close:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func fanOutAsync(ch <-chan interface{}, out []chan interface{}) {
go func() {
var wg sync.WaitGroup
defer func() { // Close outputs after all sends finish
wg.Wait()
for i := range out {
close(out[i])
}
}()

for v := range ch { // Read input
v := v
for i := range out {
i := i
wg.Add(1)
go func() { // One slow output must not block others
out[i] <- v
wg.Done()
}()
}
}
}()
}

Reflect version

Pre-build send cases; after a successful send, set that case’s channel to nil so it cannot fire again until the next value.

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
func fanOutReflect(ch <-chan interface{}, out []chan interface{}) {
go func() {
defer func() { // Close all outputs on exit
for i := range out {
close(out[i])
}
}()
cases := make([]reflect.SelectCase, len(out))
// Build send cases
for i := range cases {
cases[i].Dir = reflect.SelectSend
}
for v := range ch {
v := v
// Refresh Send values
for i := range cases {
cases[i].Chan = reflect.ValueOf(out[i])
cases[i].Send = reflect.ValueOf(v)
}
// One select per output for this value
for range cases {
chosen, _, _ := reflect.Select(cases)
// Nil blocks re-selection until next round
cases[chosen].Chan = reflect.ValueOf(nil)
}
}
}()
}

Benchmarks:

Fan-out implementation benchmark comparison

Test harness: concurrency

Questions? Leave a comment below or email: newbvirgil@gmail.com