Merging Data Streams from Multiple Producers into a Single Channel
Owner: SnippetBot
Created: 2026-07-24 00:00:46
Size: 2.17 KB
Expires: Never
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
package main
import (
"fmt"
"sync"
"time"
)
// producer generates numbers and sends them to its output channel.
func producer(id int, out chan<- int, wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < 3; i++ {
value := id*100 + i
fmt.Printf("Producer %d: Sending %d
", id, value)
out <- value
time.Sleep(time.Duration(id+1) * 50 * time.Millisecond) // Simulate varying work
}
}
// merge takes multiple input channels and merges their outputs into a single output channel.
func merge(done <-chan struct{}, inputs ...<-chan int) <-chan int {
var wg sync.WaitGroup
merged := make(chan int)
// Function to read from one input channel and send to the merged channel
output := func(c <-chan int) {
defer wg.Done()
for n := range c {
select {
case merged <- n:
case <-done:
return // Exit if done signal received
}
}
}
wg.Add(len(inputs))
for _, c := range inputs {
go output(c)
}
// Start a goroutine to close the merged channel once all inputs are closed.
go func() {
wg.Wait()
close(merged)
}()
return merged
}
func main() {
const numProducers = 3
inputChannels := make([]<-chan int, numProducers)
var producerWg sync.WaitGroup
done := make(chan struct{}) // Channel to signal graceful shutdown
fmt.Println("Main: Starting producers...")
for i := 0; i < numProducers; i++ {
ch := make(chan int)
inputChannels[i] = ch
producerWg.Add(1)
go producer(i+1, ch, &producerWg)
}
// Close producer channels once all producers are done
go func() {
producerWg.Wait()
for _, ch := range inputChannels {
close(ch.(chan int)) // Type assertion needed to close send-only channel
}
fmt.Println("Main: All producers finished and their channels closed.")
}()
// Merge the output of all producers into a single channel
mergedStream := merge(done, inputChannels...)
fmt.Println("Main: Reading from merged stream...")
for val := range mergedStream {
fmt.Printf("Main: Received from merged stream: %d
", val)
}
// In a real application, you might close `done` channel
// after some timeout or external signal.
// close(done) // Uncomment to trigger shutdown if merging is endless.
fmt.Println("Main: All data from merged stream processed.")
}