-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
Stable futures API #27
Conversation
c47ee1e
to
f8983a1
Compare
I just cannot wrap my head around this idea. Why would anyone want an always async http client? Most of the time waiting for the response actually makes sense. In general what is wrong with opening threads for async requests? |
It's primarily an efficiency thing. If you have a service that may need to make a request from more than one thread, or potentially parallel requests, you have three options:
I've benchmarked these approaches; chttp is actually 30% faster than using a vanilla curl request, because of its asynchronous design that takes advantage of curl's built-in (but optional) async features. It's more work for me to implement, but worth it in my book. This particular PR will not make any major changes to the API; the default will be a synchronous API. If you want to make requests asynchronously (which a number of people do), then it will be offered also. In fact chttp's core is already asynchronous and powered by futures. The fact that no one notices this is a success in my book. It means I already successfully abstracted away the complexity involved. All this PR does is change the guts from using The nice thing about an asynchronous core is that it benefits both normal synchronous users and asynchronous users. |
Indeed it does. But waiting does not always imply "blocking a thread". There are ways of waiting for something without blocking a thread. And that's valuable because threads are expensive and you want to use them as efficiently as possible.
I'm not sure exactly what you mean by this. Async and threads are not the same thing, if that's what you meant. In general, spawning new threads is really expensive, and you want to avoid doing it if you can. That's why for decades people have used things like thread pools or event loops, in order to conserve, control, and re-use threads efficiently without incurring a high cost. |
All this being said, I understand that this is a lot of complex talk, when there's a ton of users who don't care and really just want to make a few HTTP requests? Why does that have to be so difficult in Rust!? I sympathize, and that's exactly why I created chttp in the first place, and my vision has not changed. Making a My goal is to make the easy thing also the fast and efficient thing. |
Thanks for you answers I guess I need to understand the futures deeper. Right now when people say async I remember the horribleness of writing javascript code and callbacks and stuff and to be honest that's really not how I want to write rust I just love being able to block when I need it. Again thanks for your detailed explanation and this amazing library. I guess I need to understand the futures and how to use them effectively first |
While futures and async/await in Rust seek to accomplish similar goals to promises and async/await in JavaScript, it is designed quite differently (and admittedly has a bit of essential complexity). Either way, you don't have to use futures in order to use chttp if you don't want to. If you want to learn more about async in Rust, I'd wait until async/await syntax is stabilized in a month or so. It will be a lot easier then. I'm glad to help, and am glad if my HTTP client is useful to you. |
Things are almost finished! Only a few things remain: - Invoke complete/fail on the handler on completion from the agent - Configure easy handle inside client before executing
- Tweak log levels - Make `Client::new()` simply panic if it fails to init - Update sluice to get a bugfix for response body - Update benchmarks - Change handler drop behavior to allow futures to be canceled as intended
After more than a month of work, this PR is just about ready! |
Re-implement the core of chttp using
std
futures / futures 0.3 and async/await, and provide an API that can be used with async/await as well.We may want to put this behind a compile-time feature somehow if possible...
Body
to be async.Waker
s for waking the agent thread.