@@ -96,9 +96,10 @@ use std::sync::Arc;
9696use bittorrent_primitives:: info_hash:: InfoHash ;
9797use torrust_tracker_configuration:: { Core , TORRENT_PEERS_LIMIT } ;
9898use torrust_tracker_primitives:: core:: AnnounceData ;
99- use torrust_tracker_primitives:: peer;
99+ use torrust_tracker_primitives:: { peer, NumberOfDownloads } ;
100100
101101use super :: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
102+ use crate :: databases;
102103use crate :: error:: AnnounceError ;
103104use crate :: statistics:: persisted:: downloads:: DatabaseDownloadsMetricRepository ;
104105use crate :: whitelist:: authorization:: WhitelistAuthorization ;
@@ -163,21 +164,28 @@ impl AnnounceHandler {
163164 ) -> Result < AnnounceData , AnnounceError > {
164165 self . whitelist_authorization . authorize ( info_hash) . await ?;
165166
166- let opt_persistent_torrent = if self . config . tracker_policy . persistent_torrent_completed_stat {
167- self . db_downloads_metric_repository . load_torrent_downloads ( info_hash) ?
168- } else {
169- None
170- } ;
171-
172167 peer. change_ip ( & assign_ip_address_to_peer ( remote_client_ip, self . config . net . external_ip ) ) ;
173168
174169 self . in_memory_torrent_repository
175- . handle_announcement ( info_hash, peer, opt_persistent_torrent )
170+ . handle_announcement ( info_hash, peer, self . load_downloads_metric_if_needed ( info_hash ) ? )
176171 . await ;
177172
178173 Ok ( self . build_announce_data ( info_hash, peer, peers_wanted) . await )
179174 }
180175
176+ /// Loads the number of downloads for a torrent if needed.
177+ fn load_downloads_metric_if_needed (
178+ & self ,
179+ info_hash : & InfoHash ,
180+ ) -> Result < Option < NumberOfDownloads > , databases:: error:: Error > {
181+ if self . config . tracker_policy . persistent_torrent_completed_stat && !self . in_memory_torrent_repository . contains ( info_hash)
182+ {
183+ Ok ( self . db_downloads_metric_repository . load_torrent_downloads ( info_hash) ?)
184+ } else {
185+ Ok ( None )
186+ }
187+ }
188+
181189 /// Builds the announce data for the peer making the request.
182190 async fn build_announce_data ( & self , info_hash : & InfoHash , peer : & peer:: Peer , peers_wanted : & PeersWanted ) -> AnnounceData {
183191 let peers = self
0 commit comments