Skip to content

Commit 28c545f

Browse files
committed
pool: remove result clone from batch_msg_to and batch_event_to relay pool methods
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent e5998e9 commit 28c545f

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
pub mod pool;
1414
pub mod prelude;
1515
pub mod relay;
16+
mod util;
1617

1718
pub use self::pool::options::RelayPoolOptions;
1819
pub use self::pool::{RelayPool, RelayPoolNotification, SendEventOutput, SendOutput};

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::options::RelayPoolOptions;
2121
use super::{Error, RelayPoolNotification, SendEventOutput, SendOutput};
2222
use crate::relay::options::{FilterOptions, NegentropyOptions, RelayOptions, RelaySendOptions};
2323
use crate::relay::{Relay, RelayBlacklist};
24-
use crate::SubscribeOptions;
24+
use crate::{util, SubscribeOptions};
2525

2626
#[derive(Debug, Clone)]
2727
pub struct InternalRelayPool {
@@ -60,8 +60,8 @@ impl InternalRelayPool {
6060
relays: Arc::new(RwLock::new(HashMap::new())),
6161
notification_sender,
6262
subscriptions: Arc::new(RwLock::new(HashMap::new())),
63-
blacklist: RelayBlacklist::empty(), // TODO: allow to initialize pool with custom blacklist?
64-
//opts,
63+
blacklist: RelayBlacklist::empty(),
64+
//opts,
6565
}
6666
}
6767

@@ -315,13 +315,13 @@ impl InternalRelayPool {
315315
handle.join().await?;
316316
}
317317

318-
let result = result.lock().await;
318+
let result: SendOutput = util::take_mutex_ownership(result).await;
319319

320320
if result.success.is_empty() {
321321
return Err(Error::MsgNotSent);
322322
}
323323

324-
Ok(result.clone()) // TODO: remove clone
324+
Ok(result)
325325
}
326326
}
327327

@@ -438,13 +438,13 @@ impl InternalRelayPool {
438438
handle.join().await?;
439439
}
440440

441-
let result = result.lock().await;
441+
let result: SendOutput = util::take_mutex_ownership(result).await;
442442

443443
if result.success.is_empty() {
444444
return Err(Error::EventNotPublished);
445445
}
446446

447-
Ok(result.clone()) // TODO: remove clone
447+
Ok(result)
448448
}
449449
}
450450

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2022-2023 Yuki Kishimoto
2+
// Copyright (c) 2023-2024 Rust Nostr Developers
3+
// Distributed under the MIT software license
4+
5+
use std::sync::Arc;
6+
7+
use tokio::sync::Mutex;
8+
9+
/// Take ownership of `T` from `Arc<Mutex<T>>`.
10+
///
11+
/// Try to take ownership of result without clone.
12+
/// Clone if fail to unwrap inner value of `Arc`.
13+
pub(crate) async fn take_mutex_ownership<T>(val: Arc<Mutex<T>>) -> T
14+
where
15+
T: Clone,
16+
{
17+
match Arc::try_unwrap(val) {
18+
Ok(mutex) => mutex.into_inner(),
19+
Err(arc) => {
20+
let lock = arc.lock().await;
21+
lock.clone()
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)