@@ -35,7 +35,7 @@ pub struct RpcClient {
3535 tcp_client : Mutex < TcpClient < RpcClientHandler > > ,
3636 shared : Arc < ClientShared > ,
3737 next_request_id : AtomicU64 ,
38- timeout : Duration ,
38+ timeout : AtomicU64 ,
3939}
4040
4141impl RpcClient {
@@ -62,13 +62,18 @@ impl RpcClient {
6262 tcp_client : Mutex :: new ( tcp_client) ,
6363 shared,
6464 next_request_id : AtomicU64 :: new ( 1 ) ,
65- timeout : Duration :: from_secs ( 30 ) ,
65+ timeout : AtomicU64 :: new ( 30 * 1000 ) ,
6666 } ) )
6767 }
6868
6969 /// Set the default timeout for RPC calls.
70- pub fn set_timeout ( & mut self , timeout : Duration ) {
71- self . timeout = timeout;
70+ pub fn set_timeout ( & self , timeout : Duration ) {
71+ self . timeout
72+ . store ( timeout. as_millis ( ) as u64 , Ordering :: SeqCst ) ;
73+ }
74+
75+ fn timeout ( & self ) -> Duration {
76+ Duration :: from_millis ( self . timeout . load ( Ordering :: SeqCst ) )
7277 }
7378
7479 /// Send a request and wait for a response (blocking).
@@ -93,15 +98,18 @@ impl RpcClient {
9398 }
9499
95100 let frame = Frame :: request ( request_id, service_id, method_id, payload, false ) ;
96- {
101+ let send_result = {
97102 let client = self . tcp_client . lock ( ) . unwrap ( ) ;
98- client
99- . send ( & frame. encode ( ) )
100- . map_err ( |e| RpcError :: unavailable ( format ! ( "Send failed: {}" , e) ) ) ?;
103+ client. send ( & frame. encode ( ) )
104+ } ;
105+ if let Err ( e) = send_result {
106+ let mut pending = self . shared . pending . lock ( ) . unwrap ( ) ;
107+ pending. remove ( & request_id) ;
108+ return Err ( RpcError :: unavailable ( format ! ( "Send failed: {}" , e) ) ) ;
101109 }
102110
103111 let mut pending = self . shared . pending . lock ( ) . unwrap ( ) ;
104- let deadline = std:: time:: Instant :: now ( ) + self . timeout ;
112+ let deadline = std:: time:: Instant :: now ( ) + self . timeout ( ) ;
105113
106114 loop {
107115 if let Some ( req) = pending. get ( & request_id) {
0 commit comments