@@ -38,6 +38,7 @@ pub struct BlockFetcher {
38
38
rpc : Arc < BitcoinRpc > ,
39
39
job_id : Arc < AtomicUsize > ,
40
40
sender : std:: sync:: mpsc:: SyncSender < BlockEvent > ,
41
+ num_workers : usize ,
41
42
}
42
43
43
44
pub enum BlockEvent {
@@ -304,6 +305,7 @@ impl BlockFetcher {
304
305
pub fn new (
305
306
rpc : BitcoinRpc ,
306
307
client : reqwest:: blocking:: Client ,
308
+ num_workers : usize ,
307
309
) -> ( Self , std:: sync:: mpsc:: Receiver < BlockEvent > ) {
308
310
let ( tx, rx) = std:: sync:: mpsc:: sync_channel ( 12 ) ;
309
311
(
@@ -312,6 +314,7 @@ impl BlockFetcher {
312
314
rpc : Arc :: new ( rpc) ,
313
315
job_id : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
314
316
sender : tx,
317
+ num_workers,
315
318
} ,
316
319
rx,
317
320
)
@@ -329,6 +332,7 @@ impl BlockFetcher {
329
332
let task_rpc = self . rpc . clone ( ) ;
330
333
let current_task = self . job_id . clone ( ) ;
331
334
let task_sender = self . sender . clone ( ) ;
335
+ let num_workers = self . num_workers ;
332
336
333
337
_ = std:: thread:: spawn ( move || {
334
338
let mut last_check = Instant :: now ( ) - Duration :: from_secs ( 2 ) ;
@@ -354,16 +358,14 @@ impl BlockFetcher {
354
358
} ;
355
359
356
360
if tip > start_block. height {
357
- let concurrency = std:: cmp:: min ( tip - start_block. height , 8 ) ;
358
-
359
361
let res = Self :: run_workers (
360
362
job_id,
361
363
current_task. clone ( ) ,
362
364
task_rpc. clone ( ) ,
363
365
task_sender. clone ( ) ,
364
366
start_block,
365
367
tip,
366
- concurrency as usize ,
368
+ num_workers ,
367
369
) ;
368
370
369
371
match res {
@@ -388,7 +390,7 @@ impl BlockFetcher {
388
390
sender : std:: sync:: mpsc:: SyncSender < BlockEvent > ,
389
391
start_block : ChainAnchor ,
390
392
end_height : u32 ,
391
- concurrency : usize ,
393
+ num_workers : usize ,
392
394
) -> Result < ChainAnchor , BlockFetchError > {
393
395
let mut workers = Workers {
394
396
current_job,
@@ -399,8 +401,8 @@ impl BlockFetcher {
399
401
end_height,
400
402
ordered_sender : sender,
401
403
rpc,
402
- concurrency ,
403
- pool : ThreadPool :: new ( concurrency ) ,
404
+ num_workers ,
405
+ pool : ThreadPool :: new ( num_workers ) ,
404
406
} ;
405
407
406
408
workers. run ( )
@@ -464,7 +466,7 @@ struct Workers {
464
466
end_height : u32 ,
465
467
ordered_sender : std:: sync:: mpsc:: SyncSender < BlockEvent > ,
466
468
rpc : Arc < BitcoinRpc > ,
467
- concurrency : usize ,
469
+ num_workers : usize ,
468
470
pool : ThreadPool ,
469
471
}
470
472
@@ -508,7 +510,9 @@ impl Workers {
508
510
509
511
#[ inline( always) ]
510
512
fn can_add_workers ( & self ) -> bool {
511
- self . pool . queued_count ( ) < self . concurrency && !self . queued_all ( )
513
+ self . out_of_order . len ( ) < self . num_workers
514
+ && self . pool . queued_count ( ) < self . num_workers
515
+ && !self . queued_all ( )
512
516
}
513
517
514
518
#[ inline( always) ]
@@ -518,7 +522,7 @@ impl Workers {
518
522
519
523
fn run ( & mut self ) -> Result < ChainAnchor , BlockFetchError > {
520
524
let client = reqwest:: blocking:: Client :: new ( ) ;
521
- let ( tx, rx) = std:: sync:: mpsc:: sync_channel ( self . concurrency ) ;
525
+ let ( tx, rx) = std:: sync:: mpsc:: sync_channel ( self . num_workers ) ;
522
526
523
527
' queue_blocks: while !self . queued_all ( ) {
524
528
if self . should_stop ( ) {
@@ -559,15 +563,15 @@ impl Workers {
559
563
}
560
564
}
561
565
562
- std:: thread:: sleep ( Duration :: from_millis ( 10 ) ) ;
566
+ std:: thread:: sleep ( Duration :: from_millis ( 1 ) ) ;
563
567
}
564
568
565
569
// Wait for all blocks to be processed
566
570
while self . pool . active_count ( ) > 0 || self . last_emitted . height != self . end_height {
567
571
while self . try_emit_next_block ( & rx) ? {
568
572
// do nothing
569
573
}
570
- std:: thread:: sleep ( Duration :: from_millis ( 10 ) ) ;
574
+ std:: thread:: sleep ( Duration :: from_millis ( 1 ) ) ;
571
575
}
572
576
Ok ( self . last_emitted )
573
577
}
@@ -753,7 +757,7 @@ mod test {
753
757
let start_block_hash: BlockHash =
754
758
rpc. send_json_blocking ( & client, & rpc. get_block_hash ( start_block) ) ?;
755
759
756
- let ( fetcher, receiver) = BlockFetcher :: new ( rpc, client) ;
760
+ let ( fetcher, receiver) = BlockFetcher :: new ( rpc, client, 8 ) ;
757
761
758
762
println ! ( "fetcher starting from block {}" , start_block) ;
759
763
0 commit comments