@@ -97,7 +97,6 @@ use bittorrent_primitives::info_hash::InfoHash;
97
97
use torrust_tracker_configuration:: { Core , TORRENT_PEERS_LIMIT } ;
98
98
use torrust_tracker_primitives:: core:: AnnounceData ;
99
99
use torrust_tracker_primitives:: peer;
100
- use torrust_tracker_primitives:: swarm_metadata:: SwarmMetadata ;
101
100
102
101
use super :: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
103
102
use super :: torrent:: repository:: persisted:: DatabasePersistentTorrentRepository ;
@@ -164,45 +163,29 @@ impl AnnounceHandler {
164
163
) -> Result < AnnounceData , AnnounceError > {
165
164
self . whitelist_authorization . authorize ( info_hash) . await ?;
166
165
167
- tracing:: debug!( "Before: {peer:?}" ) ;
168
166
peer. change_ip ( & assign_ip_address_to_peer ( remote_client_ip, self . config . net . external_ip ) ) ;
169
- tracing:: debug!( "After: {peer:?}" ) ;
170
167
171
- let stats = self . upsert_peer_and_get_stats ( info_hash, peer) ;
172
-
173
- let peers = self
174
- . in_memory_torrent_repository
175
- . get_peers_for ( info_hash, peer, peers_wanted. limit ( ) ) ;
176
-
177
- Ok ( AnnounceData {
178
- peers,
179
- stats,
180
- policy : self . config . announce_policy ,
181
- } )
182
- }
183
-
184
- /// Updates the torrent data in memory, persists statistics if needed, and
185
- /// returns the updated swarm stats.
186
- #[ must_use]
187
- fn upsert_peer_and_get_stats ( & self , info_hash : & InfoHash , peer : & peer:: Peer ) -> SwarmMetadata {
188
168
let number_of_downloads_increased = self . in_memory_torrent_repository . upsert_peer ( info_hash, peer) ;
189
169
190
- let swarm_metadata = self . in_memory_torrent_repository . get_swarm_metadata ( info_hash) ;
191
-
192
- if number_of_downloads_increased {
193
- self . persist_stats ( info_hash, & swarm_metadata) ;
170
+ if self . config . tracker_policy . persistent_torrent_completed_stat && number_of_downloads_increased {
171
+ self . db_torrent_repository . increase_number_of_downloads ( info_hash) ?;
194
172
}
195
173
196
- swarm_metadata
174
+ Ok ( self . build_announce_data ( info_hash , peer , peers_wanted ) )
197
175
}
198
176
199
- /// Persists torrent statistics to the database if persistence is enabled .
200
- fn persist_stats ( & self , info_hash : & InfoHash , swarm_metadata : & SwarmMetadata ) {
201
- if self . config . tracker_policy . persistent_torrent_completed_stat {
202
- let completed = swarm_metadata . downloaded ;
203
- let info_hash = * info_hash ;
177
+ /// Builds the announce data for the peer making the request .
178
+ fn build_announce_data ( & self , info_hash : & InfoHash , peer : & peer :: Peer , peers_wanted : & PeersWanted ) -> AnnounceData {
179
+ let peers = self
180
+ . in_memory_torrent_repository
181
+ . get_peers_for ( info_hash, peer , peers_wanted . limit ( ) ) ;
204
182
205
- drop ( self . db_torrent_repository . save ( & info_hash, completed) ) ;
183
+ let swarm_metadata = self . in_memory_torrent_repository . get_swarm_metadata ( info_hash) ;
184
+
185
+ AnnounceData {
186
+ peers,
187
+ stats : swarm_metadata,
188
+ policy : self . config . announce_policy ,
206
189
}
207
190
}
208
191
}
0 commit comments