Skip to content

Commit

Permalink
pool: ensure notification subscription in subscribe auto-close logic
Browse files Browse the repository at this point in the history
Subscribe to internal notifications before sending the REQ message to the relay.

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jan 29, 2025
1 parent cd2f1cc commit 2a4b48e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
10 changes: 6 additions & 4 deletions crates/nostr-relay-pool/src/relay/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,11 +1195,15 @@ impl InnerRelay {
id: SubscriptionId,
filter: Filter,
opts: SubscribeAutoCloseOptions,
notifications: broadcast::Receiver<RelayNotification>,
) {
let relay = self.clone(); // <-- FULL RELAY CLONE HERE
task::spawn(async move {
// Check if CLOSE needed
let to_close: bool = match relay.handle_auto_closing(&id, &filter, opts).await {
let to_close: bool = match relay
.handle_auto_closing(&id, &filter, opts, notifications)
.await
{
Some((to_close, reason)) => {
// Send subscription auto-closed notification
if let Some(reason) = reason {
Expand Down Expand Up @@ -1233,16 +1237,14 @@ impl InnerRelay {
id: &SubscriptionId,
filter: &Filter,
opts: SubscribeAutoCloseOptions,
mut notifications: broadcast::Receiver<RelayNotification>,
) -> Option<(bool, Option<SubscriptionAutoClosedReason>)> {
time::timeout(opts.timeout, async move {
let mut counter: u16 = 0;
let mut received_eose: bool = false;
let mut require_resubscription: bool = false;
let mut last_event: Option<Instant> = None;

// Subscribe to notifications
let mut notifications = self.internal_notification_sender.subscribe();

// Listen to notifications with timeout
// If no notification is received within no-events timeout, `None` is returned.
while let Ok(notification) =
Expand Down
18 changes: 15 additions & 3 deletions crates/nostr-relay-pool/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,29 @@ impl Relay {
filter: Filter,
opts: SubscribeOptions,
) -> Result<(), Error> {
// Compose and send REQ message
// Compose REQ message
let msg: ClientMessage = ClientMessage::Req {
subscription_id: Cow::Borrowed(&id),
filter: Cow::Borrowed(&filter),
};
self.inner.send_msg(msg)?;

// Check if auto-close condition is set
match opts.auto_close {
Some(opts) => self.inner.spawn_auto_closing_handler(id, filter, opts),
Some(opts) => {
// Subscribe to notifications
let notifications = self.inner.internal_notification_sender.subscribe();

// Send REQ message
self.inner.send_msg(msg)?;

// Spawn auto-closing handler
self.inner
.spawn_auto_closing_handler(id, filter, opts, notifications)
}
None => {
// Send REQ message
self.inner.send_msg(msg)?;

// No auto-close subscription: update subscription filter
self.inner.update_subscription(id, filter, true).await;
}
Expand Down

0 comments on commit 2a4b48e

Please sign in to comment.