@@ -10,7 +10,6 @@ import ../rng
10
10
import ../ utils
11
11
import ../ contracts/ requests
12
12
import ../ utils/ asyncheapqueue
13
- import ../ utils/ then
14
13
import ../ utils/ trackedfutures
15
14
16
15
logScope:
@@ -333,7 +332,7 @@ proc addWorker(self: SlotQueue): ?!void =
333
332
334
333
proc dispatch (self: SlotQueue ,
335
334
worker: SlotQueueWorker ,
336
- item: SlotQueueItem ) {.async .} =
335
+ item: SlotQueueItem ) {.async : (raises: []) .} =
337
336
logScope:
338
337
requestId = item.requestId
339
338
slotIndex = item.slotIndex
@@ -380,22 +379,7 @@ proc clearSeenFlags*(self: SlotQueue) =
380
379
381
380
trace " all 'seen' flags cleared"
382
381
383
- proc start * (self: SlotQueue ) {.async .} =
384
- if self.running:
385
- return
386
-
387
- trace " starting slot queue"
388
-
389
- self.running = true
390
-
391
- # must be called in `start` to avoid sideeffects in `new`
392
- self.workers = newAsyncQueue [SlotQueueWorker ](self.maxWorkers)
393
-
394
- # Add initial workers to the `AsyncHeapQueue`. Once a worker has completed its
395
- # task, a new worker will be pushed to the queue
396
- for i in 0 ..< self.maxWorkers:
397
- if err =? self.addWorker ().errorOption:
398
- error " start: error adding new worker" , error = err.msg
382
+ proc run (self: SlotQueue ) {.async : (raises: []).} =
399
383
400
384
while self.running:
401
385
try :
@@ -405,8 +389,8 @@ proc start*(self: SlotQueue) {.async.} =
405
389
# block until unpaused is true/fired, ie wait for queue to be unpaused
406
390
await self.unpaused.wait ()
407
391
408
- let worker = await self.workers.popFirst (). track (self) # if workers saturated, wait here for new workers
409
- let item = await self.queue.pop (). track (self) # if queue empty, wait here for new items
392
+ let worker = await self.workers.popFirst () # if workers saturated, wait here for new workers
393
+ let item = await self.queue.pop () # if queue empty, wait here for new items
410
394
411
395
logScope:
412
396
reqId = item.requestId
@@ -434,19 +418,34 @@ proc start*(self: SlotQueue) {.async.} =
434
418
435
419
trace " processing item"
436
420
437
- self.dispatch (worker, item)
438
- .track (self)
439
- .catch (proc (e: ref CatchableError ) =
440
- error " Unknown error dispatching worker" , error = e.msg
441
- )
421
+ asyncSpawn self.dispatch (worker, item).track (self)
442
422
443
423
await sleepAsync (1 .millis) # poll
444
424
except CancelledError :
445
425
trace " slot queue cancelled"
446
- return
426
+ break
447
427
except CatchableError as e: # raised from self.queue.pop() or self.workers.pop()
448
428
warn " slot queue error encountered during processing" , error = e.msg
449
429
430
+ proc start * (self: SlotQueue ) =
431
+ if self.running:
432
+ return
433
+
434
+ trace " starting slot queue"
435
+
436
+ self.running = true
437
+
438
+ # must be called in `start` to avoid sideeffects in `new`
439
+ self.workers = newAsyncQueue [SlotQueueWorker ](self.maxWorkers)
440
+
441
+ # Add initial workers to the `AsyncHeapQueue`. Once a worker has completed its
442
+ # task, a new worker will be pushed to the queue
443
+ for i in 0 ..< self.maxWorkers:
444
+ if err =? self.addWorker ().errorOption:
445
+ error " start: error adding new worker" , error = err.msg
446
+
447
+ asyncSpawn self.run ().track (self)
448
+
450
449
proc stop * (self: SlotQueue ) {.async .} =
451
450
if not self.running:
452
451
return
0 commit comments