extra/channels/fanout.go

24 lines
463 B
Go
Raw Permalink Normal View History

2022-05-01 21:50:12 +03:00
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
}