Skip to content

Commit c492be2

Browse files
committed
pool: replace OnceLock with Option for the external notification sender
Replace `OnceLock` with `Option` for the external notification sender in `Relay` struct`. Removed the now unused `PoolNotificationSenderAlreadySet` error variant. Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent d10ce3f commit c492be2

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

crates/nostr-relay-pool/src/pool/inner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ impl InnerRelayPool {
143143
}
144144

145145
// Compose new relay
146-
let relay: Relay = Relay::new(url, self.state.clone(), opts);
146+
let mut relay: Relay = Relay::new(url, self.state.clone(), opts);
147147

148148
// Set notification sender
149149
relay
150150
.inner
151-
.set_notification_sender(self.notification_sender.clone())?;
151+
.set_notification_sender(self.notification_sender.clone());
152152

153153
// If relay has `READ` flag, inherit pool subscriptions
154154
if relay.flags().has_read() {

crates/nostr-relay-pool/src/relay/error.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ pub enum Error {
3737
NegentropyDeprecated(negentropy_deprecated::Error),
3838
/// Database error
3939
Database(DatabaseError),
40-
/// Pool notification sender already set
41-
PoolNotificationSenderAlreadySet,
4240
/// Generic timeout
4341
Timeout,
4442
/// Not replied to ping
@@ -130,9 +128,6 @@ impl fmt::Display for Error {
130128
Self::Negentropy(e) => write!(f, "{e}"),
131129
Self::NegentropyDeprecated(e) => write!(f, "{e}"),
132130
Self::Database(e) => write!(f, "{e}"),
133-
Self::PoolNotificationSenderAlreadySet => {
134-
write!(f, "pool notification sender already set")
135-
}
136131
Self::Timeout => write!(f, "timeout"),
137132
Self::NotRepliedToPing => write!(f, "not replied to ping"),
138133
Self::CantParsePong => write!(f, "can't parse pong"),

crates/nostr-relay-pool/src/relay/inner.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::collections::{HashMap, HashSet};
88
#[cfg(feature = "nip11")]
99
use std::sync::atomic::AtomicU64;
1010
use std::sync::atomic::{AtomicBool, Ordering};
11-
use std::sync::{Arc, OnceLock};
11+
use std::sync::Arc;
1212
use std::time::Duration;
1313

1414
use async_utility::{task, time};
@@ -148,7 +148,7 @@ pub(crate) struct InnerRelay {
148148
pub(super) stats: RelayConnectionStats,
149149
pub(super) state: SharedState,
150150
pub(super) internal_notification_sender: broadcast::Sender<RelayNotification>,
151-
external_notification_sender: OnceLock<broadcast::Sender<RelayPoolNotification>>,
151+
external_notification_sender: Option<broadcast::Sender<RelayPoolNotification>>,
152152
}
153153

154154
impl AtomicDestroyer for InnerRelay {
@@ -178,7 +178,7 @@ impl InnerRelay {
178178
stats: RelayConnectionStats::default(),
179179
state,
180180
internal_notification_sender: relay_notification_sender,
181-
external_notification_sender: OnceLock::new(),
181+
external_notification_sender: None,
182182
}
183183
}
184184

@@ -359,16 +359,14 @@ impl InnerRelay {
359359
}
360360

361361
pub(crate) fn set_notification_sender(
362-
&self,
362+
&mut self,
363363
notification_sender: broadcast::Sender<RelayPoolNotification>,
364-
) -> Result<(), Error> {
365-
self.external_notification_sender
366-
.set(notification_sender)
367-
.map_err(|_| Error::PoolNotificationSenderAlreadySet)
364+
) {
365+
self.external_notification_sender = Some(notification_sender);
368366
}
369367

370368
fn send_notification(&self, notification: RelayNotification, external: bool) {
371-
match (external, self.external_notification_sender.get()) {
369+
match (external, &self.external_notification_sender) {
372370
(true, Some(external_notification_sender)) => {
373371
// Clone and send internal notification
374372
let _ = self.internal_notification_sender.send(notification.clone());

0 commit comments

Comments
 (0)