@@ -14,7 +14,7 @@ use nostr::{Event, Kind, PublicKey, RelayUrl, TagKind, TagStandard, Timestamp};
1414use nostr_gossip:: error:: GossipError ;
1515use nostr_gossip:: flags:: GossipFlags ;
1616use nostr_gossip:: { BestRelaySelection , GossipListKind , GossipPublicKeyStatus , NostrGossip } ;
17- use tokio:: sync:: Mutex ;
17+ use tokio:: sync:: RwLock ;
1818
1919use 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 ) ]
4646pub struct NostrGossipMemory {
47- public_keys : Arc < Mutex < LruCache < PublicKey , PkData > > > ,
47+ public_keys : Arc < RwLock < LruCache < PublicKey , PkData > > > ,
4848}
4949
5050impl 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