I am trying to spawn several tokio tasks that are supposed to be querying various web APIs. My plan was to use reqwest::Client for that.
According to reqwest::Client docs
The
Clientholds a connection pool internally, so it is advised that you create one and reuse it.
You do not have to wrap theClientin anRcorArcto reuse it, because it already uses anArcinternally.
Trying to just grab the reference results in the usual, expected complaint about 'static
21 | let webapi1 = tokio::spawn(async {do_stuff(&webapi_client).await});
| ^^^^^^^^^^^-------------^^^^^^^^
| | |
| | `webapi_client` is borrowed here
| may outlive borrowed value `webapi_client`
Therefore my question is if I should
- Wrap the
ClientinArcnonetheless, e.g.
use std::sync::Arc;
async fn do_stuff(client : Arc<reqwest::Client>) {
client.get("www.google.com").send().await;
}
#[tokio::main]
async fn main() -> tokio::io::Result<()> {
let webapi_shared = Arc::new(reqwest::Client::new());
let webapi_client = &*webapi_shared;
let webapi_for_f1 = webapi_shared.clone();
let webapi_for_f2 = webapi_shared.clone();
let webapi1 = tokio::spawn(async move{do_stuff(webapi_for_f1).await});
let webapi2 = tokio::spawn(async move{do_stuff(webapi_for_f2).await});
webapi_client.get("www.google.com").send().await;
webapi1.await;
webapi2.await;
Ok(())
}
- Just clone the client:
use std::sync::Arc;
async fn do_stuff(client : reqwest::Client) {
client.get("www.google.com").send().await;
}
#[tokio::main]
async fn main() -> tokio::io::Result<()> {
let webapi_client = reqwest::Client::new();
let webapi_for_f1 = webapi_client.clone();
let webapi_for_f2 = webapi_client.clone();
let webapi1 = tokio::spawn(async move{do_stuff(webapi_for_f1).await});
let webapi2 = tokio::spawn(async move{do_stuff(webapi_for_f2).await});
webapi_client.get("www.google.com").send().await;
webapi1.await;
webapi2.await;
Ok(())
}
…but I didn’t notice .clone() returning shallow copies 🙂
- Mitigate the lifetime issue some other way and just grab the reference? (any hints also welcome here)
>Solution :
…but I didn’t notice
.clone()returning shallow copies 🙂
But that’s exactly what the docs you cited say:
You do not have to wrap the Client in an Rc or Arc to reuse it, because it already uses an
Arcinternally.