From 24c9c7b5a1adbf1e5536f0dcfeff0b5da0fcd61f Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 9 Feb 2021 18:15:14 +0900 Subject: [PATCH] Build client for every inbox in broadcast() --- plume-common/src/activity_pub/mod.rs | 51 +++++++++++++++------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/plume-common/src/activity_pub/mod.rs b/plume-common/src/activity_pub/mod.rs index bf643d89..3e94207e 100644 --- a/plume-common/src/activity_pub/mod.rs +++ b/plume-common/src/activity_pub/mod.rs @@ -132,14 +132,6 @@ where let mut rt = tokio::runtime::current_thread::Runtime::new() .expect("Error while initializing tokio runtime for federation"); - 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"); for inbox in boxes { let body = signed.to_string(); let mut headers = request::headers(); @@ -161,22 +153,33 @@ where headers.insert("Host", host_header_value.unwrap()); headers.insert("Digest", request::Digest::digest(&body)); rt.spawn( - 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(|r| r.into_body().concat2()) - .map(move |response| { - debug!("Successfully sent activity to inbox ({})", inbox); - debug!("Response: \"{:?}\"\n", response) - }) - .map_err(|e| warn!("Error while sending to inbox ({:?})", e)), + 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)), ); } rt.run().unwrap();