Overview
Implementing the fan-in and fan-out patterns in Go for concurrent processing.
What are Fan-In and Fan-Out?
Fan-in is the process of aggregating multiple inputs into one, while fan-out is the process of distributing a single input into multiple outputs.
Fan-in aggregates data, and fan-out distributes data.
In Go, this can be achieved using channels and goroutines.
Implementation
The source code is also available on github.
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
func producer(id int, out chan<- int) {
for i := 0; i < 5; i++ {
value := rand.Intn(100)
fmt.Printf("Producer %d: Sending %d\n", id, value)
out <- value
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))
}
close(out)
}
func fanIn(inputs []<-chan int, out chan<- int) {
var wg sync.WaitGroup
wg.Add(len(inputs))
for _, input := range inputs {
go func(ch <-chan int) {
for value := range ch {
out <- value
}
wg.Done()
}(input)
}
go func() {
wg.Wait()
close(out)
}()
}
func main() {
rand.Seed(time.Now().UnixNano())
// Fan-Out
numProducers := 3
inputs := make([]chan int, numProducers)
for i := 0; i < numProducers; i++ {
inputs[i] = make(chan int)
go producer(i+1, inputs[i])
}
// Convert channels to <-chan int
inputChans := make([]<-chan int, numProducers)
for i := 0; i < numProducers; i++ {
inputChans[i] = inputs[i]
}
// Fan-In
result := make(chan int)
go fanIn(inputChans, result)
// Consume the merged values
for value := range result {
fmt.Printf("Consumer: Received %d\n", value)
}
fmt.Println("All done!")
}
Data is distributed in the fan-out process and aggregated in the fan-in process.
Thoughts
I lack confidence in concurrent processing, so I need to study more...
References
- ludwig125.hatenablog.com - Go Language Pipeline, Fan-In, Fan-Out
- devlights.hatenablog.com - Go Memo-73 (Channel for Aggregating Data with Fan-In Pattern)
- devlights.hatenablog.com - Go Memo-79 (Function for Fan-Out with Specified Number of Workers)
- tech-up.hatenablog.com - Fan-Out, Fan-In Pattern [Go]
- selfnote.work - [Golang] Challenge with Algorithms ~ Let's Implement Fan In/Fan Out with Channels! ~
- go.dev - Go Concurrency Patterns: Pipelines and cancellation
- kapoorrahul.medium.com - Golang Fan-In Fan-Out Concurrency Pattern
- mariocarrion.com - LEARNING GO: FAN-IN/FAN-OUT CONCURRENCY PATTERN
- www.golinuxcloud.com - Go Fan Out Fan In Concurrency Pattern Explained