@@ -24,7 +24,7 @@ use std::{
24
24
25
25
pub use blocking_client:: BlockingClient ;
26
26
pub use client:: Client ;
27
- use rocketmq_common:: TokioExecutorService ;
27
+ use rocketmq_common:: { common :: future :: CompletableFuture , TokioExecutorService } ;
28
28
29
29
use crate :: {
30
30
net:: ResponseFuture ,
@@ -37,6 +37,7 @@ mod async_client;
37
37
mod blocking_client;
38
38
39
39
mod client;
40
+ mod rocketmq_default_impl;
40
41
41
42
#[ derive( Default ) ]
42
43
pub struct RemoteClient {
@@ -91,30 +92,48 @@ trait RemotingClient: RemotingService {
91
92
request : RemotingCommand ,
92
93
timeout_millis : u64 ,
93
94
) -> Result < RemotingCommand , Box < dyn std:: error:: Error > > ;
94
- fn invoke_async (
95
+
96
+ async fn invoke_async (
95
97
& mut self ,
96
98
addr : String ,
97
99
request : RemotingCommand ,
98
100
timeout_millis : u64 ,
99
- invoke_callback : Arc < dyn InvokeCallback > ,
101
+ invoke_callback : impl InvokeCallback ,
100
102
) -> Result < ( ) , Box < dyn std:: error:: Error > > ;
103
+
101
104
fn invoke_oneway (
102
105
& self ,
103
106
addr : String ,
104
107
request : RemotingCommand ,
105
108
timeout_millis : u64 ,
106
109
) -> Result < ( ) , Box < dyn std:: error:: Error > > ;
107
110
108
- fn invoke (
111
+ async fn invoke (
109
112
& mut self ,
110
113
addr : String ,
111
114
request : RemotingCommand ,
112
115
timeout_millis : u64 ,
113
- ) -> Result < RemotingCommand , Box < dyn std:: error:: Error > > {
114
- let future = Arc :: new ( DefaultInvokeCallback { } ) ;
115
- match self . invoke_async ( addr, request, timeout_millis, future) {
116
- Ok ( _) => Ok ( RemotingCommand :: default ( ) ) ,
117
- Err ( e) => Err ( e) ,
116
+ ) -> Result < CompletableFuture < RemotingCommand > , Box < dyn std:: error:: Error > > {
117
+ let completable_future = CompletableFuture :: new ( ) ;
118
+ let sender = completable_future. get_sender ( ) ;
119
+ match self
120
+ . invoke_async (
121
+ addr,
122
+ request,
123
+ timeout_millis,
124
+ |response : Option < RemotingCommand > ,
125
+ error : Option < Box < dyn std:: error:: Error > > ,
126
+ _response_future : Option < ResponseFuture > | {
127
+ if let Some ( response) = response {
128
+ let _ = sender. blocking_send ( response) ;
129
+ } else if let Some ( _error) = error {
130
+ }
131
+ } ,
132
+ )
133
+ . await
134
+ {
135
+ Ok ( _) => Ok ( completable_future) ,
136
+ Err ( err) => Err ( err) ,
118
137
}
119
138
}
120
139
@@ -128,14 +147,25 @@ trait RemotingClient: RemotingService {
128
147
fn set_callback_executor ( & mut self , executor : Arc < TokioExecutorService > ) ;
129
148
130
149
fn is_address_reachable ( & mut self , addr : String ) ;
131
- }
132
150
133
- struct DefaultInvokeCallback ;
151
+ fn close_clients ( & mut self , addrs : Vec < String > ) ;
152
+ }
134
153
135
- impl InvokeCallback for DefaultInvokeCallback {
136
- fn operation_complete ( & self , _response_future : ResponseFuture ) { }
154
+ impl < T > InvokeCallback for T
155
+ where
156
+ T : Fn ( Option < RemotingCommand > , Option < Box < dyn std:: error:: Error > > , Option < ResponseFuture > )
157
+ + Send
158
+ + Sync ,
159
+ {
160
+ fn operation_complete ( & self , response_future : ResponseFuture ) {
161
+ self ( None , None , Some ( response_future) )
162
+ }
137
163
138
- fn operation_succeed ( & self , _response : RemotingCommand ) { }
164
+ fn operation_succeed ( & self , response : RemotingCommand ) {
165
+ self ( Some ( response) , None , None )
166
+ }
139
167
140
- fn operation_fail ( & self , _throwable : Box < dyn std:: error:: Error > ) { }
168
+ fn operation_fail ( & self , throwable : Box < dyn std:: error:: Error > ) {
169
+ self ( None , Some ( throwable) , None )
170
+ }
141
171
}
0 commit comments