Remove async_recursion for a for loop

This commit is contained in:
Luke Parker
2023-11-07 23:05:09 -05:00
parent e1c07d89e0
commit bc07e14b1e
3 changed files with 119 additions and 134 deletions

12
Cargo.lock generated
View File

@@ -309,17 +309,6 @@ dependencies = [
"event-listener", "event-listener",
] ]
[[package]]
name = "async-recursion"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fd55a5ba1179988837d24ab4c7cc8ed6efdeff578ede0416b4225a5fca35bd0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.39",
]
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.74" version = "0.1.74"
@@ -4851,7 +4840,6 @@ dependencies = [
name = "monero-serai" name = "monero-serai"
version = "0.1.4-alpha" version = "0.1.4-alpha"
dependencies = [ dependencies = [
"async-recursion",
"async-trait", "async-trait",
"base58-monero", "base58-monero",
"curve25519-dalek", "curve25519-dalek",

View File

@@ -54,7 +54,6 @@ serde_json = { version = "1", default-features = false, features = ["alloc"] }
base58-monero = { version = "2", default-features = false, features = ["check"] } base58-monero = { version = "2", default-features = false, features = ["check"] }
# Used for the provided HTTP RPC # Used for the provided HTTP RPC
async-recursion = { version = "1", optional = true }
digest_auth = { version = "0.3", default-features = false, optional = true } digest_auth = { version = "0.3", default-features = false, optional = true }
simple-request = { path = "../../common/request", version = "0.1", default-features = false, optional = true } simple-request = { path = "../../common/request", version = "0.1", default-features = false, optional = true }
tokio = { version = "1", default-features = false, optional = true } tokio = { version = "1", default-features = false, optional = true }
@@ -101,7 +100,7 @@ std = [
"base58-monero/std", "base58-monero/std",
] ]
http-rpc = ["async-recursion", "digest_auth", "simple-request", "tokio"] http-rpc = ["digest_auth", "simple-request", "tokio"]
multisig = ["transcript", "frost", "dleq", "std"] multisig = ["transcript", "frost", "dleq", "std"]
binaries = ["tokio/rt-multi-thread", "tokio/macros", "http-rpc"] binaries = ["tokio/rt-multi-thread", "tokio/macros", "http-rpc"]
experimental = [] experimental = []

View File

@@ -112,19 +112,14 @@ impl HttpRpc {
} }
impl HttpRpc { impl HttpRpc {
#[async_recursion::async_recursion] async fn inner_post(&self, route: &str, body: Vec<u8>) -> Result<Vec<u8>, RpcError> {
async fn inner_post(
&self,
route: &str,
body: Vec<u8>,
recursing: bool,
) -> Result<Vec<u8>, RpcError> {
let request_fn = |uri| { let request_fn = |uri| {
Request::post(uri) Request::post(uri)
.body(body.clone().into()) .body(body.clone().into())
.map_err(|e| RpcError::ConnectionError(format!("couldn't make request: {e:?}"))) .map_err(|e| RpcError::ConnectionError(format!("couldn't make request: {e:?}")))
}; };
for attempt in 0 .. 2 {
let response = match &self.authentication { let response = match &self.authentication {
Authentication::Unauthenticated(client) => client Authentication::Unauthenticated(client) => client
.request(request_fn(self.url.clone() + "/" + route)?) .request(request_fn(self.url.clone() + "/" + route)?)
@@ -186,11 +181,11 @@ impl HttpRpc {
connection_lock.0 = None; connection_lock.0 = None;
} }
// If we're not already recursing and: // If we're not already on our second attempt and:
// 1) We had a connection error // A) We had a connection error
// 2) We need to re-auth due to this token being stale // B) We need to re-auth due to this token being stale
// recursively re-call this function // Move to the next loop iteration (retrying all of this)
if (!recursing) && if (attempt == 0) &&
(response_result.is_err() || { (response_result.is_err() || {
let response = response_result.as_ref().unwrap(); let response = response_result.as_ref().unwrap();
if response.status() == StatusCode::UNAUTHORIZED { if response.status() == StatusCode::UNAUTHORIZED {
@@ -207,9 +202,9 @@ impl HttpRpc {
} }
}) })
{ {
// Drop the cached authentication before we do
connection_lock.0 = None; connection_lock.0 = None;
drop(connection_lock); continue;
return self.inner_post(route, body, true).await;
} }
response_result? response_result?
@@ -246,7 +241,10 @@ impl HttpRpc {
.read_to_end(&mut res) .read_to_end(&mut res)
.unwrap(); .unwrap();
Ok(res) return Ok(res);
}
unreachable!()
} }
} }
@@ -254,7 +252,7 @@ impl HttpRpc {
impl RpcConnection for HttpRpc { impl RpcConnection for HttpRpc {
async fn post(&self, route: &str, body: Vec<u8>) -> Result<Vec<u8>, RpcError> { async fn post(&self, route: &str, body: Vec<u8>) -> Result<Vec<u8>, RpcError> {
// TODO: Make this timeout configurable // TODO: Make this timeout configurable
tokio::time::timeout(core::time::Duration::from_secs(30), self.inner_post(route, body, false)) tokio::time::timeout(core::time::Duration::from_secs(30), self.inner_post(route, body))
.await .await
.map_err(|e| RpcError::ConnectionError(format!("{e:?}")))? .map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?
} }