-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsed.go
48 lines (42 loc) · 1.12 KB
/
sed.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
import (
"sync"
)
type ShortestExpectedDelayEndpoint interface {
Endpoint
ActivateConnections() int
Weight() int
}
type ShortestExpectedDelayBalance struct {
*BaseLoadBalance
}
func NewShortestExpectedDelayBalance(endpoints []Endpoint) LoadBalance {
return &ShortestExpectedDelayBalance{
&BaseLoadBalance{
endpoints: endpoints,
lock: sync.RWMutex{},
},
}
}
func (l *ShortestExpectedDelayBalance) Select(args ...interface{}) (Endpoint, error) {
l.lock.RLock()
defer l.lock.RUnlock()
if len(l.endpoints) == 0 {
return nil, ErrNoEndpoint
}
min := l.endpoints[0].(ShortestExpectedDelayEndpoint)
// Overhead = (ACTIVE+1)*256/Weight
minOverhead := float64((min.ActivateConnections()+1)*256) / float64(min.Weight())
for _, endpoint := range l.endpoints {
endpoint := endpoint.(ShortestExpectedDelayEndpoint)
overhead := float64((endpoint.ActivateConnections()+1)*256) / float64(endpoint.Weight())
if overhead < minOverhead {
min = endpoint
minOverhead = overhead
}
}
return min, nil
}
func (l *ShortestExpectedDelayBalance) Name() string {
return string(SEDBalanceType)
}