Replies: 18 comments
-
Can this even be used in actix-web handlers with futures-0.1.29? |
Beta Was this translation helpful? Give feedback.
-
It's complaining because your closure signature is incorrect. use futures::future::TryFutureExt;
use isahc::prelude::*;
fn main() -> Result<(), isahc::Error> {
let future = Request::get("http://example.org")
.body(())?
.send_async()
.map_ok(|response| {
println!("Status: {}", response.status());
println!("Headers:\n{:?}", response.headers());
});
futures::executor::block_on(future)
} You should then be able to integrate with actix-web by using the |
Beta Was this translation helpful? Give feedback.
-
How do I get the What's the difference between |
Beta Was this translation helpful? Give feedback.
-
Yes. That's why Note that there's some methods on I'm not sure how this ties into actix-web exactly as I've never used it, I'd have to see an example of what you are trying to do.
Ah, that. |
Beta Was this translation helpful? Give feedback.
-
I understand I must convert the final future from |
Beta Was this translation helpful? Give feedback.
-
Something generally like this might work (untested): use futures::future::{self, Either, Future};
// Will have to import futures 0.3 under the alias futures_preview since both 0.1 and 0.3
// are called "futures".
use futures_preview::compat::Compat;
fn handle_request(req: HttpRequest, client: web::Data<HttpClient>)
-> impl Future<Item = HttpResponse, Error = Error> {
let request = match Request::get("http://example.org").body(()) {
Ok(request) => request,
// error must be wrapped in a future
Err(e) => return Either::A(future::err(e.into())),
};
// Begin main future pipeline. Wrap 0.3 into a 0.1 future.
let future_03 = client
.get_ref()
.send_async();
let future_01 = Compat::new(future_03);
// Wrap in an Either as before, since `impl Future` requires us to return the same
// type everywhere.
Either::B(future_01
.map(|response| {
// Do something synchronous with response.
unimplemented!()
})
.and_then(|_| {
// Do somethng async iand return a HttpResponse.
unimplemented!()
}))
} Really the key part is converting the 0.3 future (really it is Since this is generally more a question about how to deal with futures 0.1 and futures 0.3 compatability, I think other channels might be the best place to get help on this, especially since others are more familiar than I am with both actix-web and the compatability layer. |
Beta Was this translation helpful? Give feedback.
-
Should I build the request for |
Beta Was this translation helpful? Give feedback.
-
I suspect it doesn't matter, but not knowing how actix-web works, I could be wrong. |
Beta Was this translation helpful? Give feedback.
-
In your example, I believe I had to wrap the
I annotated the handler with
|
Beta Was this translation helpful? Give feedback.
-
Yes, requests borrow from |
Beta Was this translation helpful? Give feedback.
-
Can't requests hold the client in an |
Beta Was this translation helpful? Give feedback.
-
It's not just incompatible with |
Beta Was this translation helpful? Give feedback.
-
They could, but that would add a performance overhead.
Not necessarily. Async/await definitely encourages using lifetimes instead of If you want to make a case for having |
Beta Was this translation helpful? Give feedback.
-
So simply using async / await instead of combinators would solve the problem? Can this not be replicated even with In my case I think making a static |
Beta Was this translation helpful? Give feedback.
-
Both |
Beta Was this translation helpful? Give feedback.
-
actix-web would probably have to be revised for this to work, since you intend to borrow the If your goal is 1 client per thread, then yeah using thread_local! {
static CLIENT: HttpClient = HttpClient::builder()
// customization
.build()
.unwrap();
} |
Beta Was this translation helpful? Give feedback.
-
If you do go down that direction, I'd be interested to hear back about some benchmarks on that approach vs a global client. Personally I'm skeptical that the performance increases will be significant enough, but I'm willing to be proved wrong. 😉 |
Beta Was this translation helpful? Give feedback.
-
The server is on a 10 Gb/s pipe and it's only job is to make outbound HTTP/HTTPS requests. I'll find out soon enough if a single core is enough to saturate the network, especially with TLS handshakes. It will probably run into other bottlenecks. |
Beta Was this translation helpful? Give feedback.
-
How do I use futures combinators on the *_async API without async / await? Can you provide an example.
Beta Was this translation helpful? Give feedback.
All reactions