-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnq.go
48 lines (44 loc) · 1.27 KB
/
nq.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
package goloadbalancer
type NeverQueueBalance struct {
*ShortestExpectedDelayBalance
}
func NewNeverQueueBalance(endpoints []Endpoint) LoadBalance {
return &NeverQueueBalance{
NewShortestExpectedDelayBalance(endpoints).(*ShortestExpectedDelayBalance),
}
}
func (n *NeverQueueBalance) Select(args ...interface{}) (Endpoint, error) {
n.lock.RLock()
defer n.lock.RUnlock()
if len(n.endpoints) == 0 {
return nil, ErrNoEndpoint
}
endpoint := n.zeroConn()
if endpoint != nil {
return endpoint, nil
}
min := n.endpoints[0].(ShortestExpectedDelayEndpoint)
// Overhead = (ACTIVE+1)*256/Weight
minOverhead := float64((min.ActivateConnections()+1)*256) / float64(min.Weight())
for _, endpoint := range n.endpoints {
endpoint := endpoint.(ShortestExpectedDelayEndpoint)
overhead := float64((endpoint.ActivateConnections()+1)*256) / float64(endpoint.Weight())
if overhead < minOverhead {
min = endpoint
minOverhead = overhead
}
}
return min, nil
}
func (n *NeverQueueBalance) zeroConn() ShortestExpectedDelayEndpoint {
for _, endpoint := range n.endpoints {
endpoint := endpoint.(ShortestExpectedDelayEndpoint)
if endpoint.ActivateConnections() == 0 {
return endpoint
}
}
return nil
}
func (n *NeverQueueBalance) Name() string {
return string(NQBalanceType)
}