Skip to content

Commit 5f0c748

Browse files
committed
gossip-memory: replace Mutex with RwLock for better concurrency
Pull-Request: #1126 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent c37ace8 commit 5f0c748

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

gossip/nostr-gossip-memory/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
2424
-->
2525

26+
## Unreleased
27+
28+
### Changed
29+
30+
- Replace `Mutex` with `RwLock` for better concurrency (https://github.com/rust-nostr/nostr/pull/1126)
31+
2632
## v0.44.0 - 2025/11/06
2733

2834
First release.

gossip/nostr-gossip-memory/src/store.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use nostr::{Event, Kind, PublicKey, RelayUrl, TagKind, TagStandard, Timestamp};
1414
use nostr_gossip::error::GossipError;
1515
use nostr_gossip::flags::GossipFlags;
1616
use nostr_gossip::{BestRelaySelection, GossipListKind, GossipPublicKeyStatus, NostrGossip};
17-
use tokio::sync::Mutex;
17+
use tokio::sync::RwLock;
1818

1919
use crate::constant::{MAX_NIP17_SIZE, MAX_NIP65_SIZE, PUBKEY_METADATA_OUTDATED_AFTER};
2020

@@ -44,26 +44,26 @@ impl Default for PkData {
4444
/// Gossip in-memory storage.
4545
#[derive(Debug, Clone)]
4646
pub struct NostrGossipMemory {
47-
public_keys: Arc<Mutex<LruCache<PublicKey, PkData>>>,
47+
public_keys: Arc<RwLock<LruCache<PublicKey, PkData>>>,
4848
}
4949

5050
impl NostrGossipMemory {
5151
/// Construct a new **unbounded** instance
5252
pub fn unbounded() -> Self {
5353
Self {
54-
public_keys: Arc::new(Mutex::new(LruCache::unbounded())),
54+
public_keys: Arc::new(RwLock::new(LruCache::unbounded())),
5555
}
5656
}
5757

5858
/// Construct a new **bounded** instance
5959
pub fn bounded(limit: NonZeroUsize) -> Self {
6060
Self {
61-
public_keys: Arc::new(Mutex::new(LruCache::new(limit))),
61+
public_keys: Arc::new(RwLock::new(LruCache::new(limit))),
6262
}
6363
}
6464

6565
async fn process_event(&self, event: &Event, relay_url: Option<&RelayUrl>) {
66-
let mut public_keys = self.public_keys.lock().await;
66+
let mut public_keys = self.public_keys.write().await;
6767

6868
match &event.kind {
6969
// Extract NIP-65 relays
@@ -149,9 +149,9 @@ impl NostrGossipMemory {
149149
public_key: &PublicKey,
150150
list: GossipListKind,
151151
) -> GossipPublicKeyStatus {
152-
let mut public_keys = self.public_keys.lock().await;
152+
let public_keys = self.public_keys.read().await;
153153

154-
match public_keys.get(public_key) {
154+
match public_keys.peek(public_key) {
155155
Some(pk_data) => {
156156
let now: Timestamp = Timestamp::now();
157157

@@ -178,7 +178,7 @@ impl NostrGossipMemory {
178178
}
179179

180180
async fn _update_fetch_attempt(&self, public_key: &PublicKey, list: GossipListKind) {
181-
let mut public_keys = self.public_keys.lock().await;
181+
let mut public_keys = self.public_keys.write().await;
182182

183183
let pk_data: &mut PkData = public_keys.get_or_insert_mut(*public_key, PkData::default);
184184

@@ -195,7 +195,7 @@ impl NostrGossipMemory {
195195
public_key: &PublicKey,
196196
selection: BestRelaySelection,
197197
) -> HashSet<RelayUrl> {
198-
let public_keys = self.public_keys.lock().await;
198+
let public_keys = self.public_keys.read().await;
199199

200200
let mut relays: HashSet<RelayUrl> = HashSet::new();
201201

0 commit comments

Comments
 (0)