Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add HTTPS support in RaftServiceClient and update client example #112

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions examples/memstore/src/client/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use raftify::{create_client, raft_service, AbstractLogEntry};
use raftify::{create_client, create_client_with_ssl, raft_service, AbstractLogEntry};

use memstore_example_harness::state_machine::LogEntry;

Expand All @@ -8,6 +8,7 @@ use memstore_example_harness::state_machine::LogEntry;
async fn main() {
println!("---Message propose---");
let mut leader_client = create_client(&"127.0.0.1:60061").await.unwrap();
let mut leader_client2 = create_client_with_ssl(&"127.0.0.1:60061").await.unwrap();

leader_client
.propose(raft_service::ProposeArgs {
Expand Down Expand Up @@ -37,9 +38,17 @@ async fn main() {
.into_inner()
.result_json;

let result2 = leader_client2
.debug_node(raft_service::Empty {})
.await
.unwrap()
.into_inner()
.result_json;

assert_eq!(result, result2);
println!("Debug node result: {:?}", result);

println!("---Change config example---");
// println!("---Change config example---");
// leader_client.change_config(raft_service::ChangeConfigArgs {
// addrs: vec![
// "127.0.0.1:60061".to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion raftify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use crate::{
peer::Peer,
peers::Peers,
raft_bootstrapper::Raft,
raft_client::create_client,
raft_client::{create_client, create_client_with_ssl},
raft_node::{role::InitialRole, RaftNode},
raft_service::raft_service_client::RaftServiceClient,
request::common::confchange_request::ConfChangeRequest,
Expand Down
34 changes: 30 additions & 4 deletions raftify/src/raft_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,46 @@ use tonic::transport::{Channel, Error as TonicError};

use super::RaftServiceClient;

// TODO: Support https schema
pub async fn create_client<A: ToSocketAddrs>(
addr: A,
) -> Result<RaftServiceClient<Channel>, TonicError> {
_create_client(addr, false).await
}

pub async fn create_client_with_ssl<A: ToSocketAddrs>(
addr: A,
) -> Result<RaftServiceClient<Channel>, TonicError> {
_create_client(addr, true).await
}

async fn _create_client<A: ToSocketAddrs>(
addr: A,
b_ssl: bool,
) -> Result<RaftServiceClient<Channel>, TonicError> {
let httpx = if b_ssl {
String::from("https")
} else {
String::from("http")
};
let addr = addr
.to_socket_addrs()
.expect("Invalid socket address format")
.next()
.unwrap();
let addr = format!("http://{}", addr);
let addr = Bytes::copy_from_slice(addr.as_bytes());
let url = format!("{}://{}", httpx, addr);
let url = Bytes::copy_from_slice(url.as_bytes());

let channel = Channel::from_shared(addr).unwrap().connect().await?;
let channel = Channel::from_shared(url).unwrap().connect().await?;
let client = RaftServiceClient::new(channel);

Ok(client)
}

// Todo!
#[cfg(test)]
mod test {
#[tokio::test]
async fn test_create_client() {
todo!("!!!")
}
}
Loading