From fa47217a63464b8b3531fe3c39c3f632365e3a17 Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 7 Jun 2024 02:18:11 +0200 Subject: [PATCH] fetch, send: run signature in spawn_blocking --- src/fetch.rs | 14 ++++++++++---- src/send.rs | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/fetch.rs b/src/fetch.rs index 9cfb74a..cafe6d3 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -26,11 +26,17 @@ where .header("accept", "application/activity+json") .header("digest", digest_header) .body(vec![])?; - SigningConfig::new(RsaSha256, private_key, key_id) - .sign(&mut req)?; + let private_key = private_key.clone(); + let key_id = key_id.to_string(); + let req = spawn_blocking(move || { + SigningConfig::new(RsaSha256, &private_key, &key_id).sign(&mut req)?; + Ok(req) + }) + .await + .map_err(|e| Error::Response(format!("{e}")))? + .map_err(|e: sigh::Error| Error::Response(format!("{e}")))?; let req: reqwest::Request = req.try_into()?; - let res = client.execute(req) - .await?; + let res = client.execute(req).await?; if res.status() >= StatusCode::OK && res.status() < StatusCode::MULTIPLE_CHOICES { Ok(res.json().await?) } else { diff --git a/src/send.rs b/src/send.rs index 58a0d5c..705ab1a 100644 --- a/src/send.rs +++ b/src/send.rs @@ -42,12 +42,18 @@ pub async fn send_raw( .header("digest", digest_header) .body(body.as_ref().clone())?; let t1 = Instant::now(); - SigningConfig::new(RsaSha256, private_key, key_id) - .sign(&mut req)?; + let private_key = private_key.clone(); + let key_id = key_id.to_string(); + let req = spawn_blocking(move || { + SigningConfig::new(RsaSha256, &private_key, &key_id).sign(&mut req)?; + Ok(req) + }) + .await + .map_err(|e| Error::Response(format!("{e}")))? + .map_err(|e: sigh::Error| Error::Response(format!("{e}")))?; let t2 = Instant::now(); let req: reqwest::Request = req.try_into()?; - let res = client.execute(req) - .await?; + let res = client.execute(req).await?; let t3 = Instant::now(); histogram!("relay_http_request_duration") .record(t2 - t1);