2023-09-03 00:53:48 +00:00
|
|
|
# nim-raft
|
|
|
|
# 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.
|
2023-09-03 17:52:35 +00:00
|
|
|
|
2023-09-06 16:18:02 +00:00
|
|
|
import chronos
|
2023-09-03 00:53:48 +00:00
|
|
|
|
2023-09-17 00:47:29 +00:00
|
|
|
template awaitWithTimeout*[T](operation: Future[T],
|
2023-09-03 00:53:48 +00:00
|
|
|
deadline: Future[void],
|
2023-10-13 06:03:42 +00:00
|
|
|
body: untyped) =
|
2023-10-13 04:24:35 +00:00
|
|
|
let f {.inject.} = operation
|
2023-09-03 00:53:48 +00:00
|
|
|
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)
|
|
|
|
else:
|
2023-10-13 04:24:35 +00:00
|
|
|
body
|
2023-09-03 00:53:48 +00:00
|
|
|
|
2023-10-13 04:24:35 +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:
|
|
|
|
# # 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)
|
|
|
|
# onTimeout
|
|
|
|
# else:
|
|
|
|
# f.read
|
2023-09-03 00:53:48 +00:00
|
|
|
|
2023-10-13 04:24:35 +00:00
|
|
|
# template awaitWithTimeout*[T](operation: Future[T],
|
|
|
|
# timeout: Duration,
|
|
|
|
# onTimeout: untyped): T =
|
|
|
|
# awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
# onTimeout
|
2023-09-03 00:53:48 +00:00
|
|
|
|
2023-10-13 04:24:35 +00:00
|
|
|
# template awaitWithTimeout*(operation: Future[void],
|
|
|
|
# timeout: Duration,
|
|
|
|
# onTimeout: untyped) =
|
|
|
|
# awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
|