Tunstenite and reqwest connect timeout

This commit is contained in:
Mike Dilger 2022-12-26 14:53:13 +13:00
parent a5c5627749
commit 0d503a5857
2 changed files with 15 additions and 7 deletions

View File

@ -42,6 +42,9 @@ pub enum Error {
#[error("SQL: {0}")] #[error("SQL: {0}")]
Sql(#[from] rusqlite::Error), Sql(#[from] rusqlite::Error),
#[error("Timeout: {0}")]
Timeout(#[from] tokio::time::error::Elapsed),
#[error("URL has empty hostname")] #[error("URL has empty hostname")]
UrlHasEmptyHostname, UrlHasEmptyHostname,

View File

@ -92,13 +92,15 @@ impl Minion {
return Err(Error::UrlHasEmptyHostname); return Err(Error::UrlHasEmptyHostname);
} }
// Read NIP-11 information let request_nip11_future = reqwest::Client::new()
if let Ok(response) = reqwest::Client::new() .get(format!("https://{}", host))
.get(&format!("https://{}", host))
.header("Host", host) .header("Host", host)
.header("Accept", "application/nostr+json") .header("Accept", "application/nostr+json")
.send() .send();
.await
// Read NIP-11 information
if let Ok(response) =
tokio::time::timeout(std::time::Duration::new(15, 0), request_nip11_future).await?
{ {
match response.json::<RelayInformationDocument>().await { match response.json::<RelayInformationDocument>().await {
Ok(nip11) => { Ok(nip11) => {
@ -130,8 +132,11 @@ impl Minion {
accept_unmasked_frames: true, // default is false which is the standard accept_unmasked_frames: true, // default is false which is the standard
}; };
let (websocket_stream, _response) = let (websocket_stream, _response) = tokio::time::timeout(
tokio_tungstenite::connect_async_with_config(req, Some(config)).await?; std::time::Duration::new(15, 0),
tokio_tungstenite::connect_async_with_config(req, Some(config)),
)
.await??;
info!("Connected to {}", &self.url); info!("Connected to {}", &self.url);
websocket_stream websocket_stream