fetch, send: run signature in spawn_blocking

This commit is contained in:
Astro 2024-06-07 02:18:11 +02:00
parent cd36a9ecb1
commit 1e42ab1ee3
2 changed files with 22 additions and 8 deletions

View file

@ -2,6 +2,7 @@ use std::time::SystemTime;
use http::StatusCode; use http::StatusCode;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use sigh::{PrivateKey, SigningConfig, alg::RsaSha256}; use sigh::{PrivateKey, SigningConfig, alg::RsaSha256};
use tokio::task::spawn_blocking;
use crate::{digest, error::Error}; use crate::{digest, error::Error};
pub async fn authorized_fetch<T>( pub async fn authorized_fetch<T>(
@ -26,11 +27,17 @@ where
.header("accept", "application/activity+json") .header("accept", "application/activity+json")
.header("digest", digest_header) .header("digest", digest_header)
.body(vec![])?; .body(vec![])?;
SigningConfig::new(RsaSha256, private_key, key_id) let private_key = private_key.clone();
.sign(&mut req)?; 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 req: reqwest::Request = req.try_into()?;
let res = client.execute(req) let res = client.execute(req).await?;
.await?;
if res.status() >= StatusCode::OK && res.status() < StatusCode::MULTIPLE_CHOICES { if res.status() >= StatusCode::OK && res.status() < StatusCode::MULTIPLE_CHOICES {
Ok(res.json().await?) Ok(res.json().await?)
} else { } else {

View file

@ -6,6 +6,7 @@ use http::StatusCode;
use metrics::histogram; use metrics::histogram;
use serde::Serialize; use serde::Serialize;
use sigh::{PrivateKey, SigningConfig, alg::RsaSha256}; use sigh::{PrivateKey, SigningConfig, alg::RsaSha256};
use tokio::task::spawn_blocking;
use crate::{digest, error::Error}; use crate::{digest, error::Error};
pub async fn send<T: Serialize>( pub async fn send<T: Serialize>(
@ -42,12 +43,18 @@ pub async fn send_raw(
.header("digest", digest_header) .header("digest", digest_header)
.body(body.as_ref().clone())?; .body(body.as_ref().clone())?;
let t1 = Instant::now(); let t1 = Instant::now();
SigningConfig::new(RsaSha256, private_key, key_id) let private_key = private_key.clone();
.sign(&mut req)?; 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 t2 = Instant::now();
let req: reqwest::Request = req.try_into()?; let req: reqwest::Request = req.try_into()?;
let res = client.execute(req) let res = client.execute(req).await?;
.await?;
let t3 = Instant::now(); let t3 = Instant::now();
histogram!("relay_http_request_duration") histogram!("relay_http_request_duration")
.record(t2 - t1); .record(t2 - t1);