concurrency
This commit is contained in:
20
src/main.rs
20
src/main.rs
@@ -1,7 +1,9 @@
|
||||
use color_eyre::Report;
|
||||
use reqwest::Client;
|
||||
use tokio::time::sleep;
|
||||
use tracing::info;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
pub const URL_1: &str = "https://fasterthanli.me/articles/whats-in-the-box";
|
||||
@@ -12,12 +14,14 @@ async fn main() -> Result<(), Report> {
|
||||
setup()?;
|
||||
|
||||
let client = Client::new();
|
||||
let leaked_client = Box::leak(Box::new(client));
|
||||
|
||||
let fut1 = fetch_thing(&client, URL_1);
|
||||
let fut2 = fetch_thing(&client, URL_2);
|
||||
let fut1 = fetch_thing(leaked_client, URL_1);
|
||||
tokio::spawn(fut1);
|
||||
let fut2 = fetch_thing(leaked_client, URL_2);
|
||||
tokio::spawn(fut2);
|
||||
|
||||
fut1.await?;
|
||||
fut2.await?;
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -45,10 +49,10 @@ fn setup() -> Result<(), Report> {
|
||||
// }
|
||||
//
|
||||
use std::future::Future;
|
||||
fn fetch_thing<'a> (
|
||||
client: &'a Client,
|
||||
url: &'a str,
|
||||
) -> impl Future<Output = Result<(), Report>> + 'a {
|
||||
fn fetch_thing (
|
||||
client: &'static Client,
|
||||
url: &'static str,
|
||||
) -> impl Future<Output = Result<(), Report>> + 'static {
|
||||
async move {
|
||||
let res = client.get(url).send().await?.error_for_status()?;
|
||||
info!(%url, content_type = ?res.headers().get("content-type"), "Got a response!");
|
||||
|
||||
Reference in New Issue
Block a user