-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rln-relay): graceful retries on rpc calls (#2250)
* fix(rln-relay): graceful retries on rpc calls * fix: missing file
- Loading branch information
Showing
2 changed files
with
92 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
waku/waku_rln_relay/group_manager/on_chain/retry_wrapper.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import | ||
chronos | ||
|
||
|
||
type RetryStrategy* = object | ||
shouldRetry*: bool | ||
retryDelay*: Duration | ||
retryCount*: uint | ||
|
||
proc new*(T: type RetryStrategy): RetryStrategy = | ||
return RetryStrategy( | ||
shouldRetry: true, | ||
retryDelay: 1000.millis, | ||
retryCount: 3 | ||
) | ||
|
||
|
||
template retryWrapper*(res: auto, | ||
retryStrategy: RetryStrategy, | ||
errStr: string, | ||
body: untyped): auto = | ||
var retryCount = retryStrategy.retryCount | ||
var shouldRetry = retryStrategy.shouldRetry | ||
while shouldRetry and retryCount > 0: | ||
try: | ||
res = body | ||
shouldRetry = false | ||
except: | ||
retryCount -= 1 | ||
await sleepAsync(retryStrategy.retryDelay) | ||
if shouldRetry: | ||
raise newException(CatchableError, errStr & ": " & $getCurrentExceptionMsg()) |