sets up asyncyeah

This commit is contained in:
benbierens 2023-07-13 15:12:11 +02:00
parent 7401d4af64
commit ed4fd8104b
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
3 changed files with 46 additions and 7 deletions

33
codex/asyncyeah.nim Normal file
View File

@ -0,0 +1,33 @@
import std/[macros]
var globalYeahStack* {.global, threadvar.}: seq[string]
var globalYeahInt {.global, threadvar.}: int
macro asyncyeah*(functionlike: untyped{nkProcDef | nkMethodDef | nkFuncDef}): untyped =
let fl = functionlike.copyNimTree
let closureName = newStrLitNode($fl[0])
var body = fl[6].copyNimTree
var newBody = newStmtList()
newBody.add(quote do:
inc globalYeahInt
let callName = `closureName` & $globalYeahInt
# echo "push " & callName
globalYeahStack.add(callName)
defer:
let rmIndex = globalYeahStack.find(callName)
globalYeahStack.del(rmIndex)
# echo "pop " & callName
)
body.copyChildrenTo(newBody)
fl.body = newBody
let pragmas = fl[4]
var newPragmas = newNimNode(nnkPragma)
for pragma in pragmas:
if not pragma.eqIdent("asyncyeah"):
newPragmas.add(pragma)
newPragmas.add(ident("async"))
fl[4] = newPragmas
return fl

View File

@ -36,6 +36,7 @@ import ../conf
import ../contracts import ../contracts
import ../streams import ../streams
import ../loopmeasure import ../loopmeasure
import ../asyncyeah
import ./coders import ./coders
import ./json import ./json
@ -339,9 +340,13 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf, loopMeasure: LoopMeasure)
when defined(chronosDurationThreshold): when defined(chronosDurationThreshold):
var breaches = newSeq[string]() var breaches = newSeq[string]()
proc onBreach(stackTrace: string, durationUs: int64) = proc onBreach(durationUs: int64) =
error "Duration threshold breached", durationUs, stackTrace var trace = ""
breaches.add($durationUs & " usecs at " & stackTrace) for entry in globalYeahStack:
trace = trace & " -> " & entry
error "Duration threshold breached", durationUs, trace
breaches.add($durationUs & " usecs: " & trace)
setChronosDurationThresholdBreachedHandler(onBreach) setChronosDurationThresholdBreachedHandler(onBreach)

View File

@ -19,9 +19,10 @@ import ./repostore
import ../utils/timer import ../utils/timer
import ../clock import ../clock
import ../systemclock import ../systemclock
import ../asyncyeah
const const
DefaultBlockMaintenanceInterval* = 10.minutes DefaultBlockMaintenanceInterval* = 10.seconds
DefaultNumberOfBlocksToMaintainPerInterval* = 1000 DefaultNumberOfBlocksToMaintainPerInterval* = 1000
type type
@ -42,9 +43,9 @@ proc new*(
clock: Clock = SystemClock.new() clock: Clock = SystemClock.new()
): BlockMaintainer = ): BlockMaintainer =
## Create new BlockMaintainer instance ## Create new BlockMaintainer instance
## ##
## Call `start` to begin looking for for expired blocks ## Call `start` to begin looking for for expired blocks
## ##
BlockMaintainer( BlockMaintainer(
repoStore: repoStore, repoStore: repoStore,
interval: interval, interval: interval,
@ -63,7 +64,7 @@ proc processBlockExpiration(self: BlockMaintainer, be: BlockExpiration): Future[
else: else:
inc self.offset inc self.offset
proc runBlockCheck(self: BlockMaintainer): Future[void] {.async.} = proc runBlockCheck(self: BlockMaintainer): Future[void] {.asyncyeah.} =
let expirations = await self.repoStore.getBlockExpirations( let expirations = await self.repoStore.getBlockExpirations(
maxNumber = self.numberOfBlocksPerInterval, maxNumber = self.numberOfBlocksPerInterval,
offset = self.offset offset = self.offset