2019-03-28 10:52:31 +00:00
|
|
|
import
|
2020-11-17 14:36:41 +00:00
|
|
|
chronos, chronicles/chronos_tools
|
2019-04-05 08:13:22 +00:00
|
|
|
|
2020-08-14 21:31:09 +00:00
|
|
|
export
|
|
|
|
chronos_tools
|
2020-06-17 16:14:05 +00:00
|
|
|
|
|
|
|
template awaitWithTimeout*[T](operation: Future[T],
|
|
|
|
deadline: Future[void],
|
|
|
|
onTimeout: untyped): T =
|
|
|
|
let f = operation
|
|
|
|
await f or deadline
|
|
|
|
if not f.finished:
|
2020-11-17 14:36:41 +00:00
|
|
|
# If we don't wait for for the cancellation here, it's possible that
|
|
|
|
# the "next" operation will run concurrently to this one, messing up
|
|
|
|
# the order of operations (since await/async is not fair)
|
|
|
|
await cancelAndWait(f)
|
2024-01-16 17:12:47 +00:00
|
|
|
|
|
|
|
if f.cancelled: # It could have finished instead of getting cancelled
|
2020-06-17 16:14:05 +00:00
|
|
|
onTimeout
|
|
|
|
else:
|
2024-01-16 17:12:47 +00:00
|
|
|
await f # Await avoids extraneous exception effects
|
2020-06-17 16:14:05 +00:00
|
|
|
|
|
|
|
template awaitWithTimeout*[T](operation: Future[T],
|
|
|
|
timeout: Duration,
|
|
|
|
onTimeout: untyped): T =
|
|
|
|
awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
|
2021-02-04 14:45:50 +00:00
|
|
|
|
|
|
|
template awaitWithTimeout*(operation: Future[void],
|
|
|
|
deadline: Future[void],
|
|
|
|
onTimeout: untyped) =
|
|
|
|
let f = operation
|
|
|
|
await f or deadline
|
|
|
|
if not f.finished:
|
|
|
|
# If we don't wait for for the cancellation here, it's possible that
|
|
|
|
# the "next" operation will run concurrently to this one, messing up
|
|
|
|
# the order of operations (since await/async is not fair)
|
|
|
|
await cancelAndWait(f)
|
2024-01-16 17:12:47 +00:00
|
|
|
|
|
|
|
if f.cancelled: # It could have finished instead of getting cancelled
|
2021-02-04 14:45:50 +00:00
|
|
|
onTimeout
|
|
|
|
|
|
|
|
template awaitWithTimeout*(operation: Future[void],
|
|
|
|
timeout: Duration,
|
|
|
|
onTimeout: untyped) =
|
|
|
|
awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
|