Skip to content

Commit

Permalink
Tidy up nano-sleep wait directives to an update.nim-function
Browse files Browse the repository at this point in the history
  • Loading branch information
mjfh committed Jan 20, 2025
1 parent baefe22 commit 9d54fdc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
11 changes: 3 additions & 8 deletions nimbus/sync/beacon/worker/blocks_staged.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import
../worker_desc,
./blocks_staged/bodies,
./update/metrics,
"."/[blocks_unproc, db, helpers]
"."/[blocks_unproc, db, helpers, update]

# ------------------------------------------------------------------------------
# Private debugging & logging helpers
Expand Down Expand Up @@ -324,10 +324,7 @@ proc blocksStagedImport*(
ctx.updateMetrics()

# Allow pseudo/async thread switch.
try: await sleepAsync asyncThreadSwitchTimeSlot
except CancelledError: discard
if not ctx.daemon:
# Shutdown?
(await ctx.updateAsyncTasks()).isOkOr:
maxImport = ctx.chain.latestNumber()
break importLoop

Expand All @@ -349,9 +346,7 @@ proc blocksStagedImport*(
break importLoop

# Allow pseudo/async thread switch.
try: await sleepAsync asyncThreadSwitchTimeSlot
except CancelledError: discard
if not ctx.daemon:
(await ctx.updateAsyncTasks()).isOkOr:
maxImport = ctx.chain.latestNumber()
break importLoop

Expand Down
20 changes: 20 additions & 0 deletions nimbus/sync/beacon/worker/update.nim
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,26 @@ proc updateFinalBlockHeader*(
# Update, so it can be followed nicely
ctx.updateMetrics()


proc updateAsyncTasks*(
ctx: BeaconCtxRef;
): Future[Opt[void]] {.async: (raises: []).} =
## Allow task switch by issuing a short sleep request. The `due` argument
## allows to maintain a minimum time gap when invoking this function.
let start = Moment.now()
if ctx.pool.nextAsyncNanoSleep < start:

try: await sleepAsync asyncThreadSwitchTimeSlot
except CancelledError: discard

if ctx.daemon:
ctx.pool.nextAsyncNanoSleep = Moment.now() + asyncThreadSwitchGap
return ok()
# Shutdown?
return err()

return ok()

# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------
5 changes: 5 additions & 0 deletions nimbus/sync/beacon/worker_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const
asyncThreadSwitchTimeSlot* = chronos.nanoseconds(1)
## Nano-sleep to allows pseudo/async thread switch

asyncThreadSwitchGap* = chronos.milliseconds(300)
## Controls nano-sleep tart switch density when using this in a loop (e.g.
## for processing lists.) The constant requires a minimum time gap when
## invoking a nano-sleep utility.

# ----------------------

nFetchHeadersRequest* = 1_024
Expand Down
1 change: 1 addition & 0 deletions nimbus/sync/beacon/worker_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type
hdrSync*: HeaderImportSync ## Syncing by linked header chains
blkSync*: BlocksImportSync ## For importing/executing blocks
nextMetricsUpdate*: Moment ## For updating metrics
nextAsyncNanoSleep*: Moment ## Use nano-sleeps for task switch

# Blocks import/execution settings for importing with
# `nBodiesBatch` blocks in each round (minimum value is
Expand Down

0 comments on commit 9d54fdc

Please sign in to comment.