@@ -360,14 +360,16 @@ impl<L> ClientBuilder<L> {
360
360
361
361
tokio:: spawn ( wait_for_shutdown ( send_receive_task_sync_rx, client_dropped_rx, disconnect_reason. clone ( ) ) ) ;
362
362
363
- Client {
363
+ let inner = ClientInner {
364
364
to_back : to_back. clone ( ) ,
365
365
service : self . service_builder . service ( RpcService :: new ( to_back. clone ( ) ) ) ,
366
366
request_timeout : self . request_timeout ,
367
367
error : ErrorFromBack :: new ( to_back, disconnect_reason) ,
368
368
id_manager : RequestIdManager :: new ( self . id_kind ) ,
369
369
on_exit : Some ( client_dropped_tx) ,
370
- }
370
+ } ;
371
+
372
+ Client { inner : Arc :: new ( inner) }
371
373
}
372
374
373
375
/// Build the client with given transport.
@@ -430,9 +432,8 @@ impl<L> ClientBuilder<L> {
430
432
}
431
433
}
432
434
433
- /// Generic asynchronous client.
434
435
#[ derive( Debug ) ]
435
- pub struct Client < L = RpcLogger < RpcService > > {
436
+ struct ClientInner < L = RpcLogger < RpcService > > {
436
437
/// Channel to send requests to the background task.
437
438
to_back : mpsc:: Sender < FrontToBack > ,
438
439
error : ErrorFromBack ,
@@ -445,6 +446,21 @@ pub struct Client<L = RpcLogger<RpcService>> {
445
446
service : L ,
446
447
}
447
448
449
+ impl < L > Drop for ClientInner < L > {
450
+ fn drop ( & mut self ) {
451
+ if let Some ( e) = self . on_exit . take ( ) {
452
+ let _ = e. send ( ( ) ) ;
453
+ }
454
+ }
455
+ }
456
+
457
+ /// Generic asynchronous client.
458
+ #[ derive( Debug ) ]
459
+ #[ repr( transparent) ]
460
+ pub struct Client < L = RpcLogger < RpcService > > {
461
+ inner : Arc < ClientInner < L > >
462
+ }
463
+
448
464
impl Client < Identity > {
449
465
/// Create a builder for the client.
450
466
pub fn builder ( ) -> ClientBuilder {
@@ -455,13 +471,13 @@ impl Client<Identity> {
455
471
impl < L > Client < L > {
456
472
/// Checks if the client is connected to the target.
457
473
pub fn is_connected ( & self ) -> bool {
458
- !self . to_back . is_closed ( )
474
+ !self . inner . to_back . is_closed ( )
459
475
}
460
476
461
477
async fn run_future_until_timeout < T > ( & self , fut : impl Future < Output = Result < T , Error > > ) -> Result < T , Error > {
462
478
tokio:: pin!( fut) ;
463
479
464
- match futures_util:: future:: select ( fut, futures_timer:: Delay :: new ( self . request_timeout ) ) . await {
480
+ match futures_util:: future:: select ( fut, futures_timer:: Delay :: new ( self . inner . request_timeout ) ) . await {
465
481
Either :: Left ( ( Ok ( r) , _) ) => Ok ( r) ,
466
482
Either :: Left ( ( Err ( Error :: ServiceDisconnect ) , _) ) => Err ( self . on_disconnect ( ) . await ) ,
467
483
Either :: Left ( ( Err ( e) , _) ) => Err ( e) ,
@@ -476,20 +492,18 @@ impl<L> Client<L> {
476
492
///
477
493
/// This method is cancel safe.
478
494
pub async fn on_disconnect ( & self ) -> Error {
479
- self . error . read_error ( ) . await
495
+ self . inner . error . read_error ( ) . await
480
496
}
481
497
482
498
/// Returns configured request timeout.
483
499
pub fn request_timeout ( & self ) -> Duration {
484
- self . request_timeout
500
+ self . inner . request_timeout
485
501
}
486
502
}
487
503
488
- impl < L > Drop for Client < L > {
489
- fn drop ( & mut self ) {
490
- if let Some ( e) = self . on_exit . take ( ) {
491
- let _ = e. send ( ( ) ) ;
492
- }
504
+ impl < L > Clone for Client < L > {
505
+ fn clone ( & self ) -> Self {
506
+ Self { inner : self . inner . clone ( ) }
493
507
}
494
508
}
495
509
@@ -508,9 +522,9 @@ where
508
522
{
509
523
async {
510
524
// NOTE: we use this to guard against max number of concurrent requests.
511
- let _req_id = self . id_manager . next_request_id ( ) ;
525
+ let _req_id = self . inner . id_manager . next_request_id ( ) ;
512
526
let params = params. to_rpc_params ( ) ?. map ( StdCow :: Owned ) ;
513
- let fut = self . service . notification ( jsonrpsee_types:: Notification :: new ( method. into ( ) , params) ) ;
527
+ let fut = self . inner . service . notification ( jsonrpsee_types:: Notification :: new ( method. into ( ) , params) ) ;
514
528
self . run_future_until_timeout ( fut) . await ?;
515
529
Ok ( ( ) )
516
530
}
@@ -522,9 +536,9 @@ where
522
536
Params : ToRpcParams + Send ,
523
537
{
524
538
async {
525
- let id = self . id_manager . next_request_id ( ) ;
539
+ let id = self . inner . id_manager . next_request_id ( ) ;
526
540
let params = params. to_rpc_params ( ) ?;
527
- let fut = self . service . call ( Request :: borrowed ( method, params. as_deref ( ) , id. clone ( ) ) ) ;
541
+ let fut = self . inner . service . call ( Request :: borrowed ( method, params. as_deref ( ) , id. clone ( ) ) ) ;
528
542
let rp = self . run_future_until_timeout ( fut) . await ?;
529
543
let success = ResponseSuccess :: try_from ( rp. into_response ( ) . into_inner ( ) ) ?;
530
544
@@ -541,15 +555,15 @@ where
541
555
{
542
556
async {
543
557
let batch = batch. build ( ) ?;
544
- let id = self . id_manager . next_request_id ( ) ;
558
+ let id = self . inner . id_manager . next_request_id ( ) ;
545
559
let id_range = generate_batch_id_range ( id, batch. len ( ) as u64 ) ?;
546
560
547
561
let mut b = Batch :: with_capacity ( batch. len ( ) ) ;
548
562
549
563
for ( ( method, params) , id) in batch. into_iter ( ) . zip ( id_range. clone ( ) ) {
550
564
b. push ( Request {
551
565
jsonrpc : TwoPointZero ,
552
- id : self . id_manager . as_id_kind ( ) . into_id ( id) ,
566
+ id : self . inner . id_manager . as_id_kind ( ) . into_id ( id) ,
553
567
method : method. into ( ) ,
554
568
params : params. map ( StdCow :: Owned ) ,
555
569
extensions : Extensions :: new ( ) ,
@@ -558,7 +572,7 @@ where
558
572
559
573
b. extensions_mut ( ) . insert ( IsBatch { id_range } ) ;
560
574
561
- let fut = self . service . batch ( b) ;
575
+ let fut = self . inner . service . batch ( b) ;
562
576
let json_values = self . run_future_until_timeout ( fut) . await ?;
563
577
564
578
let mut responses = Vec :: with_capacity ( json_values. len ( ) ) ;
@@ -592,6 +606,8 @@ where
592
606
> + Send
593
607
+ Sync ,
594
608
{
609
+ type SubscriptionClient = Self ;
610
+
595
611
/// Send a subscription request to the server.
596
612
///
597
613
/// The `subscribe_method` and `params` are used to ask for the subscription towards the
@@ -601,7 +617,7 @@ where
601
617
subscribe_method : & ' a str ,
602
618
params : Params ,
603
619
unsubscribe_method : & ' a str ,
604
- ) -> impl Future < Output = Result < Subscription < Notif > , Error > > + Send
620
+ ) -> impl Future < Output = Result < Subscription < Self :: SubscriptionClient , Notif > , Error > > + Send
605
621
where
606
622
Params : ToRpcParams + Send ,
607
623
Notif : DeserializeOwned ,
@@ -611,8 +627,8 @@ where
611
627
return Err ( RegisterMethodError :: SubscriptionNameConflict ( unsubscribe_method. to_owned ( ) ) . into ( ) ) ;
612
628
}
613
629
614
- let req_id_sub = self . id_manager . next_request_id ( ) ;
615
- let req_id_unsub = self . id_manager . next_request_id ( ) ;
630
+ let req_id_sub = self . inner . id_manager . next_request_id ( ) ;
631
+ let req_id_unsub = self . inner . id_manager . next_request_id ( ) ;
616
632
let params = params. to_rpc_params ( ) ?;
617
633
618
634
let mut ext = Extensions :: new ( ) ;
@@ -626,24 +642,25 @@ where
626
642
extensions : ext,
627
643
} ;
628
644
629
- let fut = self . service . call ( req) ;
645
+ let fut = self . inner . service . call ( req) ;
630
646
let sub = self
631
647
. run_future_until_timeout ( fut)
632
648
. await ?
633
649
. into_subscription ( )
634
650
. expect ( "Extensions set to subscription, must return subscription; qed" ) ;
635
- Ok ( Subscription :: new ( self . to_back . clone ( ) , sub. stream , SubscriptionKind :: Subscription ( sub. sub_id ) ) )
651
+ Ok ( Subscription :: new ( self . clone ( ) , self . inner . to_back . clone ( ) , sub. stream , SubscriptionKind :: Subscription ( sub. sub_id ) ) )
636
652
}
637
653
}
638
654
639
655
/// Subscribe to a specific method.
640
- fn subscribe_to_method < N > ( & self , method : & str ) -> impl Future < Output = Result < Subscription < N > , Error > > + Send
656
+ fn subscribe_to_method < N > ( & self , method : & str ) -> impl Future < Output = Result < Subscription < Self :: SubscriptionClient , N > , Error > > + Send
641
657
where
642
658
N : DeserializeOwned ,
643
659
{
644
660
async {
645
661
let ( send_back_tx, send_back_rx) = oneshot:: channel ( ) ;
646
662
if self
663
+ . inner
647
664
. to_back
648
665
. clone ( )
649
666
. send ( FrontToBack :: RegisterNotification ( RegisterNotificationMessage {
@@ -656,15 +673,15 @@ where
656
673
return Err ( self . on_disconnect ( ) . await ) ;
657
674
}
658
675
659
- let res = call_with_timeout ( self . request_timeout , send_back_rx) . await ;
676
+ let res = call_with_timeout ( self . inner . request_timeout , send_back_rx) . await ;
660
677
661
678
let ( rx, method) = match res {
662
679
Ok ( Ok ( val) ) => val,
663
680
Ok ( Err ( err) ) => return Err ( err) ,
664
681
Err ( _) => return Err ( self . on_disconnect ( ) . await ) ,
665
682
} ;
666
683
667
- Ok ( Subscription :: new ( self . to_back . clone ( ) , rx, SubscriptionKind :: Method ( method) ) )
684
+ Ok ( Subscription :: new ( self . clone ( ) , self . inner . to_back . clone ( ) , rx, SubscriptionKind :: Method ( method) ) )
668
685
}
669
686
}
670
687
}
0 commit comments