2022-05-18 08:19:32 +00:00
|
|
|
import pkg/chronos
|
|
|
|
|
2022-05-17 09:50:52 +00:00
|
|
|
type
|
|
|
|
Clock* = ref object of RootObj
|
|
|
|
SecondsSince1970* = int64
|
2022-06-14 13:25:48 +00:00
|
|
|
Timeout* = object of CatchableError
|
2022-05-17 09:50:52 +00:00
|
|
|
|
|
|
|
method now*(clock: Clock): SecondsSince1970 {.base.} =
|
|
|
|
raiseAssert "not implemented"
|
2022-05-18 08:19:32 +00:00
|
|
|
|
|
|
|
proc waitUntil*(clock: Clock, time: SecondsSince1970) {.async.} =
|
|
|
|
while clock.now() < time:
|
|
|
|
await sleepAsync(1.seconds)
|
2022-06-14 13:25:48 +00:00
|
|
|
|
|
|
|
proc withTimeout*(future: Future[void],
|
|
|
|
clock: Clock,
|
|
|
|
expiry: SecondsSince1970) {.async.} =
|
|
|
|
let timeout = clock.waitUntil(expiry)
|
|
|
|
try:
|
|
|
|
await future or timeout
|
|
|
|
finally:
|
|
|
|
await timeout.cancelAndWait()
|
|
|
|
if not future.completed:
|
|
|
|
await future.cancelAndWait()
|
|
|
|
raise newException(Timeout, "Timed out")
|