Many times I forget to keep it simple and loose my focus and get astray and dig into some really complicated stuff to discover at the end I am trying to use a rocket launcher to kill a fly :) Remember that simple is not equal to stupid. Looking at the Linux Kernel Source really makes me feel I am steady and firmly standing on the ground ;) For example I was trying to do timers like this:
proc RaftTimerCreateCustomImpl*(timerInterval: int, oneshot: bool, timerCallback: RaftTimerCallback): RaftTimer {.nimcall, gcsafe.} =
var
timer = RaftTimer(mtx: Lock(), canceled: false, expired: false, timeout: timerInterval, oneshot: oneshot)
initLock(timer.mtx)
addTimer(timer.timeout, timer.oneshot, proc (fd: AsyncFD): bool {.closure, gcsafe.} =
withLock(timer.mtx):
if not timer.canceled:
timerCallback(timer)
if timer.oneshot:
timer.expired = true
return true
else:
return false
else:
return true
)
timer
proc RaftTimerCancelCustomImpl*(timer: RaftTimer): bool {.nimcall, gcsafe, discardable.} =
withLock(timer.mtx):
if not timer.expired and not timer.canceled:
timer.canceled = true
else:
return false
And apparently it is much easier just like this:
var fut = asyncSleep(1000)
fut.callback = proc() = echo "ALA BALA"
:D
More Elaborately:
proc timeoutFuture*[T](future: Future[T], timeout: int): Future[T] {.async.} =
let timeoutFuture = sleepAsync(timeout * 1000)
timeoutFuture.callback = proc =
if not future.finished:
future.fail(TimeoutError(msg: "The future didn't complete in time", timeout: timeout))
return await future
https://lnkd.in/dTcdFHdv
https://lnkd.in/ddi_xb5s
Security Engineering @ Datadog
1moThis is super cool 👏