@@ -8,39 +8,42 @@ use super::cmd::Command;
8
8
use super :: codec:: { Codec , Request , Response } ;
9
9
use super :: errors:: { CommandError , Error } ;
10
10
11
- type Queue = Rc < RefCell < VecDeque < pool:: Sender < Result < Response , Error > > > > > ;
12
-
13
11
#[ derive( Clone ) ]
14
12
/// Shared redis client
15
- pub struct Client {
13
+ pub struct Client ( Rc < Inner > ) ;
14
+
15
+ struct Inner {
16
16
state : State ,
17
- queue : Queue ,
17
+ queue : RefCell < VecDeque < pool :: Sender < Result < Response , Error > > > > ,
18
18
pool : pool:: Pool < Result < Response , Error > > ,
19
19
}
20
20
21
21
impl Client {
22
22
pub ( crate ) fn new ( state : State ) -> Self {
23
- let queue: Queue = Rc :: new ( RefCell :: new ( VecDeque :: new ( ) ) ) ;
23
+ let inner = Rc :: new ( Inner {
24
+ state,
25
+ pool : pool:: new ( ) ,
26
+ queue : RefCell :: new ( VecDeque :: new ( ) ) ,
27
+ } ) ;
28
+ let inner2 = inner. clone ( ) ;
24
29
25
30
// read redis response task
26
- let state2 = state. clone ( ) ;
27
- let queue2 = queue. clone ( ) ;
28
31
ntex:: rt:: spawn ( async move {
29
- let read = state2 . read ( ) ;
32
+ let read = inner . state . read ( ) ;
30
33
31
34
poll_fn ( |cx| {
32
35
loop {
33
36
match read. decode ( & Codec ) {
34
37
Err ( e) => {
35
- if let Some ( tx) = queue2 . borrow_mut ( ) . pop_front ( ) {
38
+ if let Some ( tx) = inner . queue . borrow_mut ( ) . pop_front ( ) {
36
39
let _ = tx. send ( Err ( e) ) ;
37
40
}
38
- queue2 . borrow_mut ( ) . clear ( ) ;
39
- state2 . shutdown_io ( ) ;
41
+ inner . queue . borrow_mut ( ) . clear ( ) ;
42
+ inner . state . shutdown_io ( ) ;
40
43
return Poll :: Ready ( ( ) ) ;
41
44
}
42
45
Ok ( Some ( item) ) => {
43
- if let Some ( tx) = queue2 . borrow_mut ( ) . pop_front ( ) {
46
+ if let Some ( tx) = inner . queue . borrow_mut ( ) . pop_front ( ) {
44
47
let _ = tx. send ( Ok ( item) ) ;
45
48
} else {
46
49
log:: error!( "Unexpected redis response: {:?}" , item) ;
@@ -50,28 +53,24 @@ impl Client {
50
53
}
51
54
}
52
55
53
- if !state2 . is_open ( ) {
56
+ if !inner . state . is_open ( ) {
54
57
return Poll :: Ready ( ( ) ) ;
55
58
}
56
- state2 . register_dispatcher ( cx. waker ( ) ) ;
59
+ inner . state . register_dispatcher ( cx. waker ( ) ) ;
57
60
Poll :: Pending
58
61
} )
59
62
. await
60
63
} ) ;
61
64
62
- Client {
63
- state,
64
- queue,
65
- pool : pool:: new ( ) ,
66
- }
65
+ Client ( inner2)
67
66
}
68
67
69
68
/// Execute redis command
70
69
pub fn exec < T > ( & self , cmd : T ) -> impl Future < Output = Result < T :: Output , CommandError > >
71
70
where
72
71
T : Command ,
73
72
{
74
- let is_open = self . state . is_open ( ) ;
73
+ let is_open = self . 0 . state . is_open ( ) ;
75
74
let fut = self . call ( cmd. to_request ( ) ) ;
76
75
77
76
async move {
@@ -93,7 +92,7 @@ impl Client {
93
92
94
93
/// Returns true if underlying transport is connected to redis
95
94
pub fn is_connected ( & self ) -> bool {
96
- self . state . is_open ( )
95
+ self . 0 . state . is_open ( )
97
96
}
98
97
}
99
98
@@ -104,19 +103,19 @@ impl Service for Client {
104
103
type Future = Either < CommandResult , Ready < Response , Error > > ;
105
104
106
105
fn poll_ready ( & self , _cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
107
- if !self . state . is_open ( ) {
106
+ if !self . 0 . state . is_open ( ) {
108
107
Poll :: Ready ( Err ( Error :: Disconnected ) )
109
108
} else {
110
109
Poll :: Ready ( Ok ( ( ) ) )
111
110
}
112
111
}
113
112
114
113
fn call ( & self , req : Request ) -> Self :: Future {
115
- if let Err ( e) = self . state . write ( ) . encode ( req, & Codec ) {
114
+ if let Err ( e) = self . 0 . state . write ( ) . encode ( req, & Codec ) {
116
115
Either :: Right ( Ready :: Err ( e) )
117
116
} else {
118
- let ( tx, rx) = self . pool . channel ( ) ;
119
- self . queue . borrow_mut ( ) . push_back ( tx) ;
117
+ let ( tx, rx) = self . 0 . pool . channel ( ) ;
118
+ self . 0 . queue . borrow_mut ( ) . push_back ( tx) ;
120
119
Either :: Left ( CommandResult { rx } )
121
120
}
122
121
}
@@ -125,7 +124,7 @@ impl Service for Client {
125
124
impl fmt:: Debug for Client {
126
125
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
127
126
f. debug_struct ( "Client" )
128
- . field ( "connected" , & self . state . is_open ( ) )
127
+ . field ( "connected" , & self . 0 . state . is_open ( ) )
129
128
. finish ( )
130
129
}
131
130
}
0 commit comments