Skip to content

Commit f72932d

Browse files
committed
shim: add no_retry flag
implement Connector::try_connect to handle a single connetion attempt
1 parent 6105a44 commit f72932d

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

sugondat/shim/src/cli.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ pub struct SugondatRpcParams {
8787
/// The address of the sugondat-node to connect to.
8888
#[clap(long, default_value = "ws://localhost:9988", env = ENV_SUGONDAT_NODE_URL)]
8989
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,
9096
}
9197

9298
impl DockParams {

sugondat/shim/src/cmd/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ pub async fn run(params: Params) -> anyhow::Result<()> {
1919
async 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
}

sugondat/shim/src/cmd/serve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff 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
}

sugondat/shim/src/sugondat_rpc/conn.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,31 @@ impl Connector {
8787
}
8888
}
8989

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+
90115
/// Makes sure that the client is connected. Returns the connection handle.
91116
pub async fn ensure_connected(&self) -> Arc<Conn> {
92117
let mut state = self.state.lock().await;

sugondat/shim/src/sugondat_rpc/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)