From 9fcf8e29214210612d545bed50d7f889800ac639 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Sun, 1 May 2022 21:50:12 +0300 Subject: Initial --- channels/fanout.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 channels/fanout.go (limited to 'channels/fanout.go') 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 +} -- cgit v1.2.3