diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-05-01 21:50:12 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-05-01 21:50:12 +0300 |
commit | 9fcf8e29214210612d545bed50d7f889800ac639 (patch) | |
tree | 3a99d2cd37fb8158d49abc1de6298758d205c9dd /channels/fanout.go |
Initial
Diffstat (limited to 'channels/fanout.go')
-rw-r--r-- | channels/fanout.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/channels/fanout.go b/channels/fanout.go new file mode 100644 index 0000000..23a32b4 --- /dev/null +++ b/channels/fanout.go @@ -0,0 +1,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 +} |