@@ -140,7 +140,7 @@ pub enum GetOutput<N> {
140140}
141141
142142/// Concurrency limits for the [`Downloader`].
143- #[ derive( Debug ) ]
143+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
144144pub struct ConcurrencyLimits {
145145 /// Maximum number of requests the service performs concurrently.
146146 pub max_concurrent_requests : usize ,
@@ -192,7 +192,7 @@ impl ConcurrencyLimits {
192192}
193193
194194/// Configuration for retry behavior of the [`Downloader`].
195- #[ derive( Debug ) ]
195+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
196196pub struct RetryConfig {
197197 /// Maximum number of retry attempts for a node that failed to dial or failed with IO errors.
198198 pub max_retries_per_node : u32 ,
@@ -324,13 +324,29 @@ impl Future for DownloadHandle {
324324 }
325325}
326326
327+ /// All numerical config options for the downloader.
328+ #[ derive( Debug , Default , Clone , Copy , PartialEq , Eq ) ]
329+ pub struct Config {
330+ /// Concurrency limits for the downloader.
331+ pub concurrency : ConcurrencyLimits ,
332+ /// Retry configuration for the downloader.
333+ pub retry : RetryConfig ,
334+ }
335+
327336/// Handle for the download services.
328- #[ derive( Clone , Debug ) ]
337+ #[ derive( Debug , Clone ) ]
329338pub struct Downloader {
339+ inner : Arc < Inner > ,
340+ }
341+
342+ #[ derive( Debug ) ]
343+ struct Inner {
330344 /// Next id to use for a download intent.
331- next_id : Arc < AtomicU64 > ,
345+ next_id : AtomicU64 ,
332346 /// Channel to communicate with the service.
333347 msg_tx : mpsc:: Sender < Message > ,
348+ /// Configuration for the downloader.
349+ config : Arc < Config > ,
334350 metrics : Arc < Metrics > ,
335351}
336352
@@ -340,54 +356,48 @@ impl Downloader {
340356 where
341357 S : Store ,
342358 {
343- Self :: with_config ( store, endpoint, rt, Default :: default ( ) , Default :: default ( ) )
359+ Self :: with_config ( store, endpoint, rt, Default :: default ( ) )
344360 }
345361
346362 /// Create a new Downloader with custom [`ConcurrencyLimits`] and [`RetryConfig`].
347- pub fn with_config < S > (
348- store : S ,
349- endpoint : Endpoint ,
350- rt : LocalPoolHandle ,
351- concurrency_limits : ConcurrencyLimits ,
352- retry_config : RetryConfig ,
353- ) -> Self
363+ pub fn with_config < S > ( store : S , endpoint : Endpoint , rt : LocalPoolHandle , config : Config ) -> Self
354364 where
355365 S : Store ,
356366 {
357367 let metrics = Arc :: new ( Metrics :: default ( ) ) ;
368+ let metrics2 = metrics. clone ( ) ;
358369 let me = endpoint. node_id ( ) . fmt_short ( ) ;
359370 let ( msg_tx, msg_rx) = mpsc:: channel ( SERVICE_CHANNEL_CAPACITY ) ;
360371 let dialer = Dialer :: new ( endpoint) ;
361-
362- let metrics_clone = metrics . clone ( ) ;
372+ let config = Arc :: new ( config ) ;
373+ let config2 = config . clone ( ) ;
363374 let create_future = move || {
364375 let getter = get:: IoGetter {
365376 store : store. clone ( ) ,
366377 } ;
367-
368- let service = Service :: new (
369- getter,
370- dialer,
371- concurrency_limits,
372- retry_config,
373- msg_rx,
374- metrics_clone,
375- ) ;
376-
378+ let service = Service :: new ( getter, dialer, config2, msg_rx, metrics2) ;
377379 service. run ( ) . instrument ( error_span ! ( "downloader" , %me) )
378380 } ;
379381 rt. spawn_detached ( create_future) ;
380382 Self {
381- next_id : Arc :: new ( AtomicU64 :: new ( 0 ) ) ,
382- msg_tx,
383- metrics,
383+ inner : Arc :: new ( Inner {
384+ next_id : AtomicU64 :: new ( 0 ) ,
385+ msg_tx,
386+ config,
387+ metrics,
388+ } ) ,
384389 }
385390 }
386391
392+ /// Get the current configuration.
393+ pub fn config ( & self ) -> & Config {
394+ & self . inner . config
395+ }
396+
387397 /// Queue a download.
388398 pub async fn queue ( & self , request : DownloadRequest ) -> DownloadHandle {
389399 let kind = request. kind ;
390- let intent_id = IntentId ( self . next_id . fetch_add ( 1 , Ordering :: SeqCst ) ) ;
400+ let intent_id = IntentId ( self . inner . next_id . fetch_add ( 1 , Ordering :: SeqCst ) ) ;
391401 let ( sender, receiver) = oneshot:: channel ( ) ;
392402 let handle = DownloadHandle {
393403 id : intent_id,
@@ -401,7 +411,7 @@ impl Downloader {
401411 } ;
402412 // if this fails polling the handle will fail as well since the sender side of the oneshot
403413 // will be dropped
404- if let Err ( send_err) = self . msg_tx . send ( msg) . await {
414+ if let Err ( send_err) = self . inner . msg_tx . send ( msg) . await {
405415 let msg = send_err. 0 ;
406416 debug ! ( ?msg, "download not sent" ) ;
407417 }
@@ -417,7 +427,7 @@ impl Downloader {
417427 receiver : _,
418428 } = handle;
419429 let msg = Message :: CancelIntent { id, kind } ;
420- if let Err ( send_err) = self . msg_tx . send ( msg) . await {
430+ if let Err ( send_err) = self . inner . msg_tx . send ( msg) . await {
421431 let msg = send_err. 0 ;
422432 debug ! ( ?msg, "cancel not sent" ) ;
423433 }
@@ -429,15 +439,15 @@ impl Downloader {
429439 /// downloads. Use [`Self::queue`] to queue a download.
430440 pub async fn nodes_have ( & mut self , hash : Hash , nodes : Vec < NodeId > ) {
431441 let msg = Message :: NodesHave { hash, nodes } ;
432- if let Err ( send_err) = self . msg_tx . send ( msg) . await {
442+ if let Err ( send_err) = self . inner . msg_tx . send ( msg) . await {
433443 let msg = send_err. 0 ;
434444 debug ! ( ?msg, "nodes have not been sent" )
435445 }
436446 }
437447
438448 /// Returns the metrics collected for this downloader.
439449 pub fn metrics ( & self ) -> & Arc < Metrics > {
440- & self . metrics
450+ & self . inner . metrics
441451 }
442452}
443453
@@ -586,17 +596,16 @@ impl<G: Getter<Connection = D::Connection>, D: DialerT> Service<G, D> {
586596 fn new (
587597 getter : G ,
588598 dialer : D ,
589- concurrency_limits : ConcurrencyLimits ,
590- retry_config : RetryConfig ,
599+ config : Arc < Config > ,
591600 msg_rx : mpsc:: Receiver < Message > ,
592601 metrics : Arc < Metrics > ,
593602 ) -> Self {
594603 Service {
595604 getter,
596605 dialer,
597606 msg_rx,
598- concurrency_limits,
599- retry_config,
607+ concurrency_limits : config . concurrency ,
608+ retry_config : config . retry ,
600609 connected_nodes : Default :: default ( ) ,
601610 retry_node_state : Default :: default ( ) ,
602611 providers : Default :: default ( ) ,
0 commit comments