-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_queue.go
59 lines (47 loc) · 876 Bytes
/
worker_queue.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
package pool
import "errors"
type queue interface {
enqueue(worker *Worker) error
dequeue() *Worker
len() int
isEmpty() bool
clear()
}
type workerQueue struct {
items []*Worker
size int
}
func newWorkerQueue(size int) *workerQueue {
return &workerQueue{
items: make([]*Worker, 0, size),
size: size,
}
}
func (q *workerQueue) enqueue(worker *Worker) error {
if worker == nil {
return errors.New("nil pointer of worker")
}
q.items = append(q.items, worker)
return nil
}
func (q *workerQueue) dequeue() *Worker {
l := q.len()
if l == 0 {
return nil
}
w := q.items[0]
q.items = q.items[1:l]
return w
}
func (q *workerQueue) len() int {
return len(q.items)
}
func (q *workerQueue) isEmpty() bool {
return q.len() == 0
}
func (q *workerQueue) clear() {
for i := 0; i < q.len(); i++ {
q.items[i].sendTask(nil)
}
q.items =q.items[:0]
}