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