Merge pull request 'Reuse reqwest client on broadcasting' (#1059) from fix-timeout into main

Reviewed-on: https://git.joinplu.me/Plume/Plume/pulls/1059
This commit is contained in:
KitaitiMakoto 2022-05-03 19:59:05 +00:00
commit 812fd3d956
2 changed files with 29 additions and 27 deletions

View File

@ -18,6 +18,7 @@
- Add NOT NULL constraint to email_blocklist table fields (#1016) - Add NOT NULL constraint to email_blocklist table fields (#1016)
- Don't fill empty content when switching rich editor (#1017) - Don't fill empty content when switching rich editor (#1017)
- Fix accept header (#1058) - Fix accept header (#1058)
- Reuse reqwest client on broadcasting (#1059)
## [[0.7.1]] - 2022-01-12 ## [[0.7.1]] - 2022-01-12

View File

@ -132,6 +132,14 @@ where
.sign(sender) .sign(sender)
.expect("activity_pub::broadcast: signature error"); .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() let mut rt = tokio::runtime::current_thread::Runtime::new()
.expect("Error while initializing tokio runtime for federation"); .expect("Error while initializing tokio runtime for federation");
for inbox in boxes { for inbox in boxes {
@ -155,33 +163,26 @@ where
headers.insert("Host", host_header_value.unwrap()); headers.insert("Host", host_header_value.unwrap());
headers.insert("Digest", request::Digest::digest(&body)); headers.insert("Digest", request::Digest::digest(&body));
rt.spawn( rt.spawn(
if let Some(proxy) = proxy.clone() { client
ClientBuilder::new().proxy(proxy) .post(&inbox)
} else { .headers(headers.clone())
ClientBuilder::new() .header(
} "Signature",
.connect_timeout(std::time::Duration::from_secs(5)) request::signature(sender, &headers, ("post", url.path(), url.query()))
.build() .expect("activity_pub::broadcast: request signature error"),
.expect("Can't build client") )
.post(&inbox) .body(body)
.headers(headers.clone()) .send()
.header( .and_then(move |r| {
"Signature", if r.status().is_success() {
request::signature(sender, &headers, ("post", url.path(), url.query())) debug!("Successfully sent activity to inbox ({})", &inbox);
.expect("activity_pub::broadcast: request signature error"), } else {
) warn!("Error while sending to inbox ({:?})", &r)
.body(body) }
.send() r.into_body().concat2()
.and_then(move |r| { })
if r.status().is_success() { .map(move |response| debug!("Response: \"{:?}\"\n", response))
debug!("Successfully sent activity to inbox ({})", &inbox); .map_err(|e| warn!("Error while sending to inbox ({:?})", e)),
} 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(); rt.run().unwrap();