Add timeout to mute list fetching

If a relay does not have a user's mutelist, a function in our pipeline
would wait indefinitely, causing all other requests to be locked as
well.

This commit adds a timeout to avoid this issue

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2024-08-02 16:34:54 -07:00
parent 697bd4eb83
commit 82c28abff7

View File

@ -1,5 +1,6 @@
use super::ExtendedEvent; use super::ExtendedEvent;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use tokio::time::{timeout, Duration};
pub struct MuteManager { pub struct MuteManager {
client: Client, client: Client,
@ -99,19 +100,27 @@ impl MuteManager {
let mut mute_list: Option<Event> = None; let mut mute_list: Option<Event> = None;
let mut notifications = self.client.notifications(); let mut notifications = self.client.notifications();
while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Event { let timeout_duration = Duration::from_secs(10);
subscription_id, while let Ok(result) = timeout(timeout_duration, notifications.recv()).await {
event, if let Ok(notification) = result {
.. if let RelayPoolNotification::Event {
} = notification subscription_id,
{ event,
if this_subscription_id == subscription_id && event.kind == Kind::MuteList { ..
mute_list = Some((*event).clone()); } = notification
break; {
if this_subscription_id == subscription_id && event.kind == Kind::MuteList {
mute_list = Some((*event).clone());
break;
}
} }
} }
} }
if mute_list.is_none() {
log::debug!("Mute list not found for pubkey {:?}", pubkey);
}
self.client.unsubscribe(this_subscription_id).await; self.client.unsubscribe(this_subscription_id).await;
mute_list mute_list