From 33a0c7dcd3c4377db338ed380ab33c88caeed4bf Mon Sep 17 00:00:00 2001 From: fdb-hiroshima <35889323+fdb-hiroshima@users.noreply.github.com> Date: Tue, 30 Apr 2019 23:30:13 +0200 Subject: [PATCH] reduce reqwest timeout to 5s (#557) --- plume-common/src/activity_pub/inbox.rs | 5 ++++- plume-common/src/activity_pub/mod.rs | 27 +++++++++++++++----------- plume-models/src/users.rs | 14 +++++++++---- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/plume-common/src/activity_pub/inbox.rs b/plume-common/src/activity_pub/inbox.rs index 9dcc8dd..9c71461 100644 --- a/plume-common/src/activity_pub/inbox.rs +++ b/plume-common/src/activity_pub/inbox.rs @@ -279,7 +279,10 @@ pub trait FromId: Sized { /// Dereferences an ID fn deref(id: &str) -> Result, Self::Error)> { - reqwest::Client::new() + reqwest::ClientBuilder::new() + .connect_timeout(Some(std::time::Duration::from_secs(5))) + .build() + .map_err(|_| (None, InboxError::DerefError.into()))? .get(id) .header( ACCEPT, diff --git a/plume-common/src/activity_pub/mod.rs b/plume-common/src/activity_pub/mod.rs index 520f502..c08bf06 100644 --- a/plume-common/src/activity_pub/mod.rs +++ b/plume-common/src/activity_pub/mod.rs @@ -1,6 +1,6 @@ use activitypub::{Activity, Link, Object}; use array_tool::vec::Uniq; -use reqwest::Client; +use reqwest::ClientBuilder; use rocket::{ http::Status, request::{FromRequest, Request}, @@ -134,16 +134,21 @@ where let body = signed.to_string(); let mut headers = request::headers(); headers.insert("Digest", request::Digest::digest(&body)); - let res = Client::new() - .post(&inbox) - .headers(headers.clone()) - .header( - "Signature", - request::signature(sender, &headers) - .expect("activity_pub::broadcast: request signature error"), - ) - .body(body) - .send(); + let res = ClientBuilder::new() + .connect_timeout(Some(std::time::Duration::from_secs(5))) + .build() + .and_then(|client| { + client + .post(&inbox) + .headers(headers.clone()) + .header( + "Signature", + request::signature(sender, &headers) + .expect("activity_pub::broadcast: request signature error"), + ) + .body(body) + .send() + }); match res { Ok(mut r) => { println!("Successfully sent activity to inbox ({})", inbox); diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 098da31..75305de 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -23,7 +23,7 @@ use plume_common::activity_pub::{ use plume_common::utils; use reqwest::{ header::{HeaderValue, ACCEPT}, - Client, + ClientBuilder, }; use rocket::{ outcome::IntoOutcome, @@ -267,7 +267,9 @@ impl User { } fn fetch(url: &str) -> Result { - let mut res = Client::new() + let mut res = ClientBuilder::new() + .connect_timeout(Some(std::time::Duration::from_secs(5))) + .build()? .get(url) .header( ACCEPT, @@ -369,7 +371,9 @@ impl User { } pub fn fetch_outbox(&self) -> Result> { - let mut res = Client::new() + let mut res = ClientBuilder::new() + .connect_timeout(Some(std::time::Duration::from_secs(5))) + .build()? .get(&self.outbox_url[..]) .header( ACCEPT, @@ -392,7 +396,9 @@ impl User { } pub fn fetch_followers_ids(&self) -> Result> { - let mut res = Client::new() + let mut res = ClientBuilder::new() + .connect_timeout(Some(std::time::Duration::from_secs(5))) + .build()? .get(&self.followers_endpoint[..]) .header( ACCEPT,