From 2064313e286c7993c8040be143f1b05da5b8087e Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Fri, 24 Jan 2025 12:19:25 +0100 Subject: [PATCH] pool: handle `close` WebSocket message Signed-off-by: Yuki Kishimoto --- CHANGELOG.md | 1 + crates/nostr-relay-pool/src/relay/inner.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 706901491..c36c7ffbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,7 @@ * pool: avoid repeatedly locking the relay channel receiver ([Yuki Kishimoto]) * pool: refactor `RelayPool::stream_events_targeted` ([Yuki Kishimoto]) * pool: refactor relay removal logic and add unit tests ([Yuki Kishimoto]) +* pool: handle `close` WebSocket message ([Yuki Kishimoto]) * lmdb: use `EventBorrow` instead of `DatabaseEvent` ([Yuki Kishimoto]) * ndb: refactor note-to-event conversion ([Yuki Kishimoto]) * relay-builder: refactor shutdown mechanism to use `Notify` over `broadcast` ([Yuki Kishimoto]) diff --git a/crates/nostr-relay-pool/src/relay/inner.rs b/crates/nostr-relay-pool/src/relay/inner.rs index f9adb800d..267bdcd8b 100644 --- a/crates/nostr-relay-pool/src/relay/inner.rs +++ b/crates/nostr-relay-pool/src/relay/inner.rs @@ -710,6 +710,10 @@ impl InnerRelay { while let Some(msg) = ws_rx.next().await { match msg? { + WsMessage::Text(json) => self.handle_relay_message(&json).await, + WsMessage::Binary(_) => { + tracing::warn!(url = %self.url, "Binary messages aren't supported."); + } #[cfg(not(target_arch = "wasm32"))] WsMessage::Pong(bytes) => { if self.flags.has_ping() { @@ -742,9 +746,12 @@ impl InnerRelay { } } } - WsMessage::Text(json) => self.handle_relay_message(&json).await, - WsMessage::Binary(_) => { - tracing::warn!(url = %self.url, "Binary messages aren't supported."); + #[cfg(not(target_arch = "wasm32"))] + WsMessage::Close(None) => break, + #[cfg(not(target_arch = "wasm32"))] + WsMessage::Close(Some(frame)) => { + tracing::info!(code = %frame.code, reason = %frame.reason, "Connection closed by peer."); + break; } #[cfg(not(target_arch = "wasm32"))] _ => {}