1
- use eyre:: Result ;
1
+ use eyre:: { eyre , Result } ;
2
2
use std:: time:: Duration ;
3
3
use tokio_util:: sync:: CancellationToken ;
4
4
5
- use crate :: { node:: PingpongHandler , reqres :: TaskResponder , utils:: DriaMessage , DriaComputeNode } ;
5
+ use crate :: { node:: PingpongHandler , utils:: DriaMessage , DriaComputeNode } ;
6
6
7
7
impl DriaComputeNode {
8
8
/// Runs the main loop of the compute node.
@@ -27,72 +27,45 @@ impl DriaComputeNode {
27
27
28
28
loop {
29
29
tokio:: select! {
30
- // a Workflow message to be published is received from the channel
31
- // this is expected to be sent by the workflow worker
32
- publish_msg_opt = self . publish_rx. recv( ) => {
33
- if let Some ( publish_msg) = publish_msg_opt {
34
- // remove the task from pending tasks based on its batchability
35
- let task_metadata = match publish_msg. batchable {
36
- true => {
37
- self . completed_tasks_batch += 1 ;
38
- self . pending_tasks_batch. remove( & publish_msg. task_id)
39
- } ,
40
- false => {
41
- self . completed_tasks_single += 1 ;
42
- self . pending_tasks_single. remove( & publish_msg. task_id)
43
- }
44
- } ;
45
-
46
- // respond to the request
47
- match task_metadata {
48
- Some ( channel) => {
49
- TaskResponder :: handle_respond( self , publish_msg, channel) . await ?;
50
- }
51
- None => log:: error!( "Channel not found for task id: {}" , publish_msg. task_id) ,
52
- }
53
- } else {
54
- log:: error!( "Publish channel closed unexpectedly, we still have {} batch and {} single tasks." , self . pending_tasks_batch. len( ) , self . pending_tasks_single. len( ) ) ;
55
- break ;
56
- } ;
30
+ // a task is completed by the worker & should be responded to the requesting peer
31
+ task_response_msg_opt = self . task_output_rx. recv( ) => {
32
+ let task_response_msg = task_response_msg_opt. ok_or(
33
+ eyre!( "Publish channel closed unexpectedly, we still have {} batch and {} single tasks." , self . pending_tasks_batch. len( ) , self . pending_tasks_single. len( ) )
34
+ ) ?; {
35
+ self . handle_task_response( task_response_msg) . await ?;
36
+ }
57
37
} ,
58
38
59
- // check peer count every now and then
60
- _ = diagnostic_refresh_interval. tick( ) => self . handle_diagnostic_refresh( ) . await ,
61
-
62
- // available nodes are refreshed every now and then
63
- _ = available_node_refresh_interval. tick( ) => self . handle_available_nodes_refresh( ) . await ,
64
-
65
39
// a GossipSub message is received from the channel
66
40
// this is expected to be sent by the p2p client
67
- gossipsub_msg_opt = self . message_rx. recv( ) => {
68
- if let Some ( ( peer_id, message_id, message) ) = gossipsub_msg_opt {
69
- // handle the message, returning a message acceptance for the received one
70
- let acceptance = self . handle_message( ( peer_id, & message_id, message) ) . await ;
71
-
72
- // validate the message based on the acceptance
73
- // cant do anything but log if this gives an error as well
74
- if let Err ( e) = self . p2p. validate_message( & message_id, & peer_id, acceptance) . await {
75
- log:: error!( "Error validating message {}: {:?}" , message_id, e) ;
76
- }
77
- } else {
78
- log:: error!( "message_rx channel closed unexpectedly." ) ;
79
- break ;
80
- } ;
41
+ gossipsub_msg_opt = self . gossip_message_rx. recv( ) => {
42
+ let ( peer_id, message_id, message) = gossipsub_msg_opt. ok_or( eyre!( "message_rx channel closed unexpectedly." ) ) ?;
43
+
44
+ // handle the message, returning a message acceptance for the received one
45
+ let acceptance = self . handle_message( ( peer_id, & message_id, message) ) . await ;
46
+
47
+ // validate the message based on the acceptance
48
+ // cant do anything but log if this gives an error as well
49
+ if let Err ( e) = self . p2p. validate_message( & message_id, & peer_id, acceptance) . await {
50
+ log:: error!( "Error validating message {}: {:?}" , message_id, e) ;
51
+ }
52
+
81
53
} ,
82
54
83
- // a Response message is received from the channel
84
- // this is expected to be sent by the p2p client
55
+ // a Request is received from the channel, sent by p2p client
85
56
request_msg_opt = self . request_rx. recv( ) => {
86
- if let Some ( ( peer_id, data, channel) ) = request_msg_opt {
87
- if let Err ( e) = self . handle_request( ( peer_id, data, channel) ) . await {
88
- log:: error!( "Error handling request: {:?}" , e) ;
89
- }
90
- } else {
91
- log:: error!( "request_rx channel closed unexpectedly." ) ;
92
- break ;
93
- } ;
57
+ let request = request_msg_opt. ok_or( eyre!( "request_rx channel closed unexpectedly." ) ) ?;
58
+ if let Err ( e) = self . handle_request( request) . await {
59
+ log:: error!( "Error handling request: {:?}" , e) ;
60
+ }
94
61
} ,
95
62
63
+ // check peer count every now and then
64
+ _ = diagnostic_refresh_interval. tick( ) => self . handle_diagnostic_refresh( ) . await ,
65
+
66
+ // available nodes are refreshed every now and then
67
+ _ = available_node_refresh_interval. tick( ) => self . handle_available_nodes_refresh( ) . await ,
68
+
96
69
// check if the cancellation token is cancelled
97
70
// this is expected to be cancelled by the main thread with signal handling
98
71
_ = cancellation. cancelled( ) => break ,
@@ -119,15 +92,18 @@ impl DriaComputeNode {
119
92
}
120
93
121
94
/// Shutdown channels between p2p, worker and yourself.
95
+ ///
96
+ /// Can be inlined as it is called only once from very few places.
97
+ #[ inline]
122
98
pub async fn shutdown ( & mut self ) -> Result < ( ) > {
123
99
log:: debug!( "Sending shutdown command to p2p client." ) ;
124
100
self . p2p . shutdown ( ) . await ?;
125
101
126
- log:: debug!( "Closing message channel." ) ;
127
- self . message_rx . close ( ) ;
102
+ log:: debug!( "Closing gossip message receipt channel." ) ;
103
+ self . gossip_message_rx . close ( ) ;
128
104
129
- log:: debug!( "Closing publish channel." ) ;
130
- self . publish_rx . close ( ) ;
105
+ log:: debug!( "Closing task response channel." ) ;
106
+ self . task_output_rx . close ( ) ;
131
107
132
108
Ok ( ( ) )
133
109
}
0 commit comments