Skip to content

Commit e86e915

Browse files
Return Result when creating DaprHttpServer
Especially when running custom docker setups, the container that contains the sidecar may not be running exactly when the Rust code starts running. To fix this, before we needed to sleep(2s) to avoid a panic in the Rust program. With this patch, this can be handled on the user side (e.g. the connection can be retried multiple times with timeouts in-between). Signed-off-by: Leon Matthes <[email protected]>
1 parent 4e2d316 commit e86e915

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

dapr/src/server/http.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ pub struct DaprHttpServer {
8484

8585
impl DaprHttpServer {
8686
/// Creates a new instance of the Dapr HTTP server with default options.
87+
///
88+
/// # Panics
89+
///
90+
/// This function panics if the Dapr Sidecar cannot be reached!
91+
/// For a non-panicking version that allows you to handle any errors yourself, see:
92+
/// [DaprHttpServer::try_new_with_dapr_port]
8793
pub async fn new() -> Self {
8894
let dapr_port: u16 = std::env::var("DAPR_GRPC_PORT")
8995
.unwrap_or("3501".into())
@@ -92,19 +98,38 @@ impl DaprHttpServer {
9298
Self::with_dapr_port(dapr_port).await
9399
}
94100

101+
/// Creates a new instance of the Dapr HTTP server that connects to the Dapr sidecar on the
102+
/// given dapr_port.
103+
///
104+
/// # Panics
105+
///
106+
/// This function panics if the Dapr Sidecar cannot be reached!
107+
/// For a non-panicking version that allows you to handle any errors yourself, see:
108+
/// [DaprHttpServer::try_new_with_dapr_port]
95109
pub async fn with_dapr_port(dapr_port: u16) -> Self {
96-
let dapr_addr = format!("https://127.0.0.1:{}", dapr_port);
97-
98-
let cc = match TonicClient::connect(dapr_addr).await {
110+
match Self::try_new_with_dapr_port(dapr_port).await {
99111
Ok(c) => c,
100112
Err(err) => panic!("failed to connect to dapr: {}", err),
101-
};
113+
}
114+
}
115+
116+
/// Creates a new instance of the Dapr HTTP server that connects to the Dapr sidecar on the
117+
/// given dapr_port.
118+
///
119+
/// In contrast to the other functions that create a DaprHttpServer, this function does
120+
/// not panic, but instead returns a Result.
121+
pub async fn try_new_with_dapr_port(
122+
dapr_port: u16,
123+
) -> Result<Self, Box<dyn std::error::Error>> {
124+
let dapr_addr = format!("https://127.0.0.1:{}", dapr_port);
125+
126+
let cc = TonicClient::connect(dapr_addr).await?;
102127
let rt = ActorRuntime::new(cc);
103128

104-
DaprHttpServer {
129+
Ok(DaprHttpServer {
105130
actor_runtime: Arc::new(rt),
106131
shutdown_signal: None,
107-
}
132+
})
108133
}
109134

110135
pub fn with_graceful_shutdown<F>(self, signal: F) -> Self
@@ -138,7 +163,7 @@ impl DaprHttpServer {
138163
.unwrap_or(8080);
139164

140165
let address = format!("127.0.0.1:{}", port.unwrap_or(default_port));
141-
let listener = TcpListener::bind(address).await.unwrap();
166+
let listener = TcpListener::bind(address).await?;
142167

143168
let server = axum::serve(listener, app.into_make_service());
144169

0 commit comments

Comments
 (0)