Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Don't use reqwest::blocking #60

Open
CGenie opened this issue Mar 16, 2020 · 3 comments
Open

Don't use reqwest::blocking #60

CGenie opened this issue Mar 16, 2020 · 3 comments

Comments

@CGenie
Copy link

CGenie commented Mar 16, 2020

I want to create an API service, and I see reqwest::blocking is used here. Is it possible to rewrite to use async/await instead?

@jonas-schievink
Copy link
Owner

Sure, feel free to send a PR as long as it doesn't introduce any hard dependencies on any particular executor.

@Flowneee
Copy link

Hi. I am currently interested in implementing this feature, but after quick look I think that this require major changes, because existing code is heavily rely on sync functions. What should be done:

  • _async version of Request::call and Request::call_url (this require some async transport);
  • AsyncTransport and implementation for http.

The major problem is Parser, which rely on std::io::Read: we cannot reuse it as-is for async streams (i.e. in AsyncTransport). I see here multiple approaches:

  • AsyncTransport should read entire body as string and then pass it to Parser;
  • OR make Parser async:
    • write AsyncParser, which will be almost exact clone, but all methods are async (since Parser::next should be async);
    • OR change all methods of Parser to async and:
      • (reqwest-like approach) spawn separate worker and tokio runtime there, send all async tasks there and wait for task completion in sync functions;
      • OR on each async task spawn "current thread" runtime and block on this task.

I think first option is easiest to implement, but it require more memory in runtime.

Any thoughts?

@ict-cloud
Copy link

ict-cloud commented Mar 16, 2022

I just came across the same limitation as @CGenie mentioned in the original post, but I do only have the restriction that the reqwest::blocking cannot be used within a tokio runtime where blocking is not allowed (i.e. use it within a yew_hook::use_async).
Thus probably the solution to just make the transport async, make this a feature and leave the remaining part as it is, may be a first approach.

Edit after first tries:
It seems like @Flowneee is right about his outline and the crucial part will be to overcome the Parser, which requires std::io::Read compatibility.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants