Application 2023-08-21

Implementing Fan-In and Fan-Out in Go

Implement fan-in and fan-out concurrent patterns in Go using channels and goroutines. Fan-in aggregates multiple producer outputs; fan-out distributes one input across multiple workers.

Read in: ja
Implementing Fan-In and Fan-Out in Go

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

Tags: fan-in fan-out
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles