aboutsummaryrefslogtreecommitdiff
path: root/channels/fanout.go
blob: 23a32b48655ffb94a14f1d6d8e6e10806c665d6c (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

// FanOut раскидывает очередь канала на несколько каналов равномерно
func FanOut[T any](in chan T, workers int) []chan T {
	out := make([]chan T, workers)
	for i := 0; i < workers; i++ {
		out[i] = make(chan T)
	}
	go func() {
		i := 0
		for t := range in {
			out[i] <- t
			i++
			if i == workers {
				i = 0
			}
		}
		for i := 0; i < workers; i++ {
			close(out[i])
		}
	}()
	return out
}