Skip to content

Commit

Permalink
feat: implement new endpoint env var and also legacy grpc port
Browse files Browse the repository at this point in the history
Signed-off-by: mikeee <[email protected]>
  • Loading branch information
mikeee committed Aug 27, 2024
1 parent 86fc68a commit de34919
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
59 changes: 53 additions & 6 deletions dapr/src/client_new.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::env;
use std::time::Duration;

use log::error;
use tonic::transport::{Channel, Uri};

use crate::dapr::proto::{common::v1::*, runtime::v1::dapr_client::DaprClient, runtime::v1::*};
Expand All @@ -8,8 +10,11 @@ use crate::error::Error;
/// The Rust SDK version populated in the compiler environment.
const SDK_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Dapr env var constants
//const DAPR_GRPC_ENDPOINT_ENV_VAR_NAME: &str = "DAPR_GRPC_ENDPOINT";
// Dapr env var constants
/// Primary env var for initializing a default client`DAPR_GRPC_ENDPOINT`
const DAPR_GRPC_ENDPOINT_ENV_VAR_NAME: &str = "DAPR_GRPC_ENDPOINT";
/// Secondary env var for initializing a default client `DAPR_GRPC_PORT`
const DAPR_GRPC_PORT_ENV_VAR_NAME: &str = "DAPR_GRPC_PORT";

//const DAPR_API_MAX_RETRIES_ENV_VAR_NAME: &str = "DAPR_API_MAX_RETRIES";
//const DAPR_API_MAX_RETRIES_DEFAULT: u8 = 0;
Expand All @@ -26,6 +31,9 @@ pub struct Client {
}

impl Client {
/// Internal function that creates a new dapr client instance
/// # Errors
/// Returns an error if the client cannot be created
async fn new_internal(addr: Uri, keep_alive: Option<Duration>) -> Result<Self, Error> {
let user_agent = format!("dapr-sdk-rust/{}", SDK_VERSION);
let builder = Channel::builder(addr)
Expand All @@ -42,16 +50,55 @@ impl Client {
})
}

/// Create a dapr instance with the endpoint from env var or default.
/// Creates a new dapr client instance
/// # Errors
/// Returns an error if the client cannot be created
pub async fn new() -> Result<Self, Error> {
let addr: Uri = format!("{}:{}", "http://localhost", "50051")
.parse()
.unwrap();
let addr = match Self::get_default_dapr_grpc_addr() {
Ok(addr) => addr,
Err(_) => return Err(Error::InitializationError),
};

println!("dapr grpc address: {}", addr.clone());
let keep_alive = Some(Duration::from_secs(60));
Self::new_internal(addr, keep_alive).await
// TODO: Cleanup implementation
}

/// Internal function to get the default dapr grpc address
/// This will be using in order of priority:
/// 1. Env var: `DAPR_GRPC_ENDPOINT`
/// 2. Env var: `DAPR_GRPC_PORT`
/// 3. Default: `http://localhost:50051`
/// # Errors
/// Returns an error if the address cannot be parsed from either of the env vars
fn get_default_dapr_grpc_addr() -> Result<Uri, Error> {
// Try to get the endpoint from `DAPR_GRPC_ENDPOINT` env var
if let Ok(endpoint) = env::var(DAPR_GRPC_ENDPOINT_ENV_VAR_NAME) {
match endpoint.parse() {
Ok(endpoint) => return Ok(endpoint),
Err(_) => {
error!("Failed to parse DAPR_GRPC_ENDPOINT env var");
}
};
}

// Try to get the port from `DAPR_GRPC_PORT` env var
if let Ok(port) = env::var(DAPR_GRPC_PORT_ENV_VAR_NAME) {
let addr = format!("http://localhost:{}", port);
match addr.parse() {
Ok(addr) => return Ok(addr),
Err(_) => {
error!("Failed to parse DAPR_GRPC_PORT env var");
}
};
}

// Default to 50051
let addr = "http://localhost:50051";
Ok(addr.parse().unwrap())
}

/// Invokes a method on a remote Dapr app.
pub async fn invoke_service(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions dapr/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Error {
VarError,
SerializationError,
UnimplementedError,
InitializationError,
}

impl Display for Error {
Expand Down

0 comments on commit de34919

Please sign in to comment.