Skip to content

Commit 8b0288b

Browse files
authored
fix: align DaprHttpServer default gRPC port with SDK config (#335)
Signed-off-by: immanuwell <pchpr.00@list.ru>
1 parent 7514023 commit 8b0288b

1 file changed

Lines changed: 54 additions & 6 deletions

File tree

dapr/src/server/http.rs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use futures::{Future, FutureExt};
99
use std::{pin::Pin, sync::Arc, time::Duration};
1010
use tokio::net::TcpListener;
1111

12-
use super::super::client::{AppApiTokenLayer, TonicClient};
12+
use super::super::client::{
13+
AppApiTokenLayer, TonicClient,
14+
config::{DAPR_GRPC_PORT_ENV, DEFAULT_DAPR_GRPC_PORT},
15+
};
1316
use super::actor::runtime::{ActorRuntime, ActorTypeRegistration};
1417

1518
/// The Dapr HTTP server.
@@ -92,11 +95,7 @@ impl DaprHttpServer {
9295
/// For a non-panicking version that allows you to handle any errors yourself, see:
9396
/// [DaprHttpServer::try_new_with_dapr_port]
9497
pub async fn new() -> Self {
95-
let dapr_port: u16 = std::env::var("DAPR_GRPC_PORT")
96-
.unwrap_or("3501".into())
97-
.parse()
98-
.unwrap();
99-
Self::with_dapr_port(dapr_port).await
98+
Self::with_dapr_port(dapr_grpc_port_from_env()).await
10099
}
101100

102101
/// Creates a new instance of the Dapr HTTP server that connects to the Dapr sidecar on the
@@ -385,3 +384,52 @@ struct ReminderPayload {
385384
struct TimerPayload {
386385
data: Option<String>,
387386
}
387+
388+
fn dapr_grpc_port_from_env() -> u16 {
389+
std::env::var(DAPR_GRPC_PORT_ENV)
390+
.unwrap_or_else(|_| DEFAULT_DAPR_GRPC_PORT.to_string())
391+
.parse()
392+
.unwrap()
393+
}
394+
395+
#[cfg(test)]
396+
mod tests {
397+
use super::*;
398+
use std::sync::Mutex;
399+
400+
static ENV_LOCK: Mutex<()> = Mutex::new(());
401+
402+
fn with_env<F: FnOnce()>(key: &str, value: Option<&str>, f: F) {
403+
let _guard = ENV_LOCK.lock().unwrap();
404+
let prev = std::env::var(key).ok();
405+
match value {
406+
Some(value) => unsafe { std::env::set_var(key, value) },
407+
None => unsafe { std::env::remove_var(key) },
408+
}
409+
410+
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
411+
412+
match prev {
413+
Some(value) => unsafe { std::env::set_var(key, value) },
414+
None => unsafe { std::env::remove_var(key) },
415+
}
416+
417+
if let Err(err) = result {
418+
std::panic::resume_unwind(err);
419+
}
420+
}
421+
422+
#[test]
423+
fn http_server_defaults_to_standard_dapr_grpc_port() {
424+
with_env(DAPR_GRPC_PORT_ENV, None, || {
425+
assert_eq!(dapr_grpc_port_from_env(), DEFAULT_DAPR_GRPC_PORT);
426+
});
427+
}
428+
429+
#[test]
430+
fn http_server_honors_dapr_grpc_port_override() {
431+
with_env(DAPR_GRPC_PORT_ENV, Some("12345"), || {
432+
assert_eq!(dapr_grpc_port_from_env(), 12345);
433+
});
434+
}
435+
}

0 commit comments

Comments
 (0)