Use spawn blocking instead of block in place

This commit is contained in:
asonix 2020-09-29 20:03:12 -05:00
parent 3c73552ff2
commit 88a7ecce0d
3 changed files with 11 additions and 3 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "http-signature-normalization-reqwest" name = "http-signature-normalization-reqwest"
description = "An HTTP Signatures library that leaves the signing to you" description = "An HTTP Signatures library that leaves the signing to you"
version = "0.1.1" version = "0.1.2"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
license-file = "LICENSE" license-file = "LICENSE"
readme = "README.md" readme = "README.md"
@ -31,7 +31,7 @@ reqwest = "0.10.8"
sha2 = { version = "0.9", optional = true } sha2 = { version = "0.9", optional = true }
sha3 = { version = "0.9", optional = true } sha3 = { version = "0.9", optional = true }
thiserror = "1.0" thiserror = "1.0"
tokio = { version = "0.2", default-features = false, features = ["rt-threaded", "blocking"], optional = true } tokio = { version = "0.2", default-features = false, features = ["blocking"], optional = true }
[dev-dependencies] [dev-dependencies]
pretty_env_logger = "0.4" pretty_env_logger = "0.4"

View file

@ -73,7 +73,12 @@ impl SignExt for RequestBuilder {
Self: Sized, Self: Sized,
{ {
Box::pin(async move { Box::pin(async move {
let digest = tokio::task::block_in_place(|| digest.compute(v.as_ref())); let (v, digest) = tokio::task::spawn_blocking(move || {
let digest = digest.compute(v.as_ref());
(v, digest)
})
.await
.map_err(|_| SignError::Canceled)?;
let c = self let c = self
.header("Digest", format!("{}={}", D::NAME, digest)) .header("Digest", format!("{}={}", D::NAME, digest))

View file

@ -70,6 +70,9 @@ pub enum SignError {
#[error("Cannot sign request with body already present")] #[error("Cannot sign request with body already present")]
BodyPresent, BodyPresent,
#[error("Panic in spawn blocking")]
Canceled,
} }
impl Config { impl Config {