17
17
use std:: { collections:: HashMap , error:: Error , sync:: Arc } ;
18
18
19
19
use rocketmq_common:: TokioExecutorService ;
20
+ use tokio:: sync:: Mutex ;
20
21
21
22
use crate :: {
22
23
clients:: { Client , RemotingClient } ,
@@ -32,7 +33,7 @@ pub struct RocketmqDefaultClient {
32
33
service_bridge : ServiceBridge ,
33
34
tokio_client_config : TokioClientConfig ,
34
35
//cache connection
35
- connection_tables : HashMap < String /* ip:port */ , Client > ,
36
+ connection_tables : HashMap < String /* ip:port */ , Arc < Mutex < Client > > > ,
36
37
lock : std:: sync:: RwLock < ( ) > ,
37
38
}
38
39
@@ -48,19 +49,21 @@ impl RocketmqDefaultClient {
48
49
}
49
50
50
51
impl RocketmqDefaultClient {
51
- fn get_and_create_client ( & mut self , addr : String ) -> & mut Client {
52
+ fn get_and_create_client ( & mut self , addr : String ) -> Arc < Mutex < Client > > {
52
53
let lc = self . lock . write ( ) . unwrap ( ) ;
53
54
54
55
if self . connection_tables . contains_key ( & addr) {
55
- return self . connection_tables . get_mut ( & addr) . unwrap ( ) ;
56
+ return self . connection_tables . get ( & addr) . cloned ( ) . unwrap ( ) ;
56
57
}
57
58
58
59
let addr_inner = addr. clone ( ) ;
59
60
let client =
60
61
futures:: executor:: block_on ( async move { Client :: connect ( addr_inner) . await . unwrap ( ) } ) ;
61
- self . connection_tables . insert ( addr. clone ( ) , client) ;
62
+
63
+ self . connection_tables
64
+ . insert ( addr. clone ( ) , Arc :: new ( Mutex :: new ( client) ) ) ;
62
65
drop ( lc) ;
63
- self . connection_tables . get_mut ( & addr) . unwrap ( )
66
+ self . connection_tables . get ( & addr) . cloned ( ) . unwrap ( )
64
67
}
65
68
}
66
69
@@ -103,7 +106,11 @@ impl RemotingClient for RocketmqDefaultClient {
103
106
request : RemotingCommand ,
104
107
timeout_millis : u64 ,
105
108
) -> Result < RemotingCommand , Box < dyn Error > > {
106
- todo ! ( )
109
+ let client = self . get_and_create_client ( addr. clone ( ) ) ;
110
+ Ok ( self
111
+ . service_bridge
112
+ . invoke_sync ( client, request, timeout_millis)
113
+ . unwrap ( ) )
107
114
}
108
115
109
116
async fn invoke_async (
@@ -114,8 +121,10 @@ impl RemotingClient for RocketmqDefaultClient {
114
121
invoke_callback : impl InvokeCallback ,
115
122
) -> Result < ( ) , Box < dyn Error > > {
116
123
let client = self . get_and_create_client ( addr. clone ( ) ) ;
117
-
118
- unreachable ! ( )
124
+ self . service_bridge
125
+ . invoke_async ( client, request, timeout_millis, invoke_callback)
126
+ . await ;
127
+ Ok ( ( ) )
119
128
}
120
129
121
130
fn invoke_oneway (
0 commit comments