This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
/
queue.go
190 lines (151 loc) · 3.72 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scheduler
import (
"errors"
"sync"
)
var (
errQueueEmpty = errors.New("queue empty")
errLimitExceeded = errors.New("limit exceeded")
)
type jobHandler func(queuedJob)
type queue struct {
Event chan queuedJob
Err chan *queuingError
handler jobHandler
limit uint
kill chan struct{}
items []queuedJob
mutex *sync.Mutex
status queueStatus
}
type queueStatus int
const (
queueStopped queueStatus = iota // queue not running
queueRunning // queue running, but not working. the queue must be in the is state before entering handle
queueWorking // queue is currently being worked (a goroutine is currently inside q.handle())
)
type queuingError struct {
Job job
Err error
}
func (qe *queuingError) Error() string {
return qe.Err.Error()
}
func newQueue(limit uint, handler jobHandler) *queue {
return &queue{
Event: make(chan queuedJob),
Err: make(chan *queuingError),
handler: handler,
limit: limit,
kill: make(chan struct{}),
items: []queuedJob{},
mutex: &sync.Mutex{},
status: queueStopped,
}
}
// begins the queue handling loop
func (q *queue) Start() {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.status == queueStopped {
q.status = queueRunning
go q.start()
}
}
// Stop closes both Err and Event channels, and
// causes the handling loop to exit.
func (q *queue) Stop() {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.status != queueStopped {
close(q.kill)
q.status = queueStopped
}
}
/*
Below is the private, internal functionality of the queue.
These functions are not thread-safe, and should not be used
outside the queue itself. The only interaction between a queue
and outside consumers should be through the Event chan, the
Err chan, Start(), or Stop().
*/
func (q *queue) start() {
for {
select {
case e := <-q.Event:
if err := q.push(e); err != nil {
qe := &queuingError{
Err: err,
Job: e.Job(),
}
q.Err <- qe
e.Promise().Complete([]error{qe}) // Signal job termination.
continue
}
q.mutex.Lock()
if q.status == queueRunning {
q.status = queueWorking
q.mutex.Unlock()
go q.handle()
continue
}
q.mutex.Unlock()
case <-q.kill:
// this "officially" closes the Event channel.
// after this, an attempt to write to a stopped queue will panic.
// otherwise, a goroutine will sleep forever, waiting for a reader
// of Event.
go func() { close(q.Event) }()
<-q.Event
return
}
}
}
func (q *queue) handle() {
for {
item, err := q.pop()
if err == errQueueEmpty {
q.mutex.Lock()
q.status = queueRunning
q.mutex.Unlock()
return
}
q.handler(item)
}
}
func (q *queue) length() int {
return len(q.items)
}
func (q *queue) push(j queuedJob) error {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.limit == 0 || uint(q.length())+1 <= q.limit {
q.items = append(q.items, j)
return nil
}
return errLimitExceeded
}
func (q *queue) pop() (queuedJob, error) {
q.mutex.Lock()
defer q.mutex.Unlock()
var j queuedJob
if q.length() == 0 {
return j, errQueueEmpty
}
j = q.items[0]
q.items = q.items[1:]
return j, nil
}