aboutsummaryrefslogtreecommitdiff
path: root/channels/fanout.go
diff options
context:
space:
mode:
Diffstat (limited to 'channels/fanout.go')
-rw-r--r--channels/fanout.go23
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
+}