From ed4fd8104b58b87571417bf1294675f21affe818 Mon Sep 17 00:00:00 2001 From: benbierens Date: Thu, 13 Jul 2023 15:12:11 +0200 Subject: [PATCH] sets up asyncyeah --- codex/asyncyeah.nim | 33 +++++++++++++++++++++++++++++++++ codex/rest/api.nim | 11 ++++++++--- codex/stores/maintenance.nim | 9 +++++---- 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 codex/asyncyeah.nim diff --git a/codex/asyncyeah.nim b/codex/asyncyeah.nim new file mode 100644 index 00000000..603873aa --- /dev/null +++ b/codex/asyncyeah.nim @@ -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 diff --git a/codex/rest/api.nim b/codex/rest/api.nim index 938c59c4..1a2d69f2 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -36,6 +36,7 @@ import ../conf import ../contracts import ../streams import ../loopmeasure +import ../asyncyeah import ./coders import ./json @@ -339,9 +340,13 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf, loopMeasure: LoopMeasure) when defined(chronosDurationThreshold): var breaches = newSeq[string]() - proc onBreach(stackTrace: string, durationUs: int64) = - error "Duration threshold breached", durationUs, stackTrace - breaches.add($durationUs & " usecs at " & stackTrace) + proc onBreach(durationUs: int64) = + var trace = "" + for entry in globalYeahStack: + trace = trace & " -> " & entry + + error "Duration threshold breached", durationUs, trace + breaches.add($durationUs & " usecs: " & trace) setChronosDurationThresholdBreachedHandler(onBreach) diff --git a/codex/stores/maintenance.nim b/codex/stores/maintenance.nim index ce06dafa..061228f2 100644 --- a/codex/stores/maintenance.nim +++ b/codex/stores/maintenance.nim @@ -19,9 +19,10 @@ import ./repostore import ../utils/timer import ../clock import ../systemclock +import ../asyncyeah const - DefaultBlockMaintenanceInterval* = 10.minutes + DefaultBlockMaintenanceInterval* = 10.seconds DefaultNumberOfBlocksToMaintainPerInterval* = 1000 type @@ -42,9 +43,9 @@ proc new*( clock: Clock = SystemClock.new() ): BlockMaintainer = ## Create new BlockMaintainer instance - ## + ## ## Call `start` to begin looking for for expired blocks - ## + ## BlockMaintainer( repoStore: repoStore, interval: interval, @@ -63,7 +64,7 @@ proc processBlockExpiration(self: BlockMaintainer, be: BlockExpiration): Future[ else: inc self.offset -proc runBlockCheck(self: BlockMaintainer): Future[void] {.async.} = +proc runBlockCheck(self: BlockMaintainer): Future[void] {.asyncyeah.} = let expirations = await self.repoStore.getBlockExpirations( maxNumber = self.numberOfBlocksPerInterval, offset = self.offset