Hone logo
Hone
Problems

Go Fan-In: Merging Multiple Data Streams

The fan-in pattern is a common concurrency pattern where multiple goroutines concurrently produce data, and a single goroutine merges these streams into one. This is useful for consolidating results from parallel operations, aggregating data from various sources, or handling events from multiple producers efficiently. Your challenge is to implement this pattern in Go.

Problem Description

You are tasked with creating a Go program that simulates multiple data producers, each sending integers over a channel. Your program should then implement a fan-in mechanism to consolidate all these integer streams into a single channel. A separate goroutine will read from this combined channel and print each received integer.

Key Requirements:

  1. Multiple Producers: Create at least three separate goroutines that act as data producers. Each producer should generate a sequence of integers and send them to its own dedicated output channel.
  2. Fan-In Function: Implement a function, let's call it fanIn, that takes a variable number of input channels (each sending integers) and returns a single output channel. This function should multiplex all data from the input channels onto the single output channel.
  3. Single Consumer: A separate goroutine should read from the output channel produced by fanIn and print each integer it receives.
  4. Graceful Shutdown: Ensure that all goroutines complete their work and that the program terminates cleanly. The consumer should stop reading once all producers have closed their respective channels and all data has been processed.

Expected Behavior:

The program should start the producer goroutines. Each producer will send a predetermined set of numbers (e.g., 0 to 4). The fanIn function will then merge these numbers. The consumer goroutine will print the numbers as they become available from the merged channel. The order of numbers printed from different producers is not guaranteed, but all numbers from all producers should be printed exactly once.

Edge Cases:

  • Consider the scenario where a producer might finish sending data before others.
  • Ensure the fanIn function correctly handles an empty list of input channels (though for this challenge, we'll ensure at least three).

Examples

Example 1:

Let's say we have three producers:

  • Producer 1 sends: [0, 1]
  • Producer 2 sends: [10, 11]
  • Producer 3 sends: [20, 21]

Input: (Implicit, as described above - each producer sends its sequence)

Output: (The order can vary, but all numbers must be present)

0
10
20
1
11
21

or

10
20
0
11
1
21

etc.

Explanation:

The fanIn function receives the channels from Producers 1, 2, and 3. It then starts goroutines to read from each of these input channels and forwards the received data to a single output channel. The consumer goroutine reads from this output channel and prints each number.

Constraints

  • Each producer goroutine will send a maximum of 5 integers (e.g., 0 to 4).
  • The number of producer goroutines will be at least 3.
  • The program should be written entirely in Go.
  • Avoid using external libraries beyond Go's standard library.

Notes

  • Think about how to signal to the fanIn function that all input channels are done sending data. This is crucial for properly closing the output channel.
  • The sync.WaitGroup can be helpful for managing the lifecycle of the producer goroutines.
  • Consider using select statements within the fanIn logic to efficiently read from multiple channels.
  • The defer keyword can be useful for ensuring that channels are closed.
Loading editor...
go