diff --git a/CHANGELOG.md b/CHANGELOG.md index de314b9e..df7324a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Add NOT NULL constraint to email_blocklist table fields (#1016) - Don't fill empty content when switching rich editor (#1017) - Fix accept header (#1058) +- Reuse reqwest client on broadcasting (#1059) ## [[0.7.1]] - 2022-01-12 diff --git a/plume-common/src/activity_pub/mod.rs b/plume-common/src/activity_pub/mod.rs index e04106ce..bd640f10 100644 --- a/plume-common/src/activity_pub/mod.rs +++ b/plume-common/src/activity_pub/mod.rs @@ -132,6 +132,14 @@ where .sign(sender) .expect("activity_pub::broadcast: signature error"); + let client = if let Some(proxy) = proxy { + ClientBuilder::new().proxy(proxy) + } else { + ClientBuilder::new() + } + .connect_timeout(std::time::Duration::from_secs(5)) + .build() + .expect("Can't build client"); let mut rt = tokio::runtime::current_thread::Runtime::new() .expect("Error while initializing tokio runtime for federation"); for inbox in boxes { @@ -155,33 +163,26 @@ where headers.insert("Host", host_header_value.unwrap()); headers.insert("Digest", request::Digest::digest(&body)); rt.spawn( - if let Some(proxy) = proxy.clone() { - ClientBuilder::new().proxy(proxy) - } else { - ClientBuilder::new() - } - .connect_timeout(std::time::Duration::from_secs(5)) - .build() - .expect("Can't build client") - .post(&inbox) - .headers(headers.clone()) - .header( - "Signature", - request::signature(sender, &headers, ("post", url.path(), url.query())) - .expect("activity_pub::broadcast: request signature error"), - ) - .body(body) - .send() - .and_then(move |r| { - if r.status().is_success() { - debug!("Successfully sent activity to inbox ({})", &inbox); - } else { - warn!("Error while sending to inbox ({:?})", &r) - } - r.into_body().concat2() - }) - .map(move |response| debug!("Response: \"{:?}\"\n", response)) - .map_err(|e| warn!("Error while sending to inbox ({:?})", e)), + client + .post(&inbox) + .headers(headers.clone()) + .header( + "Signature", + request::signature(sender, &headers, ("post", url.path(), url.query())) + .expect("activity_pub::broadcast: request signature error"), + ) + .body(body) + .send() + .and_then(move |r| { + if r.status().is_success() { + debug!("Successfully sent activity to inbox ({})", &inbox); + } else { + warn!("Error while sending to inbox ({:?})", &r) + } + r.into_body().concat2() + }) + .map(move |response| debug!("Response: \"{:?}\"\n", response)) + .map_err(|e| warn!("Error while sending to inbox ({:?})", e)), ); } rt.run().unwrap();