File tree Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 11use crate :: error:: ErrorContext ;
2+ use crate :: utils:: sleep;
23use crate :: utils:: timeout_op;
34use crate :: wallet:: BoardingWallet ;
45use crate :: wallet:: OnchainWallet ;
@@ -343,6 +344,50 @@ where
343344 server_info,
344345 } )
345346 }
347+
348+ /// Connects to the Ark server and retrieves server information.
349+ ///
350+ /// If it encounters errors, it will retry `max_retries`.
351+ ///
352+ /// # Errors
353+ ///
354+ /// Returns an error if the connection fails or times out.
355+ pub async fn connect_with_retries ( mut self , max_retries : usize ) -> Result < Client < B , W > , Error > {
356+ let mut n_retries = 0 ;
357+ while n_retries < max_retries {
358+ let res = timeout_op ( self . timeout , self . network_client . connect ( ) )
359+ . await
360+ . context ( "Failed to connect to Ark server" ) ?;
361+
362+ match res {
363+ Ok ( ( ) ) => break ,
364+ Err ( error) => {
365+ tracing:: warn!( ?error, "Failed to connect to Ark server, retrying" ) ;
366+
367+ sleep ( Duration :: from_secs ( 2 ) ) . await ;
368+
369+ n_retries += 1 ;
370+
371+ continue ;
372+ }
373+ } ;
374+ }
375+
376+ let server_info = timeout_op ( self . timeout , self . network_client . get_info ( ) )
377+ . await
378+ . context ( "Failed to get Ark server info" ) ??;
379+
380+ tracing:: debug!(
381+ name = self . name,
382+ ark_server_url = ?self . network_client,
383+ "Connected to Ark server"
384+ ) ;
385+
386+ Ok ( Client {
387+ inner : self ,
388+ server_info,
389+ } )
390+ }
346391}
347392
348393impl < B , W > Client < B , W >
Original file line number Diff line number Diff line change @@ -369,7 +369,7 @@ pub async fn set_up_client(
369369 "http://localhost:7070" . to_string ( ) ,
370370 Duration :: from_secs ( 30 ) ,
371371 )
372- . connect ( )
372+ . connect_with_retries ( 5 )
373373 . await
374374 . unwrap ( ) ;
375375
You can’t perform that action at this time.
0 commit comments