New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some Async Coroutine Functions #431
Conversation
I'm personally not a fan of the double await. asyncObjectResponse().await() would work, and allows you to have this be additive and not breaking. |
@SleeplessByte Yeah, I see what you mean. |
Added Async coroutine functions
import kotlinx.coroutines.experimental.CommonPool | ||
import kotlinx.coroutines.experimental.suspendCancellableCoroutine | ||
import kotlinx.coroutines.experimental.withContext | ||
import kotlinx.coroutines.experimental.* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you mind reverting this change as we try not to use wildcard imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I will.
await(byteArrayDeserializer(), scope) | ||
): Triple<Request, Response, Result<ByteArray, FuelError>> = await(byteArrayDeserializer(), scope) | ||
|
||
suspend fun Request.asyncByteArrayResponse( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason for changing the name of this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like await on next line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry I see that
@@ -23,7 +23,7 @@ class CoroutinesTest { | |||
@Test | |||
fun testAwaitResponseSuccess() = runBlocking { | |||
try { | |||
Fuel.get("/ip").awaitByteArrayResponse().third.fold({ data -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to keep awaitXX
version for our user to use? Are we gonna deprecated them and move user to use the asyncXX
version instead? I think either way we probably want to keep the tests for awaitXXX
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah definitely keep the tests until the await versions are removed, if ever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh Okay, I will restore awaitXX versions of tests.
I think this looks great. It almost like we should deprecate the All in all, after this PR is merged our Coroutine API landscape will look like this.
It looks a bit wild. I think it might be "good" to provide the same treatment (provide the Deferred<> version) with the rest of the available API. 😅 But this means that we are going to double the available API for our users. .... 🙀 I almost feel like, at this point, we might wanna clean this all up at once. Might it be just wiser to return the What do you think? |
@kittinunf I would love to deprecated Result and Triple and only uses Deferred. |
I concur with Jonathan: Deprecate, don't remove, but remove from
documentation.
…On Sat, 15 Sep 2018, 20:10 Jonathan, ***@***.***> wrote:
@kittinunf <https://github.com/kittinunf> I would love to deprecated
Result and Triple and only uses Deferred.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#431 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AB35WLP0QfL5BOB8kkKpOT4-gUWNFs-9ks5ubUKzgaJpZM4Woevr>
.
|
Just to confirm, I guess we want to go with just 2 versions!
And the rest, we shall deprecate them? |
@kittinunf async {
asyncByteArrayResponse().await().third
} You don't need to have async on that line. |
Hmm, in my experience, @iNoles If you don't mind, could you help me out? |
@kittinunf Sure fun awaitByteArrayResult(): Result<ByteArray, FuelError> = asyncByteArrayResponse().await().third |
Hmm, I think it is supposed to be This is for the parity of the APIs so that Do you think it makes sense? |
The await version is not deferred because doing an . await blocks.
So the async one is deferred. Seems correct?
…On Mon, 17 Sep 2018, 10:07 Kittinun Vantasin, ***@***.***> wrote:
Hmm, I think it is supposed to be Deferred<Result<T, FuelError>> ...
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#431 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AB35WCg-QABUM1dCutdxr69r30NmNcuQks5ub1hDgaJpZM4Woevr>
.
|
@SleeplessByte Deferred's await() will wait for Deferred to complete. @kittinunf I have a concern about it over asyncXXXResult because it is adding more complexity to the developers. It is going to be like asyncXXXResponse().await().third.await(). It going to be Confusing for double await(). |
@iNoles exactly what I thought :) And I agree with the double await being confusing. |
Using a The main problem described in #429 is that the coroutine still blocks instead of suspending the thread when it's waiting on I/O. This doesn't actually fix that problem. |
Oh, what does solve this problem? |
@clarcharr @SleeplessByte I am sure that I may well have this wrong, however when I first looked at implementing this I can remember that I felt the best approach to make this a 'true' suspending implementation one would have to change (or implement similar) line 53, Deserializable.kt private fun <T : Any, U : Deserializable<T>> Request.response(deserializable: U,
success: (Request, Response, T) -> Unit,
failure: (Request, Response, FuelError) -> Unit): Request {
... as this (if I understand this correctly) is the part that makes the request. I think that one would have to add a suspending function here that executes the task in order to achieve what you are looking for |
Ahhhh sorry, I understand it now. Then, the current API proposal is OK to me. @clarcharr I think we need you to perhaps shed some light here. I think the last thing we want is merging this and it doesn't solve your problem. |
@kittinunf Do you want me to restore
because 80% of the time? |
I feel like if I am gonna use the coroutine version, I think I might prefer to use |
I'm not going to be able to get to this today, but I'll try this weekend to draft up the sort of changes that would be needed to fix the original issue. Part of it can be done without really affecting the surface API, but another part can't. Basically, I think that establishing a connection to the server and getting back the response can be done asynchronously, where the only difference from the existing implementation is that the current thread isn't blocked from performing other work while the connection happens. Making the read of the response body asynchronous would require changing the deserialization API, which would be breaking. But as far as things go, a majority of the time for the request is in getting back the response headers, which means that there's still a lot to gain without breaking the API. |
@SleeplessByte @iNoles @clarcharr I would really value your critique on my pr #438 |
Just a note about returning a |
Codecov Report
@@ Coverage Diff @@
## master #431 +/- ##
========================================
- Coverage 77.8% 76.8% -1%
Complexity 164 164
========================================
Files 33 33
Lines 811 815 +4
Branches 129 135 +6
========================================
- Hits 631 626 -5
- Misses 114 129 +15
+ Partials 66 60 -6
Continue to review full report at Codecov.
|
I am going to close this PR because async-await is BEST for Multiple coroutines with concurrency. Since 80% of the cases, it going to be single operation that is best for withContext. |
Thank you for submitting your Pull Request. Please make sure you have
familiarised yourself with the Contributing Guidelines
before continuing.
Description
I added async block for some functions and left blocking one alone. I was a bit curious why somebody else didn't added this one.
Fixes #429
Type of change
Check all that apply
How Has This Been Tested?
In case you did not include tests describe why you and how you have verified the
changes, with instructions so we can reproduce. If you have added comprehensive
tests for your changes, you may ommit this section.
Checklist: