Coroutines and cancellation support
See original GitHub issueFeature - Coroutines and cancellation.
Currently Ninjato’s http api call, is a blocking method, as we have it below. https://github.com/agoda-com/ninjato/blob/ae5b8017dfefb137ba57a74b56644314ac4f44f3/clients/client-okhttp/src/main/kotlin/com/agoda/ninjato/client/NinjatoOkHttpClient.kt#L26
Type
Since coroutines and suspend functions are getting more popular and recommended method in handling threads in Android, wrapping the existing Ninjato’s method inside a suspend function makes it non-cancellable. When we cancel the job, it should be relayed to the http layer and we need to remove it from client’s queue if it’s not already taken.
Proposed Solution
We need to enqueue the request on the client, and if the outer scope is canceled, we need to remove the call from the queue. By doing this instead of just ignoring the result, we are completely stopping it from executing, and it results in great performance improvement when we have to do some polling and or make some repeated http calls.
val call = client.newCall(request).also {
it.enqueue(callback)
}
ctn.invokeOnCancellation {
call.cancel()
}
We need to support both blocking and non-blocking api function, and let the user choose which one they want to use. So we need to keep the existing classes untouched and do this as an optional feature.
Things to do
Update Kotlin version - 1.4.21 (at least) Add coroutines support - 1.4.2 (at least)
Working solution
I already have a working solution in which we see a lot of performance improvements. If we think we need this feature, I can take this up and create a PR.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (3 by maintainers)
@kartykx thanks for your invaluable contribution! Based on what I observed from your PRs I managed to come up with #22 which adds suspended capabilities as new set of APIs on top of current with minimal impact on current library users. Is this something that can satisfy your needs? WDYT?
Unfortunately, no. And there might be troubles publishing the update, but I’ll try to solve it.