aboutsummaryrefslogtreecommitdiff
path: root/channels/fanin.go
blob: 679aeee07380729e44278f9bf4aafd6f584af550 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package channels

import "sync/atomic"

// FanIn сливает несколько каналов в один
func FanIn[T any](chans ...chan T) chan T {
	out := make(chan T)
	workers := int64(len(chans))
	for _, ch := range chans {
		func(ch chan T) {
			go func() {
				for t := range ch {
					out <- t
				}
				atomic.AddInt64(&workers, -1)
				if workers == 0 {
					close(out)
				}
			}()
		}(ch)
	}
	return out
}