wait for cancellation to be processed on timeout (#311)

This commit is contained in:
Jacek Sieka 2020-11-17 15:36:41 +01:00 committed by GitHub
parent bc8a8947fc
commit 114680453f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
import
chronos, chronicles, chronicles/chronos_tools
chronos, chronicles/chronos_tools
export
chronos_tools
@ -10,7 +10,10 @@ template awaitWithTimeout*[T](operation: Future[T],
let f = operation
await f or deadline
if not f.finished:
cancel f
# 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
@ -19,4 +22,3 @@ template awaitWithTimeout*[T](operation: Future[T],
timeout: Duration,
onTimeout: untyped): T =
awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)