Skip to content

Commit

Permalink
Merge pull request #715 from MutinyWallet/timeout-all-requests
Browse files Browse the repository at this point in the history
Timeout all requests after 30s
  • Loading branch information
TonyGiorgio authored Aug 16, 2023
2 parents 48282e0 + b6edf07 commit 341b612
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 32 deletions.
10 changes: 6 additions & 4 deletions mutiny-core/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
lnurlauth::{make_lnurl_auth_connection, AuthManager},
logging::MutinyLogger,
networking::websocket::{SimpleWebSocket, WebSocketImpl},
utils,
};
use jwt_compact::UntrustedToken;
use lightning::util::logger::*;
Expand Down Expand Up @@ -101,10 +102,11 @@ impl MutinyAuthClient {
request = request.json(&json);
}

request
.send()
.await
.map_err(|_| MutinyError::ConnectionFailed)
utils::fetch_with_timeout(
&self.http_client,
request.build().expect("should build req"),
)
.await
}

async fn retrieve_new_jwt(&self) -> Result<String, MutinyError> {
Expand Down
16 changes: 8 additions & 8 deletions mutiny-core/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ struct MempoolFees {

impl<S: MutinyStorage> MutinyFeeEstimator<S> {
async fn get_mempool_recommended_fees(&self) -> anyhow::Result<HashMap<String, f64>> {
let fees = self
.esplora
.client()
.get(&format!("{}/v1/fees/recommended", self.esplora.url()))
.send()
let client = self.esplora.client();
let request = client
.get(format!("{}/v1/fees/recommended", self.esplora.url()))
.build()?;

let fees_response = utils::fetch_with_timeout(&client, request)
.await?
.error_for_status()?
.json::<MempoolFees>()
.await?;
.error_for_status()?;
let fees = fees_response.json::<MempoolFees>().await?;

// convert to hashmap of num blocks -> fee rate
let mut fee_estimates = HashMap::new();
Expand Down
9 changes: 5 additions & 4 deletions mutiny-core/src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,13 @@ async fn fetch_updated_gossip(
let http_client = Client::builder()
.build()
.map_err(|_| MutinyError::RapidGossipSyncError)?;
let rgs_response = http_client
.get(rgs_url)
.send()
.await

let request = http_client
.get(&rgs_url)
.build()
.map_err(|_| MutinyError::RapidGossipSyncError)?;

let rgs_response = utils::fetch_with_timeout(&http_client, request).await?;
let rgs_data = rgs_response
.bytes()
.await
Expand Down
30 changes: 18 additions & 12 deletions mutiny-core/src/lspclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bitcoin::secp256k1::PublicKey;
use reqwest::Client;
use serde::{Deserialize, Serialize};

use crate::error::MutinyError;
use crate::{error::MutinyError, utils};

#[derive(Clone, Debug)]
pub(crate) struct LspClient {
Expand Down Expand Up @@ -76,11 +76,13 @@ const FEE_PATH: &str = "/api/v1/fee";
impl LspClient {
pub async fn new(url: &str) -> Result<Self, MutinyError> {
let http_client = Client::new();
let get_info_response: GetInfoResponse = http_client
let request = http_client
.get(format!("{}{}", url, GET_INFO_PATH))
.send()
.await
.map_err(|_| MutinyError::LspGenericError)?
.build()
.map_err(|_| MutinyError::LspGenericError)?;
let response: reqwest::Response = utils::fetch_with_timeout(&http_client, request).await?;

let get_info_response: GetInfoResponse = response
.json()
.await
.map_err(|_| MutinyError::LspGenericError)?;
Expand Down Expand Up @@ -127,14 +129,15 @@ impl LspClient {
port: None,
};

let response: reqwest::Response = self
let request = self
.http_client
.post(format!("{}{}", &self.url, PROPOSAL_PATH))
.json(&payload)
.send()
.await
.build()
.map_err(|_| MutinyError::LspGenericError)?;

let response: reqwest::Response =
utils::fetch_with_timeout(&self.http_client, request).await?;
let status = response.status().as_u16();
if (200..300).contains(&status) {
let proposal_response: ProposalResponse = response
Expand Down Expand Up @@ -170,13 +173,16 @@ impl LspClient {
&self,
fee_request: FeeRequest,
) -> Result<u64, MutinyError> {
let fee_response: FeeResponse = self
let request = self
.http_client
.post(format!("{}{}", &self.url, FEE_PATH))
.json(&fee_request)
.send()
.await
.map_err(|_| MutinyError::LspGenericError)?
.build()
.map_err(|_| MutinyError::LspGenericError)?;
let response: reqwest::Response =
utils::fetch_with_timeout(&self.http_client, request).await?;

let fee_response: FeeResponse = response
.json()
.await
.map_err(|_| MutinyError::LspGenericError)?;
Expand Down
6 changes: 3 additions & 3 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2262,12 +2262,12 @@ impl<S: MutinyStorage> NodeManager<S> {
.build()
.map_err(|_| MutinyError::BitcoinPriceError)?;

let resp = client
let request = client
.get("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd")
.send()
.await
.build()
.map_err(|_| MutinyError::BitcoinPriceError)?;

let resp: reqwest::Response = utils::fetch_with_timeout(&client, request).await?;
let response: CoingeckoResponse = resp
.error_for_status()
.map_err(|_| MutinyError::BitcoinPriceError)?
Expand Down
36 changes: 35 additions & 1 deletion mutiny-core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use crate::error::MutinyError;
use bitcoin::Network;
use core::cell::{RefCell, RefMut};
use core::ops::{Deref, DerefMut};
use core::time::Duration;
use futures::{
future::{self, Either},
pin_mut,
};
use lightning::routing::scoring::LockableScore;
use lightning::routing::scoring::Score;
use lightning::util::ser::Writeable;
use lightning::util::ser::Writer;
use reqwest::Client;

pub const FETCH_TIMEOUT: i32 = 30_000;

pub(crate) fn min_lightning_amount(network: Network) -> u64 {
match network {
Expand All @@ -28,7 +36,7 @@ pub async fn sleep(millis: i32) {
}
#[cfg(not(target_arch = "wasm32"))]
{
std::thread::sleep(Duration::from_millis(millis.try_into().unwrap()));
tokio::time::sleep(Duration::from_millis(millis.try_into().unwrap())).await;
}
}

Expand All @@ -44,6 +52,32 @@ pub fn now() -> Duration {
.unwrap();
}

pub async fn fetch_with_timeout(
client: &Client,
req: reqwest::Request,
) -> Result<reqwest::Response, MutinyError> {
let fetch_future = fetch(client, req);
let timeout_future = async {
sleep(FETCH_TIMEOUT).await;
Err(MutinyError::ConnectionFailed)
};

pin_mut!(fetch_future);
pin_mut!(timeout_future);

match future::select(fetch_future, timeout_future).await {
Either::Left((ok, _)) => ok,
Either::Right((err, _)) => err,
}
}

async fn fetch(client: &Client, req: reqwest::Request) -> Result<reqwest::Response, MutinyError> {
client
.execute(req)
.await
.map_err(|_| MutinyError::ConnectionFailed)
}

pub type LockResult<Guard> = Result<Guard, ()>;

pub struct Mutex<T: ?Sized> {
Expand Down

0 comments on commit 341b612

Please sign in to comment.