Consider the following scenario: - High-latency HT...
# coroutines
f
Consider the following scenario: - High-latency HTTP requests, done using the Apache Async Client - Coroutine that "launches" multiple requests using
async
, obtaining
Deferred
objects - Coroutine then cancels one of those
Deferred
objects. 1) How can such a
cancel
trigger a cancellation on the pending HTTP request? The Apache Async Client has a way to cancel requests, however since there isn't any thread actively monitoring the
isActive
, I need to "link" the
Deferred
cancel with the pending request cancel. For instance, in .NET the
CancellationToken
as a
Register
method to register an action to be performed. 2) Also, if the coroutine decides to ignore the cancellation (e.g. it is to late to cancel the HTTP request), how can we observe that on the
Deferred
? What I'm observing is that the
await
will throw the
JobCancellationException
even if the
cancel
is ignored.
f
Thanks.
Regarding 2): when cancelling a
Deferred
, apparently it will transition immediately to a cancelled state, even if the cancellation is ignored by the underlying coroutine, right?
Or is there a way to make the
Deferred
state depend on the coroutine reaction to the cancellation request?
e
It will become Cancelling. It will become Cancelled only when the underlying coroutine completes. More in that here: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/index.html