3838//! checks the queues to see if there are more parcels of work that can be spawned in a new worker
3939//! task.
4040
41+ use crate :: rayon_manager:: RayonManager ;
4142use crate :: work_reprocessing_queue:: {
4243 QueuedBackfillBatch , QueuedColumnReconstruction , QueuedGossipBlock , ReprocessQueueMessage ,
4344} ;
@@ -47,6 +48,7 @@ use lighthouse_network::{MessageId, NetworkGlobals, PeerId};
4748use logging:: TimeLatch ;
4849use logging:: crit;
4950use parking_lot:: Mutex ;
51+ use rayon:: ThreadPool ;
5052pub use scheduler:: work_reprocessing_queue;
5153use serde:: { Deserialize , Serialize } ;
5254use slot_clock:: SlotClock ;
@@ -74,6 +76,7 @@ use work_reprocessing_queue::{
7476} ;
7577
7678mod metrics;
79+ pub mod rayon_manager;
7780pub mod scheduler;
7881
7982/// The maximum size of the channel for work events to the `BeaconProcessor`.
@@ -603,7 +606,7 @@ pub enum Work<E: EthSpec> {
603606 process_fn : BlockingFn ,
604607 } ,
605608 ChainSegment ( AsyncFn ) ,
606- ChainSegmentBackfill ( AsyncFn ) ,
609+ ChainSegmentBackfill ( BlockingFn ) ,
607610 Status ( BlockingFn ) ,
608611 BlocksByRangeRequest ( AsyncFn ) ,
609612 BlocksByRootsRequest ( AsyncFn ) ,
@@ -807,6 +810,7 @@ pub struct BeaconProcessor<E: EthSpec> {
807810 pub network_globals : Arc < NetworkGlobals < E > > ,
808811 pub executor : TaskExecutor ,
809812 pub current_workers : usize ,
813+ pub rayon_manager : RayonManager ,
810814 pub config : BeaconProcessorConfig ,
811815}
812816
@@ -1603,7 +1607,17 @@ impl<E: EthSpec> BeaconProcessor<E> {
16031607 Work :: BlocksByRangeRequest ( work) | Work :: BlocksByRootsRequest ( work) => {
16041608 task_spawner. spawn_async ( work)
16051609 }
1606- Work :: ChainSegmentBackfill ( process_fn) => task_spawner. spawn_async ( process_fn) ,
1610+ Work :: ChainSegmentBackfill ( process_fn) => {
1611+ if self . config . enable_backfill_rate_limiting {
1612+ task_spawner. spawn_blocking_with_rayon (
1613+ self . rayon_manager . low_priority_threadpool . clone ( ) ,
1614+ process_fn,
1615+ )
1616+ } else {
1617+ // use the global rayon thread pool if backfill rate limiting is disabled.
1618+ task_spawner. spawn_blocking ( process_fn)
1619+ }
1620+ }
16071621 Work :: ApiRequestP0 ( process_fn) | Work :: ApiRequestP1 ( process_fn) => match process_fn {
16081622 BlockingOrAsync :: Blocking ( process_fn) => task_spawner. spawn_blocking ( process_fn) ,
16091623 BlockingOrAsync :: Async ( process_fn) => task_spawner. spawn_async ( process_fn) ,
@@ -1665,6 +1679,22 @@ impl TaskSpawner {
16651679 WORKER_TASK_NAME ,
16661680 )
16671681 }
1682+
1683+ /// Spawns a blocking task on a rayon thread pool, dropping the `SendOnDrop` after task completion.
1684+ fn spawn_blocking_with_rayon < F > ( self , thread_pool : Arc < ThreadPool > , task : F )
1685+ where
1686+ F : FnOnce ( ) + Send + ' static ,
1687+ {
1688+ self . executor . spawn_blocking (
1689+ move || {
1690+ thread_pool. install ( || {
1691+ task ( ) ;
1692+ } ) ;
1693+ drop ( self . send_idle_on_drop )
1694+ } ,
1695+ WORKER_TASK_NAME ,
1696+ )
1697+ }
16681698}
16691699
16701700/// This struct will send a message on `self.tx` when it is dropped. An error will be logged
0 commit comments