@@ -16,55 +16,65 @@ pub async fn start_receive_downstream(
1616 mut recv_from_down : mpsc:: Receiver < String > ,
1717 connection_id : u32 ,
1818) -> Result < ( ) , Error < ' static > > {
19- let task_manager_clone = task_manager. clone ( ) ;
20- let handle = task:: spawn ( async move {
21- while let Some ( incoming) = recv_from_down. recv ( ) . await {
22- let incoming: Result < json_rpc:: Message , _ > = serde_json:: from_str ( & incoming) ;
23- if let Ok ( incoming) = incoming {
24- // if message is Submit Shares update difficulty management
25- if let sv1_api:: Message :: StandardRequest ( standard_req) = incoming. clone ( ) {
26- if let Ok ( Submit { .. } ) = standard_req. try_into ( ) {
27- if let Err ( e) = Downstream :: save_share ( downstream. clone ( ) ) {
28- error ! ( "{}" , e) ;
29- break ;
19+ let handle = {
20+ let task_manager = task_manager. clone ( ) ;
21+ task:: spawn ( async move {
22+ while let Some ( incoming) = recv_from_down. recv ( ) . await {
23+ let incoming: Result < json_rpc:: Message , _ > = serde_json:: from_str ( & incoming) ;
24+ if let Ok ( incoming) = incoming {
25+ // if message is Submit Shares update difficulty management
26+ if let sv1_api:: Message :: StandardRequest ( standard_req) = incoming. clone ( ) {
27+ if let Ok ( Submit { .. } ) = standard_req. try_into ( ) {
28+ if let Err ( e) = Downstream :: save_share ( downstream. clone ( ) ) {
29+ error ! ( "{}" , e) ;
30+ break ;
31+ }
3032 }
3133 }
32- }
3334
34- if let Err ( error) =
35- Downstream :: handle_incoming_sv1 ( downstream. clone ( ) , incoming) . await
36- {
37- error ! ( "Failed to handle incoming sv1 msg: {:?}" , error) ;
38- ProxyState :: update_downstream_state ( DownstreamType :: TranslatorDownstream ) ;
39- } ;
40- } else {
41- // Message received could not be converted to rpc message
42- error ! (
43- "{}" ,
44- Error :: V1Protocol ( Box :: new( sv1_api:: error:: Error :: InvalidJsonRpcMessageKind ) )
45- ) ;
46- return ;
35+ if let Err ( error) =
36+ Downstream :: handle_incoming_sv1 ( downstream. clone ( ) , incoming) . await
37+ {
38+ error ! ( "Failed to handle incoming sv1 msg: {:?}" , error) ;
39+ ProxyState :: update_downstream_state ( DownstreamType :: TranslatorDownstream ) ;
40+ } ;
41+ } else {
42+ // Message received could not be converted to rpc message
43+ error ! (
44+ "{}" ,
45+ Error :: V1Protocol ( Box :: new(
46+ sv1_api:: error:: Error :: InvalidJsonRpcMessageKind
47+ ) )
48+ ) ;
49+ return ;
50+ }
4751 }
48- }
49- if let Ok ( stats_sender) = downstream. safe_lock ( |d| d. stats_sender . clone ( ) ) {
50- stats_sender. remove_stats ( connection_id) ;
51- }
52- // No message to receive
53- warn ! (
54- "Downstream: Shutting down sv1 downstream reader {}" ,
55- connection_id
56- ) ;
52+ if let Ok ( stats_sender) = downstream. safe_lock ( |d| d. stats_sender . clone ( ) ) {
53+ stats_sender. remove_stats ( connection_id) ;
54+ }
55+ // No message to receive
56+ warn ! (
57+ "Downstream: Shutting down sv1 downstream reader {}" ,
58+ connection_id
59+ ) ;
5760
58- if let Err ( e) = Downstream :: remove_downstream_hashrate_from_channel ( & downstream) {
59- error ! ( "Failed to remove downstream hashrate from channel: {}" , e)
60- } ;
61- if task_manager_clone
62- . safe_lock ( |tm| tm. abort_tasks_for_connection_id ( connection_id) )
63- . is_err ( )
64- {
65- error ! ( "TaskManager mutex poisoned" )
66- } ;
67- } ) ;
61+ if let Err ( e) = Downstream :: remove_downstream_hashrate_from_channel ( & downstream) {
62+ error ! ( "Failed to remove downstream hashrate from channel: {}" , e)
63+ } ;
64+ // Apparently there is no way to make the compiler happy without unwrapping here. But
65+ // is not an issue since:
66+ // 1. the mutex should never get poisioned and if it does will be very very rare
67+ // 2. restarting the process after the unwrapping or restarting the all the tasks from
68+ // inside the process (that is what we should do here) is almost the same thing
69+ let send_kill_signal = task_manager
70+ . safe_lock ( |tm| tm. send_kill_signal . clone ( ) )
71+ . unwrap ( ) ;
72+ if send_kill_signal. send ( connection_id) . await . is_err ( ) {
73+ error ! ( "Proxy can not abort downstreams tasks" ) ;
74+ ProxyState :: update_inconsistency ( Some ( 1 ) ) ;
75+ }
76+ } )
77+ } ;
6878 TaskManager :: add_receive_downstream ( task_manager, handle. into ( ) , connection_id)
6979 . await
7080 . map_err ( |_| Error :: TranslatorTaskManagerFailed )
0 commit comments