Skip to content

Commit

Permalink
pool: return reference instead of cloned structs for some getter meth…
Browse files Browse the repository at this point in the history
…ods of `Relay`

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Oct 28, 2024
1 parent 9a7f5ab commit ad910be
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* pool: avoid full `InnerRelay` clone when requesting NIP11 document ([Yuki Kishimoto])
* pool: rework relay connection methods and auto-connection logic ([Yuki Kishimoto])
* pool: increase `MAX_ADJ_RETRY_SEC` to 120 secs ([Yuki Kishimoto])
* pool: return reference instead of cloned structs for some getter methods of `Relay` ([Yuki Kishimoto])
* sdk: deprecate `Client::get_events_of` and `Client::get_events_from` methods ([Yuki Kishimoto])
* sdk: use `Events` instead of `Vec<Event>` in fetch and query methods ([Yuki Kishimoto])
* sdk: rename `stream_events_of` to `stream_events` ([Yuki Kishimoto])
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Relay {

/// Get relay filtering
pub fn filtering(&self) -> RelayFiltering {
self.inner.filtering().into()
self.inner.filtering().clone().into()
}

/// Check if `Relay` is connected
Expand Down Expand Up @@ -190,7 +190,7 @@ impl Relay {
}

pub fn opts(&self) -> RelayOptions {
self.inner.opts().into()
self.inner.opts().clone().into()
}

pub fn stats(&self) -> Arc<RelayConnectionStats> {
Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-sdk-js/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ impl JsRelay {

/// Get Relay Service Flags
pub fn flags(&self) -> JsAtomicRelayServiceFlags {
self.inner.flags().into()
self.inner.flags().clone().into()
}

/// Get relay filtering
pub fn filtering(&self) -> JsRelayFiltering {
self.inner.filtering().into()
self.inner.filtering().clone().into()
}

/// Check if relay is connected
Expand All @@ -147,7 +147,7 @@ impl JsRelay {

/// Get options
pub fn opts(&self) -> JsRelayOptions {
self.inner.opts().into()
self.inner.opts().clone().into()
}

// TODO: add stats
Expand Down
7 changes: 3 additions & 4 deletions crates/nostr-relay-pool/src/pool/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ impl InternalRelayPool {
flag: RelayServiceFlags,
check: FlagCheck,
) -> impl Iterator<Item = (&'a Url, &'a Relay)> + 'a {
txn.iter()
.filter(move |(_, r)| r.flags_ref().has(flag, check))
txn.iter().filter(move |(_, r)| r.flags().has(flag, check))
}

/// Get relays that has `READ` or `WRITE` flags
Expand Down Expand Up @@ -242,7 +241,7 @@ impl InternalRelayPool {
}

// Insert relay into map
relays.insert(relay.url(), relay);
relays.insert(relay.url().clone(), relay);

Ok(true)
}
Expand Down Expand Up @@ -282,7 +281,7 @@ impl InternalRelayPool {
if let Some(relay) = relays.remove(&url) {
// It NOT force, check if has INBOX or OUTBOX flags
if !force {
let flags = relay.flags_ref();
let flags = relay.flags();
if flags.has_any(RelayServiceFlags::INBOX | RelayServiceFlags::OUTBOX) {
// Remove READ, WRITE and DISCOVERY flags
flags.remove(
Expand Down
48 changes: 11 additions & 37 deletions crates/nostr-relay-pool/src/relay/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use super::constants::{
WEBSOCKET_TX_TIMEOUT,
};
use super::filtering::{CheckFiltering, RelayFiltering};
use super::flags::AtomicRelayServiceFlags;
use super::options::{
FilterOptions, RelayOptions, RelaySendOptions, SubscribeAutoCloseOptions, SubscribeOptions,
SyncOptions,
Expand Down Expand Up @@ -150,9 +149,9 @@ pub(crate) struct InnerRelay {
status: Arc<AtomicRelayStatus>,
#[cfg(feature = "nip11")]
document: Arc<RwLock<RelayInformationDocument>>,
opts: RelayOptions,
pub(super) opts: RelayOptions,
pub(super) stats: RelayConnectionStats,
filtering: RelayFiltering,
pub(super) filtering: RelayFiltering,
database: Arc<DynNostrDatabase>,
channels: Arc<RelayChannels>,
pub(super) internal_notification_sender: broadcast::Sender<RelayNotification>,
Expand Down Expand Up @@ -199,11 +198,6 @@ impl InnerRelay {
}
}

#[inline]
pub fn url(&self) -> Url {
self.url.clone()
}

#[inline]
pub fn connection_mode(&self) -> ConnectionMode {
self.opts.connection_mode.clone()
Expand Down Expand Up @@ -235,21 +229,6 @@ impl InnerRelay {
self.send_notification(RelayNotification::RelayStatus { status }, true);
}

#[inline]
pub fn flags(&self) -> AtomicRelayServiceFlags {
self.opts.flags.clone()
}

#[inline]
pub fn flags_ref(&self) -> &AtomicRelayServiceFlags {
&self.opts.flags
}

#[inline]
pub fn filtering(&self) -> RelayFiltering {
self.filtering.clone()
}

#[inline]
pub fn is_connected(&self) -> bool {
self.status().is_connected()
Expand Down Expand Up @@ -310,7 +289,7 @@ impl InnerRelay {
};

if allowed {
let url = self.url();
let url = self.url.clone();
let d = self.document.clone();
let _ = thread::spawn(async move {
match RelayInformationDocument::get(url.clone(), proxy).await {
Expand Down Expand Up @@ -362,7 +341,7 @@ impl InnerRelay {
}
}

/// Check if should subscribe for current websocket session
/// Check if it should subscribe for current websocket session
pub(crate) async fn should_resubscribe(&self, id: &SubscriptionId) -> bool {
let subscriptions = self.subscriptions.read().await;
match subscriptions.get(id) {
Expand Down Expand Up @@ -390,11 +369,6 @@ impl InnerRelay {
subscriptions.remove(id);
}

#[inline]
pub fn opts(&self) -> RelayOptions {
self.opts.clone()
}

#[inline]
pub fn queue(&self) -> usize {
self.channels.nostr_queue()
Expand All @@ -420,22 +394,22 @@ impl InnerRelay {
subscription_id,
event,
} => RelayPoolNotification::Event {
relay_url: self.url(),
relay_url: self.url.clone(),
subscription_id,
event,
},
RelayNotification::Message { message } => RelayPoolNotification::Message {
relay_url: self.url(),
relay_url: self.url.clone(),
message,
},
RelayNotification::RelayStatus { status } => {
RelayPoolNotification::RelayStatus {
relay_url: self.url(),
relay_url: self.url.clone(),
status,
}
}
RelayNotification::Authenticated => RelayPoolNotification::Authenticated {
relay_url: self.url(),
relay_url: self.url.clone(),
},
RelayNotification::Shutdown => RelayPoolNotification::Shutdown,
};
Expand Down Expand Up @@ -958,7 +932,7 @@ impl InnerRelay {
// Set event as seen by relay
if let Err(e) = self
.database
.event_id_seen(partial_event.id, self.url())
.event_id_seen(partial_event.id, self.url.clone())
.await
{
tracing::error!(
Expand Down Expand Up @@ -1857,7 +1831,7 @@ impl InnerRelay {

output
.send_failures
.entry(self.url())
.entry(self.url.clone())
.and_modify(|map| {
map.insert(event_id, message.clone());
})
Expand Down Expand Up @@ -2167,7 +2141,7 @@ impl InnerRelay {

output
.send_failures
.entry(self.url())
.entry(self.url.clone())
.and_modify(|map| {
map.insert(event_id, message.clone());
})
Expand Down
24 changes: 9 additions & 15 deletions crates/nostr-relay-pool/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ impl Relay {

/// Get relay url
#[inline]
pub fn url(&self) -> Url {
self.inner.url()
pub fn url(&self) -> &Url {
&self.inner.url
}

/// Get connection mode
Expand All @@ -177,20 +177,14 @@ impl Relay {

/// Get Relay Service Flags
#[inline]
pub fn flags(&self) -> AtomicRelayServiceFlags {
self.inner.flags()
}

/// Get Relay Service Flags
#[inline]
pub fn flags_ref(&self) -> &AtomicRelayServiceFlags {
self.inner.flags_ref()
pub fn flags(&self) -> &AtomicRelayServiceFlags {
&self.inner.opts.flags
}

/// Get relay filtering
#[inline]
pub fn filtering(&self) -> RelayFiltering {
self.inner.filtering()
pub fn filtering(&self) -> &RelayFiltering {
&self.inner.filtering
}

/// Check if relay is connected
Expand Down Expand Up @@ -218,10 +212,10 @@ impl Relay {
self.inner.subscription(id).await
}

/// Get [`RelayOptions`]
/// Get options
#[inline]
pub fn opts(&self) -> RelayOptions {
self.inner.opts()
pub fn opts(&self) -> &RelayOptions {
&self.inner.opts
}

/// Get [`RelayConnectionStats`]
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl Client {
.await?
{
Some(relay) => {
relay.flags_ref().add(flag);
relay.flags().add(flag);
Ok(false)
}
None => {
Expand Down

0 comments on commit ad910be

Please sign in to comment.