@@ -9,7 +9,7 @@ use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent
99
1010use crate :: entry:: peer_list:: PeerList ;
1111use crate :: entry:: torrent:: TrackedTorrent ;
12- use crate :: TrackedTorrentHandle ;
12+ use crate :: { LockTrackedTorrent , TrackedTorrentHandle } ;
1313
1414#[ derive( Default , Debug ) ]
1515pub struct TorrentRepository {
@@ -46,11 +46,7 @@ impl TorrentRepository {
4646 if let Some ( existing_entry) = self . torrents . get ( info_hash) {
4747 tracing:: debug!( "Torrent already exists: {:?}" , info_hash) ;
4848
49- existing_entry
50- . value ( )
51- . lock ( )
52- . expect ( "can't acquire lock for tracked torrent handle" )
53- . upsert_peer ( peer)
49+ existing_entry. value ( ) . lock_or_panic ( ) . upsert_peer ( peer)
5450 } else {
5551 tracing:: debug!( "Inserting new torrent: {:?}" , info_hash) ;
5652
@@ -68,10 +64,7 @@ impl TorrentRepository {
6864
6965 let inserted_entry = self . torrents . get_or_insert ( * info_hash, new_entry) ;
7066
71- let mut torrent_guard = inserted_entry
72- . value ( )
73- . lock ( )
74- . expect ( "can't acquire lock for tracked torrent handle" ) ;
67+ let mut torrent_guard = inserted_entry. value ( ) . lock_or_panic ( ) ;
7568
7669 torrent_guard. upsert_peer ( peer)
7770 }
@@ -97,11 +90,7 @@ impl TorrentRepository {
9790 /// This function panics if the lock for the entry cannot be obtained.
9891 pub fn remove_inactive_peers ( & self , current_cutoff : DurationSinceUnixEpoch ) {
9992 for entry in & self . torrents {
100- entry
101- . value ( )
102- . lock ( )
103- . expect ( "can't acquire lock for tracked torrent handle" )
104- . remove_inactive_peers ( current_cutoff) ;
93+ entry. value ( ) . lock_or_panic ( ) . remove_inactive_peers ( current_cutoff) ;
10594 }
10695 }
10796
@@ -154,13 +143,9 @@ impl TorrentRepository {
154143 /// This function panics if the lock for the entry cannot be obtained.
155144 #[ must_use]
156145 pub fn get_swarm_metadata ( & self , info_hash : & InfoHash ) -> Option < SwarmMetadata > {
157- self . torrents . get ( info_hash) . map ( |entry| {
158- entry
159- . value ( )
160- . lock ( )
161- . expect ( "can't acquire lock for tracked torrent handle" )
162- . get_swarm_metadata ( )
163- } )
146+ self . torrents
147+ . get ( info_hash)
148+ . map ( |entry| entry. value ( ) . lock_or_panic ( ) . get_swarm_metadata ( ) )
164149 }
165150
166151 /// Retrieves swarm metadata for a given torrent.
@@ -196,10 +181,7 @@ impl TorrentRepository {
196181 pub fn get_peers_for ( & self , info_hash : & InfoHash , peer : & peer:: Peer , limit : usize ) -> Vec < Arc < peer:: Peer > > {
197182 match self . get ( info_hash) {
198183 None => vec ! [ ] ,
199- Some ( entry) => entry
200- . lock ( )
201- . expect ( "can't acquire lock for tracked torrent handle" )
202- . get_peers_for_client ( & peer. peer_addr , Some ( limit) ) ,
184+ Some ( entry) => entry. lock_or_panic ( ) . get_peers_for_client ( & peer. peer_addr , Some ( limit) ) ,
203185 }
204186 }
205187
@@ -220,10 +202,7 @@ impl TorrentRepository {
220202 pub fn get_torrent_peers ( & self , info_hash : & InfoHash , limit : usize ) -> Vec < Arc < peer:: Peer > > {
221203 match self . get ( info_hash) {
222204 None => vec ! [ ] ,
223- Some ( entry) => entry
224- . lock ( )
225- . expect ( "can't acquire lock for tracked torrent handle" )
226- . get_peers ( Some ( limit) ) ,
205+ Some ( entry) => entry. lock_or_panic ( ) . get_peers ( Some ( limit) ) ,
227206 }
228207 }
229208
@@ -237,12 +216,7 @@ impl TorrentRepository {
237216 /// This function panics if the lock for the entry cannot be obtained.
238217 pub fn remove_peerless_torrents ( & self , policy : & TrackerPolicy ) {
239218 for entry in & self . torrents {
240- if entry
241- . value ( )
242- . lock ( )
243- . expect ( "can't acquire lock for tracked torrent handle" )
244- . meets_retaining_policy ( policy)
245- {
219+ if entry. value ( ) . lock_or_panic ( ) . meets_retaining_policy ( policy) {
246220 continue ;
247221 }
248222
@@ -293,11 +267,7 @@ impl TorrentRepository {
293267 let mut metrics = AggregateSwarmMetadata :: default ( ) ;
294268
295269 for entry in & self . torrents {
296- let stats = entry
297- . value ( )
298- . lock ( )
299- . expect ( "can't acquire lock for tracked torrent handle" )
300- . get_swarm_metadata ( ) ;
270+ let stats = entry. value ( ) . lock_or_panic ( ) . get_swarm_metadata ( ) ;
301271 metrics. total_complete += u64:: from ( stats. complete ) ;
302272 metrics. total_downloaded += u64:: from ( stats. downloaded ) ;
303273 metrics. total_incomplete += u64:: from ( stats. incomplete ) ;
@@ -584,7 +554,7 @@ mod tests {
584554
585555 use crate :: repository:: TorrentRepository ;
586556 use crate :: tests:: { sample_info_hash, sample_peer} ;
587- use crate :: TrackedTorrentHandle ;
557+ use crate :: { LockTrackedTorrent , TrackedTorrentHandle } ;
588558
589559 /// `TorrentEntry` data is not directly accessible. It's only
590560 /// accessible through the trait methods. We need this temporary
@@ -599,7 +569,7 @@ mod tests {
599569 #[ allow( clippy:: from_over_into) ]
600570 impl Into < TorrentEntryInfo > for TrackedTorrentHandle {
601571 fn into ( self ) -> TorrentEntryInfo {
602- let torrent_guard = self . lock ( ) . expect ( "can't acquire lock for tracked torrent handle" ) ;
572+ let torrent_guard = self . lock_or_panic ( ) ;
603573
604574 let torrent_entry_info = TorrentEntryInfo {
605575 swarm_metadata : torrent_guard. get_swarm_metadata ( ) ,
0 commit comments