Skip to content

Commit 3fe51dd

Browse files
committed
Connect to microgrid API lazily so startup tolerates absent server
Replace the eager Channel::connect call in MicrogridClientHandle::try_new with Endpoint::connect_lazy, so a missing server at startup no longer causes the calling app to exit immediately. Connection errors now surface per-RPC, where the actor's existing per-stream retry loop can recover once the server becomes reachable. Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent e38b5dd commit 3fe51dd

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

src/client/microgrid_client_handle.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
use chrono::TimeDelta;
1010
use tokio::sync::{broadcast, mpsc, oneshot};
11-
use tonic::transport::Channel;
11+
use tonic::transport::{Channel, Endpoint};
1212

1313
use crate::{
1414
Bounds, Error,
@@ -36,20 +36,25 @@ pub struct MicrogridClientHandle {
3636
}
3737

3838
impl MicrogridClientHandle {
39-
/// Creates a new `MicrogridClientHandle` that connects to the microgrid API
40-
/// at the specified URL.
39+
/// Creates a new `MicrogridClientHandle` for the microgrid API at the
40+
/// specified URL.
41+
///
42+
/// The connection is established lazily on the first RPC, so this method
43+
/// succeeds even when no server is reachable yet. Per-call errors will
44+
/// surface from the individual RPC methods, and the actor's per-stream
45+
/// retry loop will keep attempting to reconnect telemetry streams.
46+
///
47+
/// Returns an error only if `url` is not a valid endpoint URL.
4148
pub async fn try_new(url: impl Into<String>) -> Result<Self, Error> {
42-
let client = match MicrogridClient::<Channel>::connect(url.into()).await {
43-
Ok(t) => t,
44-
Err(e) => {
45-
tracing::error!("Could not connect to server: {e}");
46-
return Err(Error::connection_failure(format!(
47-
"Could not connect to server: {e}"
48-
)));
49-
}
50-
};
51-
52-
Ok(Self::new_from_client(client))
49+
let url = url.into();
50+
let channel = Endpoint::from_shared(url.clone())
51+
.map_err(|e| {
52+
Error::connection_failure(format!("Invalid microgrid API URL {url}: {e}"))
53+
})?
54+
.connect_lazy();
55+
Ok(Self::new_from_client(MicrogridClient::<Channel>::new(
56+
channel,
57+
)))
5358
}
5459

5560
pub fn new_from_client(client: impl MicrogridApiClient) -> Self {

src/microgrid.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ impl Microgrid {
2121
/// Creates a new `Microgrid` instance with the given microgrid API URL and
2222
/// logical meter configuration.
2323
///
24-
/// Returns an error if the URL is unreachable, or if the component graph
25-
/// cannot be created with the given configuration.
24+
/// The microgrid API connection is established lazily and connection or
25+
/// component-graph build errors during setup are retried indefinitely, so
26+
/// this call blocks until the server is reachable and returns valid data.
27+
/// Returns an error only if the URL is malformed or if the provided
28+
/// logical meter configuration is invalid.
2629
pub async fn try_new(
2730
url: impl Into<String>,
2831
config: LogicalMeterConfig,

0 commit comments

Comments
 (0)