-
Notifications
You must be signed in to change notification settings - Fork 5
/
lb.go
55 lines (50 loc) · 1.21 KB
/
lb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2022 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Written by Changkun Ou <changkun.de>
package chann
import (
"math/rand"
"sync"
)
// Fanin provides a generic fan-in functionality for variadic channels.
func Fanin[T any](chans ...*Chann[T]) *Chann[T] {
buf := 0
for _, ch := range chans {
if ch.Len() > buf {
buf = ch.Len()
}
}
out := New[T](Cap(buf))
wg := sync.WaitGroup{}
wg.Add(len(chans))
for _, ch := range chans {
go func(ch *Chann[T]) {
for v := range ch.Out() {
out.In() <- v
}
wg.Done()
}(ch)
}
go func() {
wg.Wait()
out.Close()
}()
return out
}
// Fanout provides a generic fan-out functionality for variadic channels.
func Fanout[T any](randomizer func(max int) int, in *Chann[T], outs ...*Chann[T]) {
l := len(outs)
for v := range in.Out() {
i := randomizer(l)
if i < 0 || i > l {
i = rand.Intn(l)
}
go func(v T) { outs[i].In() <- v }(v)
}
}
// LB load balances the given input channels to the output channels.
func LB[T any](randomizer func(max int) int, ins []*Chann[T], outs []*Chann[T]) {
Fanout(randomizer, Fanin(ins...), outs...)
}