mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-02 21:43:11 +00:00
* checked exceptions in stores
* makes asynciter as much exception safe as it gets
* introduce "SafeAsyncIter" that uses Results and limits exceptions to cancellations
* adds {.push raises: [].} to errors
* uses SafeAsyncIter in "listBlocks" and in "getBlockExpirations"
* simplifies safeasynciter (magic of auto)
* gets rid of ugly casts
* tiny fix in hte way we create raising futures in tests of safeasynciter
* Removes two more casts caused by using checked exceptions
* adds an extended explanation of one more complex SafeAsyncIter test
* adds missing "finishOnErr" param in slice constructor of SafeAsyncIter
* better fix for "Error: Exception can raise an unlisted exception: Exception" error.
---------
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
54 lines
1.5 KiB
Nim
54 lines
1.5 KiB
Nim
## Nim-Codex
|
|
## Copyright (c) 2023 Status Research & Development GmbH
|
|
## Licensed under either of
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
## at your option.
|
|
## This file may not be copied, modified, or distributed except according to
|
|
## those terms.
|
|
|
|
## Timer
|
|
## Used to execute a callback in a loop
|
|
|
|
{.push raises: [].}
|
|
|
|
import pkg/chronos
|
|
|
|
import ../logutils
|
|
|
|
type
|
|
TimerCallback* = proc(): Future[void] {.gcsafe, async: (raises: []).}
|
|
Timer* = ref object of RootObj
|
|
callback: TimerCallback
|
|
interval: Duration
|
|
name: string
|
|
loopFuture: Future[void]
|
|
|
|
proc new*(T: type Timer, timerName = "Unnamed Timer"): Timer =
|
|
## Create a new Timer intance with the given name
|
|
Timer(name: timerName)
|
|
|
|
proc timerLoop(timer: Timer) {.async: (raises: []).} =
|
|
try:
|
|
while true:
|
|
await timer.callback()
|
|
await sleepAsync(timer.interval)
|
|
except CancelledError:
|
|
discard # do not propagate as timerLoop is asyncSpawned
|
|
|
|
method start*(
|
|
timer: Timer, callback: TimerCallback, interval: Duration
|
|
) {.gcsafe, base.} =
|
|
if timer.loopFuture != nil:
|
|
return
|
|
trace "Timer starting: ", name = timer.name
|
|
timer.callback = callback
|
|
timer.interval = interval
|
|
timer.loopFuture = timerLoop(timer)
|
|
|
|
method stop*(timer: Timer) {.base, async: (raises: []).} =
|
|
if timer.loopFuture != nil and not timer.loopFuture.finished:
|
|
trace "Timer stopping: ", name = timer.name
|
|
await timer.loopFuture.cancelAndWait()
|
|
timer.loopFuture = nil
|