Skip to content

[ISSUE #274]🚀Implement remoting client-3 #275

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

Merged
merged 1 commit into from
Mar 15, 2024
Merged
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
25 changes: 17 additions & 8 deletions rocketmq-remoting/src/clients/rocketmq_default_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use std::{collections::HashMap, error::Error, sync::Arc};

use rocketmq_common::TokioExecutorService;
use tokio::sync::Mutex;

use crate::{
clients::{Client, RemotingClient},
Expand All @@ -32,7 +33,7 @@ pub struct RocketmqDefaultClient {
service_bridge: ServiceBridge,
tokio_client_config: TokioClientConfig,
//cache connection
connection_tables: HashMap<String /* ip:port */, Client>,
connection_tables: HashMap<String /* ip:port */, Arc<Mutex<Client>>>,
lock: std::sync::RwLock<()>,
}

Expand All @@ -48,19 +49,21 @@ impl RocketmqDefaultClient {
}

impl RocketmqDefaultClient {
fn get_and_create_client(&mut self, addr: String) -> &mut Client {
fn get_and_create_client(&mut self, addr: String) -> Arc<Mutex<Client>> {
let lc = self.lock.write().unwrap();

if self.connection_tables.contains_key(&addr) {
return self.connection_tables.get_mut(&addr).unwrap();
return self.connection_tables.get(&addr).cloned().unwrap();
}

let addr_inner = addr.clone();
let client =
futures::executor::block_on(async move { Client::connect(addr_inner).await.unwrap() });
self.connection_tables.insert(addr.clone(), client);

self.connection_tables
.insert(addr.clone(), Arc::new(Mutex::new(client)));
drop(lc);
self.connection_tables.get_mut(&addr).unwrap()
self.connection_tables.get(&addr).cloned().unwrap()
}
}

Expand Down Expand Up @@ -103,7 +106,11 @@ impl RemotingClient for RocketmqDefaultClient {
request: RemotingCommand,
timeout_millis: u64,
) -> Result<RemotingCommand, Box<dyn Error>> {
todo!()
let client = self.get_and_create_client(addr.clone());
Ok(self
.service_bridge
.invoke_sync(client, request, timeout_millis)
.unwrap())
}

async fn invoke_async(
Expand All @@ -114,8 +121,10 @@ impl RemotingClient for RocketmqDefaultClient {
invoke_callback: impl InvokeCallback,
) -> Result<(), Box<dyn Error>> {
let client = self.get_and_create_client(addr.clone());

unreachable!()
self.service_bridge
.invoke_async(client, request, timeout_millis, invoke_callback)
.await;
Ok(())
}

fn invoke_oneway(
Expand Down
22 changes: 19 additions & 3 deletions rocketmq-remoting/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use std::{collections::HashMap, sync::Arc, time::Duration};

use rocketmq_common::{common::Pair, TokioExecutorService};
use tokio::time;
use tokio::{runtime::Runtime, sync::Mutex, time, time::timeout};

use crate::{
clients::Client,
Expand Down Expand Up @@ -119,17 +119,33 @@ impl ServiceBridge {

pub async fn invoke_async(
&mut self,
client: &mut Client,
client: Arc<Mutex<Client>>,
request: RemotingCommand,
timeout_millis: u64,
invoke_callback: impl InvokeCallback,
) {
if let Ok(resp) = time::timeout(Duration::from_millis(timeout_millis), async {
client.invoke(request).await.unwrap()
client.lock().await.invoke(request).await.unwrap()
})
.await
{
invoke_callback.operation_succeed(resp)
}
}

pub fn invoke_sync(
&mut self,
client: Arc<Mutex<Client>>,
request: RemotingCommand,
timeout_millis: u64,
) -> Option<RemotingCommand> {
let remoting_command = Runtime::new().unwrap().block_on(async move {
let result = timeout(Duration::from_millis(timeout_millis), async move {
client.lock().await.invoke(request).await.unwrap()
})
.await;
result.unwrap()
});
Some(remoting_command)
}
}