-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwlc.go
50 lines (44 loc) · 1.19 KB
/
wlc.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
package goloadbalancer
import (
"sync"
)
type WeightLeastConnectionsEndpoint interface {
Endpoint
LeastConnectionsEndpoint
Weight() int
}
type WeightLeastConnectionsBalance struct {
// lock sync.Mutex
// endpoints []Endpoint
*BaseLoadBalance
}
func NewWeightLeastConnectionsBalance(endpoints []Endpoint) *WeightLeastConnectionsBalance {
return &WeightLeastConnectionsBalance{
&BaseLoadBalance{
endpoints: endpoints,
lock: sync.RWMutex{},
},
}
}
func (l *WeightLeastConnectionsBalance) Select(_ ...interface{}) (Endpoint, error) {
l.lock.RLock()
defer l.lock.RUnlock()
if len(l.endpoints) == 0 {
return nil, ErrNoEndpoint
}
min := l.endpoints[0].(WeightLeastConnectionsEndpoint)
// Overhead =( Active * 256 + Inactive )/Weight
minRatio := float64(min.ActivateConnections()*256+min.InactiveConnections()) / float64(min.Weight())
for _, ep := range l.endpoints {
endpoint := ep.(WeightLeastConnectionsEndpoint)
ratio := float64(endpoint.Weight()) / float64(endpoint.ActivateConnections()+1)
if ratio < minRatio {
min = endpoint
minRatio = ratio
}
}
return min, nil
}
func (r *WeightLeastConnectionsBalance) Name() string {
return string(WLCBalanceType)
}