question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Coroutines and cancellation support

See original GitHub issue

Feature - 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:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Unlimitycommented, Jun 11, 2021

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

1reaction
Unlimitycommented, Jun 9, 2021

Do we have a slack channel for this repo?

Unfortunately, no. And there might be troubles publishing the update, but I’ll try to solve it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cancellation in coroutines
To be able to call suspend functions when a coroutine is cancelled, we will need to switch the cleanup work we need to...
Read more >
Cancellation in Kotlin Coroutines
Once a job is cancelled, it cannot be used as a parent for any new coroutines. It is first in the "Cancelling" and...
Read more >
Cancellation and timeouts | Kotlin
This section covers coroutine cancellation and timeouts. ... All the suspending functions in kotlinx.coroutines are cancellable.
Read more >
Cancelling Kotlin Coroutines
When calling cancel on a coroutine job, the isActive flag is set to false, but the job will continue to run. This means...
Read more >
Things you should know about Coroutines. Part 5: Cancellation.
In this article, we will discuss everything you need to know about cancellation in coroutines.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found