-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathleast_busy.go
73 lines (63 loc) · 1.37 KB
/
least_busy.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
package slb
import (
"container/heap"
"net/http"
"strings"
)
// leastBusy is a scheduling strategy that looks at the amount of work
// that the workers have and schedules jobs to the least busy worker.
type leastBusy struct {
nodes nodes
dispatchChan chan chan node
completeChan chan *http.Response
}
func newLeastBusy(hosts []string) *leastBusy {
nodes := newNodes(hosts)
heap.Init(&nodes)
lb := &leastBusy{
nodes: nodes,
dispatchChan: make(chan chan node),
completeChan: make(chan *http.Response),
}
go lb.balance()
return lb
}
func (l *leastBusy) Dispatch() node {
nodeChan := make(chan node)
l.dispatchChan <- nodeChan
return <-nodeChan
}
func (l *leastBusy) Complete(res *http.Response) {
l.completeChan <- res
}
func (l *leastBusy) balance() {
for {
select {
case nodeChan := <-l.dispatchChan:
nodeChan <- l.dispatch()
case res := <-l.completeChan:
l.complete(res.Request.URL.Host)
}
}
}
func (l *leastBusy) dispatch() node {
node := heap.Pop(&l.nodes).(*node)
node.pending += 1
heap.Push(&l.nodes, node)
heap.Fix(&l.nodes, node.index)
return *node
}
func (l *leastBusy) complete(host string) {
var n *node
for _, node := range l.nodes {
if strings.Compare(node.host, host) == 0 {
n = node
break
}
}
n.pending -= 1
heap.Fix(&l.nodes, n.index)
}
func (l *leastBusy) String() string {
return l.nodes.String()
}