-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lb.go
92 lines (77 loc) · 2.2 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright © 2021 Hedzr Yeh.
// Package lb provides a flexible load balancer with pluggable strategies.
package lb
import (
"log"
"sync"
"github.com/hedzr/lb/hash"
"github.com/hedzr/lb/lbapi"
"github.com/hedzr/lb/random"
"github.com/hedzr/lb/rr"
"github.com/hedzr/lb/version"
"github.com/hedzr/lb/wrandom"
"github.com/hedzr/lb/wrr"
)
// New make a new instance of a balancer.
//
// l := lb.New(lb.WeightedRoundRobin, lb.WithPeers(some-peers-here...))
// fmt.Println(l.Next(lbapi.DummyFactor)
//
// check out the real example in test codes.
func New(algorithm string, opts ...lbapi.Opt) lbapi.Balancer {
kbs.RLock()
defer kbs.RUnlock()
if g, ok := knownBalancers[algorithm]; ok {
return g(opts...)
}
log.Fatalf("unknown/unregistered balancer and generator: %q", algorithm)
return nil // unreachable
}
// WithPeers adds the initial peers.
func WithPeers(peers ...lbapi.Peer) lbapi.Opt {
return func(balancer lbapi.Balancer) {
for _, p := range peers {
balancer.Add(p)
}
}
}
// Register assign a (algorithm, generator) pair.
func Register(algorithm string, generator func(opts ...lbapi.Opt) lbapi.Balancer) {
kbs.Lock()
defer kbs.Unlock()
knownBalancers[algorithm] = generator
}
// Unregister revoke a (algorithm, generator) pair.
func Unregister(algorithm string) {
kbs.Lock()
defer kbs.Unlock()
delete(knownBalancers, algorithm)
}
const (
// Random algorithm
Random = "random"
// RoundRobin algorithm
RoundRobin = "round-robin"
// WeightedRoundRobin algorithm
WeightedRoundRobin = "weighted-round-robin"
// ConsistentHash algorithm
ConsistentHash = "consistent-hash"
// WeightedRandom algorithm
WeightedRandom = "weighted-random"
// VersioningWRR algorithm
VersioningWRR = "versioning-wrr"
)
func init() {
kbs.Lock()
defer kbs.Unlock()
knownBalancers = make(map[string]func(opts ...lbapi.Opt) lbapi.Balancer)
knownBalancers[Random] = random.New
knownBalancers[RoundRobin] = rr.New
knownBalancers[WeightedRoundRobin] = wrr.New
knownBalancers[ConsistentHash] = hash.New
knownBalancers[WeightedRandom] = wrandom.New
knownBalancers[VersioningWRR] = version.New
}
var knownBalancers map[string]func(opts ...lbapi.Opt) lbapi.Balancer
var kbs sync.RWMutex
// need await for go 2 generic