mirror of https://github.com/status-im/nim-eth.git
better async timeout wait (#659)
* don't call timeout code if future finishes before getting cancelled * avoid extra raises effect resulting from `read` (vs `await`)
This commit is contained in:
parent
e52f5fac0a
commit
e5e695c396
|
@ -14,9 +14,11 @@ template awaitWithTimeout*[T](operation: Future[T],
|
|||
# the "next" operation will run concurrently to this one, messing up
|
||||
# the order of operations (since await/async is not fair)
|
||||
await cancelAndWait(f)
|
||||
|
||||
if f.cancelled: # It could have finished instead of getting cancelled
|
||||
onTimeout
|
||||
else:
|
||||
f.read
|
||||
await f # Await avoids extraneous exception effects
|
||||
|
||||
template awaitWithTimeout*[T](operation: Future[T],
|
||||
timeout: Duration,
|
||||
|
@ -33,10 +35,11 @@ template awaitWithTimeout*(operation: Future[void],
|
|||
# the "next" operation will run concurrently to this one, messing up
|
||||
# the order of operations (since await/async is not fair)
|
||||
await cancelAndWait(f)
|
||||
|
||||
if f.cancelled: # It could have finished instead of getting cancelled
|
||||
onTimeout
|
||||
|
||||
template awaitWithTimeout*(operation: Future[void],
|
||||
timeout: Duration,
|
||||
onTimeout: untyped) =
|
||||
awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
|
||||
|
||||
|
|
Loading…
Reference in New Issue