@@ -50,6 +50,7 @@ mod reask_reciprocity;
5050mod requested_block;
5151mod salvage;
5252mod shared_catalog;
53+ mod source_exchange;
5354mod store;
5455mod transfer_sql;
5556mod upload;
@@ -76,6 +77,7 @@ pub use model::{
7677 Ed2kCallbackIntent , Ed2kLocalIngestSummary , Ed2kPieceState , Ed2kResumeManifest , Ed2kSourceHint ,
7778 Ed2kTransferJob , Ed2kTransferState ,
7879} ;
80+ use source_exchange:: SourceExchangeState ;
7981use upload_queue:: DEFAULT_SOFT_QUEUE_SIZE ;
8082use upload_queue:: Ed2kUploadQueueState ;
8183pub ( crate ) use upload_queue:: {
@@ -140,16 +142,6 @@ pub fn ed2k_part_count(file_size: u64) -> u16 {
140142/// Canonical eMule upload block size used inside one ED2K part request.
141143pub ( crate ) const ED2K_EMBLOCK_SIZE : u64 = 184_320 ;
142144const PAYLOAD_FILE_NAME : & str = "pieces.bin" ;
143- const SOURCE_EXCHANGE_REASK_INTERVAL : Duration = Duration :: from_secs ( 40 * 60 ) ;
144- const SOURCE_EXCHANGE_COMMON_REASK_INTERVAL : Duration = Duration :: from_secs ( 160 * 60 ) ;
145- const SOURCE_EXCHANGE_RARE_FILE : usize = 50 ;
146-
147- #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
148- struct SourceExchangeRequestKey {
149- file_hash : String ,
150- peer_addr : SocketAddr ,
151- user_hash : Option < [ u8 ; 16 ] > ,
152- }
153145
154146/// Runtime owner for ED2K transfer manifests, piece-store payloads, and the
155147/// transfer-backed shared catalog.
@@ -162,7 +154,7 @@ pub struct Ed2kTransferRuntime {
162154 manifest_io : Arc < Mutex < ( ) > > ,
163155 manifest_cache : Arc < Mutex < HashMap < String , Ed2kResumeManifest > > > ,
164156 manifest_checkpoint_state : Arc < Mutex < HashMap < String , Ed2kManifestCheckpointState > > > ,
165- source_exchange_requests : Arc < Mutex < HashMap < SourceExchangeRequestKey , Instant > > > ,
157+ source_exchange : SourceExchangeState ,
166158 /// Per-file accumulator of network-proposed AICH roots and their distinct
167159 /// proposing IPs. A network-learned root is only promoted to the
168160 /// salvage-authorizing `manifest.aich_root` once it clears the master's
@@ -295,7 +287,7 @@ impl Ed2kTransferRuntime {
295287 manifest_io : Arc :: new ( Mutex :: new ( ( ) ) ) ,
296288 manifest_cache : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
297289 manifest_checkpoint_state : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
298- source_exchange_requests : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
290+ source_exchange : SourceExchangeState :: default ( ) ,
299291 aich_root_corroboration : Arc :: new ( StdMutex :: new ( HashMap :: new ( ) ) ) ,
300292 download_activity : Arc :: new ( StdMutex :: new ( HashMap :: new ( ) ) ) ,
301293 download_sources : Arc :: new ( StdMutex :: new ( HashMap :: new ( ) ) ) ,
@@ -512,28 +504,13 @@ impl Ed2kTransferRuntime {
512504 if !self . can_engage_file_source ( current_source_count) {
513505 return false ;
514506 }
515- let reask_interval = source_exchange_reask_interval ( current_source_count) ;
516- let key = SourceExchangeRequestKey {
517- file_hash : file_hash. to_string ( ) ,
518- peer_addr,
519- user_hash,
520- } ;
521- let mut requests = self . source_exchange_requests . lock ( ) . await ;
522- let allowed = requests
523- . get ( & key)
524- . is_none_or ( |last_requested| now. duration_since ( * last_requested) > reask_interval) ;
525- if allowed {
526- requests. insert ( key, now) ;
527- }
528- allowed
507+ self . source_exchange
508+ . should_request ( file_hash, peer_addr, user_hash, current_source_count, now)
509+ . await
529510 }
530- }
531511
532- fn source_exchange_reask_interval ( current_source_count : usize ) -> Duration {
533- if current_source_count > SOURCE_EXCHANGE_RARE_FILE {
534- SOURCE_EXCHANGE_COMMON_REASK_INTERVAL
535- } else {
536- SOURCE_EXCHANGE_REASK_INTERVAL
512+ pub ( crate ) async fn note_source_exchange_answer ( & self , file_hash : & str , now : Instant ) {
513+ self . source_exchange . note_answer ( file_hash, now) . await ;
537514 }
538515}
539516
0 commit comments