I get a panic when I try to set a timeout for the connection.
Reproducible on the following code:
let pool = Arc::new(ThreadPool::new().expect("Failed to build pool"));
let client = reqwest::Client::builder().connect_timeout(Duration::from_millis(300)).build().unwrap();
pool.spawn_ok(async move {
if let Ok(resp) = client.get("https://google.com").send().await {
let headers = resp.headers();
println!("{:?}", headers);
}
});
...
You need to use reqwest on a Tokio runtime.
I also get panic with the code below, could you please give explanation @seanmonstar ?
Is it working only with #[tokio::main] macro?
let mut rt = Builder::new()
.threaded_scheduler()
.thread_name("async_worker")
.thread_stack_size(1024 * 512)
.build()
.unwrap();
rt.block_on(async {
let client = reqwest::Client::builder()
.connect_timeout(Duration::from_millis(300))
.build()
.unwrap();
if let Ok(resp) = client.get("https://google.com").send().await {
let headers = resp.headers();
println!("{:?}", headers);
}
});
You need to enable_time and enable_io if customizing a builder.
@seanmonstar Thanks!
I think it would be nice to update the documentation on this subject, right?
It's there in the docs: https://docs.rs/reqwest/0.10.1/reqwest/struct.ClientBuilder.html#method.connect_timeout
I mean in case someone will use reqwest with custom tokio runtime. This is not an obvious thing.
I can do PR if necessary.
The Tokio docs also describe this: https://docs.rs/tokio/0.2.9/tokio/runtime/index.html#resource-drivers
I think the best thing to do is to improve the panic messages in Tokio: https://github.com/tokio-rs/tokio/issues/1713
Most helpful comment
You need to
enable_timeandenable_ioif customizing a builder.