@@ -20,22 +20,21 @@ use crate::{
20
20
const DIAGNOSTIC_REFRESH_INTERVAL_SECS : u64 = 30 ;
21
21
/// Number of seconds between refreshing the available nodes.
22
22
const AVAILABLE_NODES_REFRESH_INTERVAL_SECS : u64 = 30 * 60 ; // 30 minutes
23
+ /// Buffer size for message publishes.
24
+ const PUBLISH_CHANNEL_BUFSIZE : usize = 1024 ;
23
25
24
26
pub struct DriaComputeNode {
25
27
pub config : DriaComputeNodeConfig ,
26
28
pub p2p : DriaP2PCommander ,
27
29
pub available_nodes : AvailableNodes ,
28
- pub cancellation : CancellationToken ,
29
30
/// Gossipsub message receiver.
30
31
message_rx : mpsc:: Receiver < ( PeerId , MessageId , Message ) > ,
32
+ /// Publish receiver to receive messages to be published.
33
+ publish_rx : mpsc:: Receiver < WorkflowsWorkerOutput > ,
31
34
/// Workflow transmitter to send batchable tasks.
32
35
workflow_batch_tx : mpsc:: Sender < WorkflowsWorkerInput > ,
33
- /// Publish receiver to receive messages to be published.
34
- publish_batch_rx : mpsc:: Receiver < WorkflowsWorkerOutput > ,
35
36
/// Workflow transmitter to send single tasks.
36
37
workflow_single_tx : mpsc:: Sender < WorkflowsWorkerInput > ,
37
- /// Publish receiver to receive messages to be published.
38
- publish_single_rx : mpsc:: Receiver < WorkflowsWorkerOutput > ,
39
38
}
40
39
41
40
impl DriaComputeNode {
@@ -44,7 +43,6 @@ impl DriaComputeNode {
44
43
/// Returns the node instance and p2p client together. P2p MUST be run in a separate task before this node is used at all.
45
44
pub async fn new (
46
45
config : DriaComputeNodeConfig ,
47
- cancellation : CancellationToken ,
48
46
) -> Result < (
49
47
DriaComputeNode ,
50
48
DriaP2PClient ,
@@ -77,22 +75,20 @@ impl DriaComputeNode {
77
75
protocol,
78
76
) ?;
79
77
80
- // create workflow workers
81
- let ( workflows_batch_worker , workflow_batch_tx , publish_batch_rx ) = WorkflowsWorker :: new ( ) ;
82
- let ( workflows_single_worker , workflow_single_tx , publish_single_rx ) =
83
- WorkflowsWorker :: new ( ) ;
78
+ // create workflow workers, all workers use the same publish channel
79
+ let ( publish_tx , publish_rx ) = mpsc :: channel ( PUBLISH_CHANNEL_BUFSIZE ) ;
80
+ let ( workflows_batch_worker , workflow_batch_tx ) = WorkflowsWorker :: new ( publish_tx . clone ( ) ) ;
81
+ let ( workflows_single_worker , workflow_single_tx ) = WorkflowsWorker :: new ( publish_tx ) ;
84
82
85
83
Ok ( (
86
84
DriaComputeNode {
87
85
config,
88
86
p2p : p2p_commander,
89
- cancellation,
90
87
available_nodes,
91
88
message_rx,
89
+ publish_rx,
92
90
workflow_batch_tx,
93
- publish_batch_rx,
94
91
workflow_single_tx,
95
- publish_single_rx,
96
92
} ,
97
93
p2p_client,
98
94
workflows_batch_worker,
@@ -248,8 +244,8 @@ impl DriaComputeNode {
248
244
}
249
245
250
246
/// Runs the main loop of the compute node.
251
- /// This method is not expected to return until cancellation occurs.
252
- pub async fn run ( & mut self ) -> Result < ( ) > {
247
+ /// This method is not expected to return until cancellation occurs for the given token .
248
+ pub async fn run ( & mut self , cancellation : CancellationToken ) -> Result < ( ) > {
253
249
// prepare durations for sleeps
254
250
let mut peer_refresh_interval =
255
251
tokio:: time:: interval ( Duration :: from_secs ( DIAGNOSTIC_REFRESH_INTERVAL_SECS ) ) ;
@@ -270,16 +266,7 @@ impl DriaComputeNode {
270
266
_ = available_node_refresh_interval. tick( ) => self . handle_available_nodes_refresh( ) . await ,
271
267
// a Workflow message to be published is received from the channel
272
268
// this is expected to be sent by the workflow worker
273
- publish_msg = self . publish_batch_rx. recv( ) => {
274
- if let Some ( result) = publish_msg {
275
- WorkflowHandler :: handle_publish( self , result) . await ?;
276
- } else {
277
- log:: error!( "Publish channel closed unexpectedly." ) ;
278
- break ;
279
- } ;
280
- } ,
281
- // TODO: make the both receivers handled together somehow
282
- publish_msg = self . publish_single_rx. recv( ) => {
269
+ publish_msg = self . publish_rx. recv( ) => {
283
270
if let Some ( result) = publish_msg {
284
271
WorkflowHandler :: handle_publish( self , result) . await ?;
285
272
} else {
@@ -306,7 +293,7 @@ impl DriaComputeNode {
306
293
} ,
307
294
// check if the cancellation token is cancelled
308
295
// this is expected to be cancelled by the main thread with signal handling
309
- _ = self . cancellation. cancelled( ) => break ,
296
+ _ = cancellation. cancelled( ) => break ,
310
297
}
311
298
}
312
299
@@ -331,7 +318,7 @@ impl DriaComputeNode {
331
318
self . message_rx . close ( ) ;
332
319
333
320
log:: debug!( "Closing publish channel." ) ;
334
- self . publish_batch_rx . close ( ) ;
321
+ self . publish_rx . close ( ) ;
335
322
336
323
Ok ( ( ) )
337
324
}
@@ -345,8 +332,8 @@ impl DriaComputeNode {
345
332
}
346
333
347
334
// print task counts
348
- let [ single, batch] = self . get_active_task_count ( ) ;
349
- log:: info!( "Active Task Count (single/batch): {} / {}" , single, batch) ;
335
+ // let [single, batch] = self.get_active_task_count();
336
+ // log::info!("Active Task Count (single/batch): {} / {}", single, batch);
350
337
}
351
338
352
339
/// Updates the local list of available nodes by refreshing it.
@@ -382,18 +369,18 @@ mod tests {
382
369
383
370
// create node
384
371
let cancellation = CancellationToken :: new ( ) ;
385
- let ( mut node, p2p, _, _) =
386
- DriaComputeNode :: new ( DriaComputeNodeConfig :: default ( ) , cancellation. clone ( ) )
387
- . await
388
- . expect ( "should create node" ) ;
372
+ let ( mut node, p2p, _, _) = DriaComputeNode :: new ( DriaComputeNodeConfig :: default ( ) )
373
+ . await
374
+ . expect ( "should create node" ) ;
389
375
390
376
// spawn p2p task
391
377
let p2p_task = tokio:: spawn ( async move { p2p. run ( ) . await } ) ;
392
378
393
379
// launch & wait for a while for connections
394
380
log:: info!( "Waiting a bit for peer setup." ) ;
381
+ let run_cancellation = cancellation. clone ( ) ;
395
382
tokio:: select! {
396
- _ = node. run( ) => ( ) ,
383
+ _ = node. run( run_cancellation ) => ( ) ,
397
384
_ = tokio:: time:: sleep( tokio:: time:: Duration :: from_secs( 20 ) ) => cancellation. cancel( ) ,
398
385
}
399
386
log:: info!( "Connected Peers:\n {:#?}" , node. peers( ) . await ?) ;
0 commit comments