1
1
use std:: future:: Future ;
2
2
use std:: net:: SocketAddr ;
3
- use std:: ops:: { Add , Deref } ;
3
+ use std:: ops:: { Add , AsyncFn , Deref } ;
4
4
use std:: sync:: Arc ;
5
5
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
6
6
use std:: time:: Duration ;
7
7
8
- use tokio :: sync :: { Mutex , oneshot } ;
9
- use tokio:: sync:: mpsc :: UnboundedSender ;
8
+ use futures :: future :: BoxFuture ;
9
+ use tokio:: sync:: Mutex ;
10
10
use tokio:: time:: Instant ;
11
11
use tokio_rustls:: rustls:: RootCertStore ;
12
12
use uuid:: Uuid ;
@@ -16,11 +16,10 @@ use crate::error::Error;
16
16
use crate :: error:: Result ;
17
17
use crate :: memdx:: auth_mechanism:: AuthMechanism ;
18
18
use crate :: memdx:: connection:: { Connection , ConnectOptions } ;
19
- use crate :: memdx:: dispatcher:: { Dispatcher , DispatcherOptions } ;
19
+ use crate :: memdx:: dispatcher:: { Dispatcher , DispatcherOptions , OrphanResponseHandler } ;
20
20
use crate :: memdx:: hello_feature:: HelloFeature ;
21
21
use crate :: memdx:: op_auth_saslauto:: SASLAuthAutoOptions ;
22
22
use crate :: memdx:: op_bootstrap:: BootstrapOptions ;
23
- use crate :: memdx:: packet:: ResponsePacket ;
24
23
use crate :: memdx:: request:: { GetErrorMapRequest , HelloRequest , SelectBucketRequest } ;
25
24
use crate :: service_type:: ServiceType ;
26
25
@@ -53,9 +52,12 @@ impl PartialEq for KvClientConfig {
53
52
}
54
53
}
55
54
55
+ pub ( crate ) type OnKvClientCloseHandler =
56
+ Arc < dyn Fn ( String ) -> BoxFuture < ' static , ( ) > + Send + Sync > ;
57
+
56
58
pub ( crate ) struct KvClientOptions {
57
- pub orphan_handler : Arc < UnboundedSender < ResponsePacket > > ,
58
- pub on_close_tx : Option < UnboundedSender < String > > ,
59
+ pub orphan_handler : OrphanResponseHandler ,
60
+ pub on_close : OnKvClientCloseHandler ,
59
61
}
60
62
61
63
pub ( crate ) trait KvClient : Sized + PartialEq + Send + Sync {
@@ -178,16 +180,26 @@ where
178
180
) ) ;
179
181
}
180
182
181
- let ( connection_close_tx, mut connection_close_rx) =
182
- oneshot:: channel :: < crate :: memdx:: error:: Result < ( ) > > ( ) ;
183
+ let closed = Arc :: new ( AtomicBool :: new ( false ) ) ;
184
+ let closed_clone = closed. clone ( ) ;
185
+ let id = Uuid :: new_v4 ( ) . to_string ( ) ;
186
+ let read_id = id. clone ( ) ;
187
+
188
+ let on_close = opts. on_close . clone ( ) ;
183
189
let memdx_client_opts = DispatcherOptions {
184
- on_connection_close_handler : Some ( connection_close_tx) ,
190
+ on_connection_close_handler : Arc :: new ( move || {
191
+ // There's not much to do when the connection closes so just mark us as closed.
192
+ closed_clone. store ( true , Ordering :: SeqCst ) ;
193
+ let on_close = on_close. clone ( ) ;
194
+ let read_id = read_id. clone ( ) ;
195
+
196
+ Box :: pin ( async move {
197
+ on_close ( read_id) . await ;
198
+ } )
199
+ } ) ,
185
200
orphan_handler : opts. orphan_handler ,
186
201
} ;
187
202
188
- let closed = Arc :: new ( AtomicBool :: new ( false ) ) ;
189
- let closed_clone = closed. clone ( ) ;
190
-
191
203
let conn = Connection :: connect (
192
204
config. address ,
193
205
ConnectOptions {
@@ -205,7 +217,6 @@ where
205
217
let local_addr = * conn. local_addr ( ) ;
206
218
207
219
let mut cli = D :: new ( conn, memdx_client_opts) ;
208
- let id = Uuid :: new_v4 ( ) . to_string ( ) ;
209
220
210
221
let mut kv_cli = StdKvClient {
211
222
remote_addr,
@@ -218,18 +229,6 @@ where
218
229
id : id. clone ( ) ,
219
230
} ;
220
231
221
- tokio:: spawn ( async move {
222
- // There's not much to do when the connection closes so just mark us as closed.
223
- if connection_close_rx. await . is_ok ( ) {
224
- closed_clone. store ( true , Ordering :: SeqCst ) ;
225
- } ;
226
-
227
- if let Some ( mut tx) = opts. on_close_tx {
228
- // TODO: Probably log on failure.
229
- tx. send ( id) . unwrap_or_default ( ) ;
230
- }
231
- } ) ;
232
-
233
232
if should_bootstrap {
234
233
if let Some ( b) = & bootstrap_select_bucket {
235
234
let mut guard = kv_cli. selected_bucket . lock ( ) . await ;
@@ -309,7 +308,7 @@ where
309
308
. await
310
309
{
311
310
Ok ( _) => { }
312
- Err ( e ) => {
311
+ Err ( _e ) => {
313
312
let mut current_bucket = self . selected_bucket . lock ( ) . await ;
314
313
* current_bucket = None ;
315
314
drop ( current_bucket) ;
@@ -362,14 +361,12 @@ mod tests {
362
361
use std:: sync:: Arc ;
363
362
use std:: time:: Duration ;
364
363
365
- use tokio:: sync:: mpsc:: unbounded_channel;
366
364
use tokio:: time:: Instant ;
367
365
368
366
use crate :: authenticator:: PasswordAuthenticator ;
369
367
use crate :: kvclient:: { KvClient , KvClientConfig , KvClientOptions , StdKvClient } ;
370
368
use crate :: kvclient_ops:: KvClientOps ;
371
369
use crate :: memdx:: client:: Client ;
372
- use crate :: memdx:: packet:: ResponsePacket ;
373
370
use crate :: memdx:: request:: { GetRequest , SetRequest } ;
374
371
375
372
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
@@ -378,21 +375,6 @@ mod tests {
378
375
379
376
let instant = Instant :: now ( ) . add ( Duration :: new ( 7 , 0 ) ) ;
380
377
381
- let ( orphan_tx, mut orphan_rx) = unbounded_channel :: < ResponsePacket > ( ) ;
382
-
383
- tokio:: spawn ( async move {
384
- loop {
385
- match orphan_rx. recv ( ) . await {
386
- Some ( resp) => {
387
- dbg ! ( "unexpected orphan" , resp) ;
388
- }
389
- None => {
390
- return ;
391
- }
392
- }
393
- }
394
- } ) ;
395
-
396
378
let client_config = KvClientConfig {
397
379
address : "192.168.107.128:11210"
398
380
. parse ( )
@@ -416,8 +398,10 @@ mod tests {
416
398
let mut client = StdKvClient :: < Client > :: new (
417
399
client_config,
418
400
KvClientOptions {
419
- orphan_handler : Arc :: new ( orphan_tx) ,
420
- on_close_tx : None ,
401
+ orphan_handler : Arc :: new ( |packet| {
402
+ dbg ! ( "unexpected orphan" , packet) ;
403
+ } ) ,
404
+ on_close : Arc :: new ( |id| Box :: pin ( async { } ) ) ,
421
405
} ,
422
406
)
423
407
. await
0 commit comments