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:
Jacek Sieka 2024-01-16 18:12:47 +01:00 committed by GitHub
parent e52f5fac0a
commit e5e695c396
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 2 deletions

View File

@ -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)