@@ -140,7 +140,7 @@ pub enum GetOutput<N> {
140
140
}
141
141
142
142
/// Concurrency limits for the [`Downloader`].
143
- #[ derive( Debug ) ]
143
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
144
144
pub struct ConcurrencyLimits {
145
145
/// Maximum number of requests the service performs concurrently.
146
146
pub max_concurrent_requests : usize ,
@@ -192,7 +192,7 @@ impl ConcurrencyLimits {
192
192
}
193
193
194
194
/// Configuration for retry behavior of the [`Downloader`].
195
- #[ derive( Debug ) ]
195
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
196
196
pub struct RetryConfig {
197
197
/// Maximum number of retry attempts for a node that failed to dial or failed with IO errors.
198
198
pub max_retries_per_node : u32 ,
@@ -324,13 +324,29 @@ impl Future for DownloadHandle {
324
324
}
325
325
}
326
326
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
+
327
336
/// Handle for the download services.
328
- #[ derive( Clone , Debug ) ]
337
+ #[ derive( Debug , Clone ) ]
329
338
pub struct Downloader {
339
+ inner : Arc < Inner > ,
340
+ }
341
+
342
+ #[ derive( Debug ) ]
343
+ struct Inner {
330
344
/// Next id to use for a download intent.
331
- next_id : Arc < AtomicU64 > ,
345
+ next_id : AtomicU64 ,
332
346
/// Channel to communicate with the service.
333
347
msg_tx : mpsc:: Sender < Message > ,
348
+ /// Configuration for the downloader.
349
+ config : Arc < Config > ,
334
350
metrics : Arc < Metrics > ,
335
351
}
336
352
@@ -340,54 +356,48 @@ impl Downloader {
340
356
where
341
357
S : Store ,
342
358
{
343
- Self :: with_config ( store, endpoint, rt, Default :: default ( ) , Default :: default ( ) )
359
+ Self :: with_config ( store, endpoint, rt, Default :: default ( ) )
344
360
}
345
361
346
362
/// 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
354
364
where
355
365
S : Store ,
356
366
{
357
367
let metrics = Arc :: new ( Metrics :: default ( ) ) ;
368
+ let metrics2 = metrics. clone ( ) ;
358
369
let me = endpoint. node_id ( ) . fmt_short ( ) ;
359
370
let ( msg_tx, msg_rx) = mpsc:: channel ( SERVICE_CHANNEL_CAPACITY ) ;
360
371
let dialer = Dialer :: new ( endpoint) ;
361
-
362
- let metrics_clone = metrics . clone ( ) ;
372
+ let config = Arc :: new ( config ) ;
373
+ let config2 = config . clone ( ) ;
363
374
let create_future = move || {
364
375
let getter = get:: IoGetter {
365
376
store : store. clone ( ) ,
366
377
} ;
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) ;
377
379
service. run ( ) . instrument ( error_span ! ( "downloader" , %me) )
378
380
} ;
379
381
rt. spawn_detached ( create_future) ;
380
382
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
+ } ) ,
384
389
}
385
390
}
386
391
392
+ /// Get the current configuration.
393
+ pub fn config ( & self ) -> & Config {
394
+ & self . inner . config
395
+ }
396
+
387
397
/// Queue a download.
388
398
pub async fn queue ( & self , request : DownloadRequest ) -> DownloadHandle {
389
399
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 ) ) ;
391
401
let ( sender, receiver) = oneshot:: channel ( ) ;
392
402
let handle = DownloadHandle {
393
403
id : intent_id,
@@ -401,7 +411,7 @@ impl Downloader {
401
411
} ;
402
412
// if this fails polling the handle will fail as well since the sender side of the oneshot
403
413
// 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 {
405
415
let msg = send_err. 0 ;
406
416
debug ! ( ?msg, "download not sent" ) ;
407
417
}
@@ -417,7 +427,7 @@ impl Downloader {
417
427
receiver : _,
418
428
} = handle;
419
429
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 {
421
431
let msg = send_err. 0 ;
422
432
debug ! ( ?msg, "cancel not sent" ) ;
423
433
}
@@ -429,15 +439,15 @@ impl Downloader {
429
439
/// downloads. Use [`Self::queue`] to queue a download.
430
440
pub async fn nodes_have ( & mut self , hash : Hash , nodes : Vec < NodeId > ) {
431
441
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 {
433
443
let msg = send_err. 0 ;
434
444
debug ! ( ?msg, "nodes have not been sent" )
435
445
}
436
446
}
437
447
438
448
/// Returns the metrics collected for this downloader.
439
449
pub fn metrics ( & self ) -> & Arc < Metrics > {
440
- & self . metrics
450
+ & self . inner . metrics
441
451
}
442
452
}
443
453
@@ -586,17 +596,16 @@ impl<G: Getter<Connection = D::Connection>, D: DialerT> Service<G, D> {
586
596
fn new (
587
597
getter : G ,
588
598
dialer : D ,
589
- concurrency_limits : ConcurrencyLimits ,
590
- retry_config : RetryConfig ,
599
+ config : Arc < Config > ,
591
600
msg_rx : mpsc:: Receiver < Message > ,
592
601
metrics : Arc < Metrics > ,
593
602
) -> Self {
594
603
Service {
595
604
getter,
596
605
dialer,
597
606
msg_rx,
598
- concurrency_limits,
599
- retry_config,
607
+ concurrency_limits : config . concurrency ,
608
+ retry_config : config . retry ,
600
609
connected_nodes : Default :: default ( ) ,
601
610
retry_node_state : Default :: default ( ) ,
602
611
providers : Default :: default ( ) ,
0 commit comments