@@ -159,27 +159,23 @@ func (sq *servingQueue) newTask(peer *clientPeer, maxTime uint64, priority int64
159
159
// run tokens from the token channel and allow the corresponding tasks to run
160
160
// without entering the priority queue.
161
161
func (sq * servingQueue ) threadController () {
162
+ defer sq .wg .Done ()
162
163
for {
163
164
token := make (runToken )
164
165
select {
165
166
case best := <- sq .queueBestCh :
166
167
best .tokenCh <- token
167
168
case <- sq .stopThreadCh :
168
- sq .wg .Done ()
169
169
return
170
170
case <- sq .quit :
171
- sq .wg .Done ()
172
171
return
173
172
}
174
- <- token
175
173
select {
176
174
case <- sq .stopThreadCh :
177
- sq .wg .Done ()
178
175
return
179
176
case <- sq .quit :
180
- sq .wg .Done ()
181
177
return
182
- default :
178
+ case <- token :
183
179
}
184
180
}
185
181
}
@@ -298,6 +294,7 @@ func (sq *servingQueue) addTask(task *servingTask) {
298
294
// and always tries to send the highest priority task to queueBestCh. Successfully sent
299
295
// tasks are removed from the queue.
300
296
func (sq * servingQueue ) queueLoop () {
297
+ defer sq .wg .Done ()
301
298
for {
302
299
if sq .best != nil {
303
300
expTime := sq .best .expTime
@@ -316,15 +313,13 @@ func (sq *servingQueue) queueLoop() {
316
313
sq .best , _ = sq .queue .PopItem ().(* servingTask )
317
314
}
318
315
case <- sq .quit :
319
- sq .wg .Done ()
320
316
return
321
317
}
322
318
} else {
323
319
select {
324
320
case task := <- sq .queueAddCh :
325
321
sq .addTask (task )
326
322
case <- sq .quit :
327
- sq .wg .Done ()
328
323
return
329
324
}
330
325
}
@@ -335,6 +330,7 @@ func (sq *servingQueue) queueLoop() {
335
330
// of active thread controller goroutines.
336
331
func (sq * servingQueue ) threadCountLoop () {
337
332
var threadCountTarget int
333
+ defer sq .wg .Done ()
338
334
for {
339
335
for threadCountTarget > sq .threadCount {
340
336
sq .wg .Add (1 )
@@ -347,14 +343,12 @@ func (sq *servingQueue) threadCountLoop() {
347
343
case sq .stopThreadCh <- struct {}{}:
348
344
sq .threadCount --
349
345
case <- sq .quit :
350
- sq .wg .Done ()
351
346
return
352
347
}
353
348
} else {
354
349
select {
355
350
case threadCountTarget = <- sq .setThreadsCh :
356
351
case <- sq .quit :
357
- sq .wg .Done ()
358
352
return
359
353
}
360
354
}
0 commit comments