@@ -651,10 +651,7 @@ impl Tracker {
651
651
peer. change_ip ( & assign_ip_address_to_peer ( remote_client_ip, self . external_ip ) ) ;
652
652
debug ! ( "After: {peer:?}" ) ;
653
653
654
- // we should update the torrent and get the stats before we get the peer list.
655
- let stats = self . update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ;
656
-
657
- let peers = self . get_torrent_peers_for_peer ( info_hash, peer) . await ;
654
+ let ( stats, peers) = self . inner_announce ( info_hash, peer) . await ;
658
655
659
656
AnnounceData {
660
657
peers,
@@ -722,19 +719,6 @@ impl Tracker {
722
719
Ok ( ( ) )
723
720
}
724
721
725
- async fn get_torrent_peers_for_peer ( & self , info_hash : & InfoHash , peer : & Peer ) -> Vec < peer:: Peer > {
726
- let read_lock = self . torrents . get_torrents ( ) . await ;
727
-
728
- match read_lock. get ( info_hash) {
729
- None => vec ! [ ] ,
730
- Some ( entry) => entry
731
- . get_peers_for_peer ( peer, TORRENT_PEERS_LIMIT )
732
- . into_iter ( )
733
- . copied ( )
734
- . collect ( ) ,
735
- }
736
- }
737
-
738
722
/// # Context: Tracker
739
723
///
740
724
/// Get all torrent peers for a given torrent
@@ -752,11 +736,8 @@ impl Tracker {
752
736
/// needed for a `announce` request response.
753
737
///
754
738
/// # Context: Tracker
755
- pub async fn update_torrent_with_peer_and_get_stats ( & self , info_hash : & InfoHash , peer : & peer:: Peer ) -> torrent:: SwarmStats {
756
- // code-review: consider splitting the function in two (command and query segregation).
757
- // `update_torrent_with_peer` and `get_stats`
758
-
759
- let ( stats, stats_updated) = self . torrents . update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ;
739
+ pub async fn inner_announce ( & self , info_hash : & InfoHash , peer : & peer:: Peer ) -> ( torrent:: SwarmStats , Vec < Peer > ) {
740
+ let ( stats, stats_updated, peers) = self . torrents . announce ( info_hash, peer) . await ;
760
741
761
742
if self . policy . persistent_torrent_completed_stat && stats_updated {
762
743
let completed = stats. downloaded ;
@@ -765,7 +746,7 @@ impl Tracker {
765
746
drop ( self . database . save_persistent_torrent ( & info_hash, completed) . await ) ;
766
747
}
767
748
768
- stats
749
+ ( stats, peers )
769
750
}
770
751
771
752
/// It calculates and returns the general `Tracker`
@@ -1228,7 +1209,7 @@ mod tests {
1228
1209
let info_hash = sample_info_hash ( ) ;
1229
1210
let peer = sample_peer ( ) ;
1230
1211
1231
- tracker. update_torrent_with_peer_and_get_stats ( & info_hash, & peer) . await ;
1212
+ tracker. inner_announce ( & info_hash, & peer) . await ;
1232
1213
1233
1214
let peers = tracker. get_torrent_peers ( & info_hash) . await ;
1234
1215
@@ -1242,9 +1223,7 @@ mod tests {
1242
1223
let info_hash = sample_info_hash ( ) ;
1243
1224
let peer = sample_peer ( ) ;
1244
1225
1245
- tracker. update_torrent_with_peer_and_get_stats ( & info_hash, & peer) . await ;
1246
-
1247
- let peers = tracker. get_torrent_peers_for_peer ( & info_hash, & peer) . await ;
1226
+ let ( _stats, peers) = tracker. inner_announce ( & info_hash, & peer) . await ;
1248
1227
1249
1228
assert_eq ! ( peers, vec![ ] ) ;
1250
1229
}
@@ -1253,9 +1232,7 @@ mod tests {
1253
1232
async fn it_should_return_the_torrent_metrics ( ) {
1254
1233
let tracker = public_tracker ( ) ;
1255
1234
1256
- tracker
1257
- . update_torrent_with_peer_and_get_stats ( & sample_info_hash ( ) , & leecher ( ) )
1258
- . await ;
1235
+ tracker. inner_announce ( & sample_info_hash ( ) , & leecher ( ) ) . await ;
1259
1236
1260
1237
let torrent_metrics = tracker. get_torrents_metrics ( ) . await ;
1261
1238
@@ -1764,11 +1741,11 @@ mod tests {
1764
1741
let mut peer = sample_peer ( ) ;
1765
1742
1766
1743
peer. event = AnnounceEvent :: Started ;
1767
- let swarm_stats = tracker. update_torrent_with_peer_and_get_stats ( & info_hash, & peer) . await ;
1744
+ let ( swarm_stats, _peers ) = tracker. inner_announce ( & info_hash, & peer) . await ;
1768
1745
assert_eq ! ( swarm_stats. downloaded, 0 ) ;
1769
1746
1770
1747
peer. event = AnnounceEvent :: Completed ;
1771
- let swarm_stats = tracker. update_torrent_with_peer_and_get_stats ( & info_hash, & peer) . await ;
1748
+ let ( swarm_stats, _peers ) = tracker. inner_announce ( & info_hash, & peer) . await ;
1772
1749
assert_eq ! ( swarm_stats. downloaded, 1 ) ;
1773
1750
1774
1751
// Remove the newly updated torrent from memory
0 commit comments