File tree Expand file tree Collapse file tree 5 files changed +42
-6
lines changed Expand file tree Collapse file tree 5 files changed +42
-6
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,12 @@ pub struct SugondatRpcParams {
87
87
/// The address of the sugondat-node to connect to.
88
88
#[ clap( long, default_value = "ws://localhost:9988" , env = ENV_SUGONDAT_NODE_URL ) ]
89
89
pub node_url : String ,
90
+
91
+ /// By default the first connection to the node is retried until it is properly connected.
92
+ ///
93
+ /// This flag avoids this behavior by attempting to connect only once
94
+ #[ clap( long) ]
95
+ pub no_retry : bool ,
90
96
}
91
97
92
98
impl DockParams {
Original file line number Diff line number Diff line change @@ -19,5 +19,5 @@ pub async fn run(params: Params) -> anyhow::Result<()> {
19
19
async fn connect_rpc (
20
20
conn_params : crate :: cli:: SugondatRpcParams ,
21
21
) -> 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
23
23
}
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ Pass --submit-dev-alice or --submit-private-key=<..> to fix."
16
16
) ;
17
17
}
18
18
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 ?;
20
20
let methods = dock:: init ( dock:: Config {
21
21
// TODO: whenever there are more docks, the logic of checking if any at least one is enabled
22
22
// and other similar stuff should be in CLI.
@@ -30,7 +30,7 @@ Pass --submit-dev-alice or --submit-private-key=<..> to fix."
30
30
Ok ( ( ) )
31
31
}
32
32
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 ?;
35
35
Ok ( client)
36
36
}
Original file line number Diff line number Diff line change @@ -87,6 +87,31 @@ impl Connector {
87
87
}
88
88
}
89
89
90
+ /// Attempt a single connection. Returns the connection handle if successful, otherwise returns the reason for failure
91
+ pub async fn try_connect ( & self ) -> anyhow:: Result < Arc < Conn > > {
92
+ let mut state = self . state . lock ( ) . await ;
93
+ match & mut * state {
94
+ State :: Connected ( conn) => Ok ( conn. clone ( ) ) ,
95
+ State :: Connecting { .. } => Err ( anyhow:: anyhow!(
96
+ "The connector is already attempting to connect"
97
+ ) ) ,
98
+ State :: Disconnected => {
99
+ let conn_id = self . gen_conn_id ( ) ;
100
+ let rpc_url = self . rpc_url . clone ( ) ;
101
+ match Conn :: connect ( conn_id, & rpc_url) . await {
102
+ Ok ( conn) => {
103
+ * state = State :: Connected ( conn. clone ( ) ) ;
104
+ Ok ( conn)
105
+ }
106
+ Err ( e) => Err ( anyhow:: anyhow!(
107
+ "failed to connect to sugondat node: {}\n " ,
108
+ e
109
+ ) ) ,
110
+ }
111
+ }
112
+ }
113
+ }
114
+
90
115
/// Makes sure that the client is connected. Returns the connection handle.
91
116
pub async fn ensure_connected ( & self ) -> Arc < Conn > {
92
117
let mut state = self . state . lock ( ) . await ;
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ impl Client {
38
38
/// The RPC URL must be a valid URL pointing to a sugondat node. If it's not a malformed URL,
39
39
/// returns an error.
40
40
#[ 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 > {
42
42
anyhow:: ensure!(
43
43
url:: Url :: parse( & rpc_url) . is_ok( ) ,
44
44
"invalid RPC URL: {}" ,
@@ -50,7 +50,12 @@ impl Client {
50
50
let me = Self {
51
51
connector : Arc :: new ( conn:: Connector :: new ( rpc_url) ) ,
52
52
} ;
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
+
54
59
Ok ( me)
55
60
}
56
61
You can’t perform that action at this time.
0 commit comments