File tree Expand file tree Collapse file tree 4 files changed +34
-6
lines changed Expand file tree Collapse file tree 4 files changed +34
-6
lines changed Original file line number Diff line number Diff line change @@ -19,5 +19,5 @@ pub async fn run(params: Params) -> anyhow::Result<()> {
1919async fn connect_rpc (
2020 conn_params : crate :: cli:: SugondatRpcParams ,
2121) -> anyhow:: Result < sugondat_rpc:: Client > {
22- sugondat_rpc:: Client :: new ( conn_params. node_url ) . await
22+ sugondat_rpc:: Client :: new ( conn_params. node_url , conn_params . no_retry ) . await
2323}
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ Pass --submit-dev-alice or --submit-private-key=<..> to fix."
1616 ) ;
1717 }
1818 let server = Server :: builder ( ) . build ( listen_on) . await ?;
19- let client = connect_client ( & params. rpc . node_url ) . await ?;
19+ let client = connect_client ( & params. rpc . node_url , params . rpc . no_retry ) . await ?;
2020 let methods = dock:: init ( dock:: Config {
2121 // TODO: whenever there are more docks, the logic of checking if any at least one is enabled
2222 // and other similar stuff should be in CLI.
@@ -30,7 +30,7 @@ Pass --submit-dev-alice or --submit-private-key=<..> to fix."
3030 Ok ( ( ) )
3131}
3232
33- async fn connect_client ( url : & str ) -> anyhow:: Result < Client > {
34- let client = Client :: new ( url. to_string ( ) ) . await ?;
33+ async fn connect_client ( url : & str , no_retry : bool ) -> anyhow:: Result < Client > {
34+ let client = Client :: new ( url. to_string ( ) , no_retry ) . await ?;
3535 Ok ( client)
3636}
Original file line number Diff line number Diff line change @@ -87,6 +87,29 @@ impl Connector {
8787 }
8888 }
8989
90+ pub async fn try_connect ( & self ) -> anyhow:: Result < Arc < Conn > > {
91+ let mut state = self . state . lock ( ) . await ;
92+ match & mut * state {
93+ State :: Connected ( _) => Err ( anyhow:: anyhow!( "Client already Connected" ) ) ,
94+ State :: Connecting { .. } => Err ( anyhow:: anyhow!( "Client already Connecting" ) ) ,
95+ State :: Disconnected => {
96+ // We are the first to connect.
97+ let conn_id = self . gen_conn_id ( ) ;
98+ let rpc_url = self . rpc_url . clone ( ) ;
99+ match Conn :: connect ( conn_id, & rpc_url) . await {
100+ Ok ( conn) => {
101+ * state = State :: Connected ( conn. clone ( ) ) ;
102+ Ok ( conn)
103+ }
104+ Err ( e) => Err ( anyhow:: anyhow!(
105+ "failed to connect to sugondat node: {}\n " ,
106+ e
107+ ) ) ,
108+ }
109+ }
110+ }
111+ }
112+
90113 /// Makes sure that the client is connected. Returns the connection handle.
91114 pub async fn ensure_connected ( & self ) -> Arc < Conn > {
92115 let mut state = self . state . lock ( ) . await ;
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ impl Client {
3838 /// The RPC URL must be a valid URL pointing to a sugondat node. If it's not a malformed URL,
3939 /// returns an error.
4040 #[ tracing:: instrument( level = Level :: DEBUG ) ]
41- pub async fn new ( rpc_url : String ) -> anyhow:: Result < Self > {
41+ pub async fn new ( rpc_url : String , no_retry : bool ) -> anyhow:: Result < Self > {
4242 anyhow:: ensure!(
4343 url:: Url :: parse( & rpc_url) . is_ok( ) ,
4444 "invalid RPC URL: {}" ,
@@ -50,7 +50,12 @@ impl Client {
5050 let me = Self {
5151 connector : Arc :: new ( conn:: Connector :: new ( rpc_url) ) ,
5252 } ;
53- me. connector . ensure_connected ( ) . await ;
53+
54+ match no_retry {
55+ true => me. connector . try_connect ( ) . await ?,
56+ false => me. connector . ensure_connected ( ) . await ,
57+ } ;
58+
5459 Ok ( me)
5560 }
5661
You can’t perform that action at this time.
0 commit comments