Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions settings.tpl.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ payment_retries_interval = 60
[nostr]
nsec_privkey = 'nsec1...'
relays = ['ws://localhost:7000']
# Seconds to wait for relay connections at startup before continuing (default: 5)
# relay_connection_timeout_secs = 5

[mostro]
# NIP-01 Kind 0 Metadata (optional)
Expand Down
1 change: 1 addition & 0 deletions src/app/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ pub mod test_utils {
nsec_privkey: "nsec13as48eum93hkg7plv526r9gjpa0uc52zysqm93pmnkca9e69x6tsdjmdxd"
.to_string(),
relays: vec!["wss://relay.test".to_string()],
relay_connection_timeout_secs: 5,
},
mostro: MostroSettings::default(),
lightning: LightningSettings::default(),
Expand Down
7 changes: 7 additions & 0 deletions src/config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ pub struct NostrSettings {
pub nsec_privkey: String,
/// Nostr relays list
pub relays: Vec<String>,
/// Seconds to wait for relay connections at startup before continuing (default: 5)
#[serde(default = "default_relay_connection_timeout_secs")]
pub relay_connection_timeout_secs: u64,
}

fn default_relay_connection_timeout_secs() -> u64 {
5
}
/// RPC configuration settings
#[derive(Debug, Deserialize, Clone)]
Expand Down
18 changes: 17 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,24 @@ pub async fn connect_nostr() -> Result<Client, MostroError> {
.map_err(|e| MostroInternalErr(ServiceError::NostrError(e.to_string())))?;
}

// Connect to relays and keep connection alive
let timeout =
std::time::Duration::from_secs(nostr_settings.relay_connection_timeout_secs);
client.connect().await;
client.wait_for_connection(timeout).await;

let connected = client
.relays()
.await
.values()
.filter(|r| r.is_connected())
.count();

if connected == 0 {
tracing::error!("No Nostr relays connected after timeout of {}s", nostr_settings.relay_connection_timeout_secs);
return Err(MostroInternalErr(ServiceError::NostrError(
"No relays connected".to_string(),
)));
}

Ok(client)
}
Expand Down