@@ -159,27 +159,23 @@ func (sq *servingQueue) newTask(peer *clientPeer, maxTime uint64, priority int64
159159// run tokens from the token channel and allow the corresponding tasks to run
160160// without entering the priority queue.
161161func (sq * servingQueue ) threadController () {
162+ defer sq .wg .Done ()
162163 for {
163164 token := make (runToken )
164165 select {
165166 case best := <- sq .queueBestCh :
166167 best .tokenCh <- token
167168 case <- sq .stopThreadCh :
168- sq .wg .Done ()
169169 return
170170 case <- sq .quit :
171- sq .wg .Done ()
172171 return
173172 }
174- <- token
175173 select {
176174 case <- sq .stopThreadCh :
177- sq .wg .Done ()
178175 return
179176 case <- sq .quit :
180- sq .wg .Done ()
181177 return
182- default :
178+ case <- token :
183179 }
184180 }
185181}
@@ -298,6 +294,7 @@ func (sq *servingQueue) addTask(task *servingTask) {
298294// and always tries to send the highest priority task to queueBestCh. Successfully sent
299295// tasks are removed from the queue.
300296func (sq * servingQueue ) queueLoop () {
297+ defer sq .wg .Done ()
301298 for {
302299 if sq .best != nil {
303300 expTime := sq .best .expTime
@@ -316,15 +313,13 @@ func (sq *servingQueue) queueLoop() {
316313 sq .best , _ = sq .queue .PopItem ().(* servingTask )
317314 }
318315 case <- sq .quit :
319- sq .wg .Done ()
320316 return
321317 }
322318 } else {
323319 select {
324320 case task := <- sq .queueAddCh :
325321 sq .addTask (task )
326322 case <- sq .quit :
327- sq .wg .Done ()
328323 return
329324 }
330325 }
@@ -335,6 +330,7 @@ func (sq *servingQueue) queueLoop() {
335330// of active thread controller goroutines.
336331func (sq * servingQueue ) threadCountLoop () {
337332 var threadCountTarget int
333+ defer sq .wg .Done ()
338334 for {
339335 for threadCountTarget > sq .threadCount {
340336 sq .wg .Add (1 )
@@ -347,14 +343,12 @@ func (sq *servingQueue) threadCountLoop() {
347343 case sq .stopThreadCh <- struct {}{}:
348344 sq .threadCount --
349345 case <- sq .quit :
350- sq .wg .Done ()
351346 return
352347 }
353348 } else {
354349 select {
355350 case threadCountTarget = <- sq .setThreadsCh :
356351 case <- sq .quit :
357- sq .wg .Done ()
358352 return
359353 }
360354 }
0 commit comments