-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
56 lines (47 loc) · 983 Bytes
/
pool.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
package main
import (
"container/heap"
"fmt"
)
//Pool is a worker pool
type Pool []*Worker
//NewPool creates a new worker pool, and initializes the workers
func NewPool(size int, done chan *Worker) *Pool {
var pool Pool
for i := 0; i < size; i++ {
requests := make(chan Request, 100)
worker := Worker{requests, 0, i}
go worker.work(done)
pool = append(pool, &worker)
}
heap.Init(&pool)
return &pool
}
func (p Pool) String() string {
s := "Pool: "
for _, v := range p {
s += fmt.Sprint(" ", v.pending)
// s += fmt.Sprint(" ", len(v.requests))
}
return s
}
//implement heap interface
func (p Pool) Len() int {
return len(p)
}
func (p Pool) Less(i, j int) bool {
return p[i].pending < p[j].pending
}
func (p Pool) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p *Pool) Push(x interface{}) {
*p = append(*p, x.(*Worker))
}
func (p *Pool) Pop() interface{} {
old := *p
last := len(old) - 1
element := old[last]
*p = old[:last]
return element
}