-
Notifications
You must be signed in to change notification settings - Fork 1
/
selector.go
44 lines (37 loc) · 894 Bytes
/
selector.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
package multinet
import (
"fmt"
"net/netip"
)
type selectorBase struct {
cfg *Config
ds []Dialer
}
func (s *selectorBase) addrHashKey(r netip.AddrPort) string {
if s.cfg.HashPort {
return r.String()
}
return r.Addr().String()
}
type Selector interface {
Select(r netip.AddrPort) Dialer
}
type SelectorBuilder func(cfg *Config, ds []Dialer) Selector
var sbm = map[string]SelectorBuilder{
"random": newRandomSelector,
"hash": newAddrHashSelector,
"round-robin": newRoundRobinSelector,
"weighted-lru": newWeightedLRUSelector,
//todo: least utilized path selector
}
func GetSelector(cfg *Config) (Selector, error) {
sb, z := sbm[cfg.Algorithm]
if !z {
return nil, fmt.Errorf("Unknown selector: %v", cfg.Algorithm)
}
ds, err := BuildDialers(cfg)
if err != nil {
return nil, err
}
return sb(cfg, ds), nil
}