Skip to content

Commit

Permalink
pool: update Error::WebSocket variant inner type
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jan 1, 2025
1 parent 1d4bdf6 commit 8c39462
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

### Changed

* pool: update `Error::WebSocket` variant inner type ([Yuki Kishimoto])

### Added

* pool: add `Relay::try_connect` ([Yuki Kishimoto])
Expand Down
13 changes: 5 additions & 8 deletions crates/nostr-relay-pool/src/relay/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::fmt;
use std::time::Duration;

use async_wsocket::Error as WsError;
use nostr::event;
use nostr::event::builder;
use nostr::message::MessageHandleError;
Expand All @@ -18,7 +19,7 @@ use crate::RelayPoolNotification;
#[derive(Debug)]
pub enum Error {
/// WebSocket error
WebSocket(Box<dyn std::error::Error + Send + Sync>),
WebSocket(WsError),
/// Shared state error
SharedState(SharedStateError),
/// MessageHandle error
Expand Down Expand Up @@ -180,13 +181,9 @@ impl fmt::Display for Error {
}
}

impl Error {
#[inline]
pub(super) fn websocket<E>(error: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::WebSocket(Box::new(error))
impl From<WsError> for Error {
fn from(e: WsError) -> Self {
Self::WebSocket(e)
}
}

Expand Down
11 changes: 4 additions & 7 deletions crates/nostr-relay-pool/src/relay/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,7 @@ impl InnerRelay {
}

// Try to connect
let stream = self
._try_connect(timeout, false)
.await
.map_err(Error::websocket)?;
let stream = self._try_connect(timeout, false).await?;

// Spawn connection task
self.spawn_and_try_connect(Some(stream));
Expand Down Expand Up @@ -800,7 +797,7 @@ impl InnerRelay {
let _ping = ping;

while let Some(msg) = ws_rx.next().await {
match msg.map_err(Error::websocket)? {
match msg? {
#[cfg(not(target_arch = "wasm32"))]
WsMessage::Pong(bytes) => {
if self.flags.has_ping() {
Expand Down Expand Up @@ -2398,15 +2395,15 @@ impl InnerRelay {
async fn send_ws_msgs(tx: &mut Sink, msgs: Vec<WsMessage>) -> Result<(), Error> {
let mut stream = futures_util::stream::iter(msgs.into_iter().map(Ok));
match time::timeout(Some(WEBSOCKET_TX_TIMEOUT), tx.send_all(&mut stream)).await {
Some(res) => res.map_err(Error::websocket),
Some(res) => Ok(res?),
None => Err(Error::Timeout),
}
}

/// Send WebSocket messages with timeout set to [WEBSOCKET_TX_TIMEOUT].
async fn close_ws(tx: &mut Sink) -> Result<(), Error> {
match time::timeout(Some(WEBSOCKET_TX_TIMEOUT), tx.close()).await {
Some(res) => res.map_err(Error::websocket),
Some(res) => Ok(res?),
None => Err(Error::Timeout),
}
}

0 comments on commit 8c39462

Please sign in to comment.