@@ -16,6 +16,7 @@ pub enum RedisPool {
16
16
Clustered ( ClusteredRedisPool ) ,
17
17
ClusteredUnpooled ( ClusteredRedisUnpooled ) ,
18
18
NonClustered ( NonClusteredRedisPool ) ,
19
+ NonClusteredUnpooled ( NonClusteredRedisUnpooled ) ,
19
20
}
20
21
21
22
impl RedisPool {
@@ -24,6 +25,7 @@ impl RedisPool {
24
25
Self :: Clustered ( pool) => pool. get ( ) . await ,
25
26
Self :: NonClustered ( pool) => pool. get ( ) . await ,
26
27
Self :: ClusteredUnpooled ( pool) => pool. get ( ) . await ,
28
+ Self :: NonClusteredUnpooled ( pool) => pool. get ( ) . await ,
27
29
}
28
30
}
29
31
}
@@ -63,6 +65,27 @@ impl std::fmt::Debug for ClusteredRedisUnpooled {
63
65
}
64
66
}
65
67
68
+ #[ derive( Clone ) ]
69
+ pub struct NonClusteredRedisUnpooled {
70
+ con : redis:: aio:: ConnectionManager ,
71
+ }
72
+
73
+ impl NonClusteredRedisUnpooled {
74
+ pub async fn get ( & self ) -> Result < PooledConnection < ' _ > , RunError < RedisError > > {
75
+ Ok ( PooledConnection :: NonClusteredUnpooled (
76
+ NonClusteredUnpooledConnection {
77
+ con : self . con . clone ( ) ,
78
+ } ,
79
+ ) )
80
+ }
81
+ }
82
+
83
+ impl std:: fmt:: Debug for NonClusteredRedisUnpooled {
84
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
85
+ f. debug_struct ( "NonClusteredRedisUnpooled" ) . finish ( )
86
+ }
87
+ }
88
+
66
89
#[ derive( Clone , Debug ) ]
67
90
pub struct NonClusteredRedisPool {
68
91
pool : Pool < RedisConnectionManager > ,
@@ -80,6 +103,7 @@ pub enum PooledConnection<'a> {
80
103
Clustered ( ClusteredPooledConnection < ' a > ) ,
81
104
ClusteredUnpooled ( ClusteredUnpooledConnection ) ,
82
105
NonClustered ( NonClusteredPooledConnection < ' a > ) ,
106
+ NonClusteredUnpooled ( NonClusteredUnpooledConnection ) ,
83
107
}
84
108
85
109
impl PooledConnection < ' _ > {
@@ -104,6 +128,7 @@ impl redis::aio::ConnectionLike for PooledConnection<'_> {
104
128
PooledConnection :: Clustered ( conn) => conn. con . req_packed_command ( cmd) ,
105
129
PooledConnection :: NonClustered ( conn) => conn. con . req_packed_command ( cmd) ,
106
130
PooledConnection :: ClusteredUnpooled ( conn) => conn. con . req_packed_command ( cmd) ,
131
+ PooledConnection :: NonClusteredUnpooled ( conn) => conn. con . req_packed_command ( cmd) ,
107
132
}
108
133
}
109
134
@@ -121,6 +146,9 @@ impl redis::aio::ConnectionLike for PooledConnection<'_> {
121
146
PooledConnection :: ClusteredUnpooled ( conn) => {
122
147
conn. con . req_packed_commands ( cmd, offset, count)
123
148
}
149
+ PooledConnection :: NonClusteredUnpooled ( conn) => {
150
+ conn. con . req_packed_commands ( cmd, offset, count)
151
+ }
124
152
}
125
153
}
126
154
@@ -129,6 +157,7 @@ impl redis::aio::ConnectionLike for PooledConnection<'_> {
129
157
PooledConnection :: Clustered ( conn) => conn. con . get_db ( ) ,
130
158
PooledConnection :: NonClustered ( conn) => conn. con . get_db ( ) ,
131
159
PooledConnection :: ClusteredUnpooled ( conn) => conn. con . get_db ( ) ,
160
+ PooledConnection :: NonClusteredUnpooled ( conn) => conn. con . get_db ( ) ,
132
161
}
133
162
}
134
163
}
@@ -150,6 +179,23 @@ impl<'a> NonClusteredPooledConnection<'a> {
150
179
}
151
180
}
152
181
182
+ pub struct NonClusteredUnpooledConnection {
183
+ con : redis:: aio:: ConnectionManager ,
184
+ }
185
+
186
+ impl NonClusteredUnpooledConnection {
187
+ pub async fn query_async < T : FromRedisValue > ( & mut self , cmd : redis:: Cmd ) -> RedisResult < T > {
188
+ cmd. query_async ( & mut self . con ) . await
189
+ }
190
+
191
+ pub async fn query_async_pipeline < T : FromRedisValue > (
192
+ & mut self ,
193
+ pipe : redis:: Pipeline ,
194
+ ) -> RedisResult < T > {
195
+ pipe. query_async ( & mut self . con ) . await
196
+ }
197
+ }
198
+
153
199
pub struct ClusteredPooledConnection < ' a > {
154
200
con : bb8:: PooledConnection < ' a , RedisClusterConnectionManager > ,
155
201
}
@@ -211,31 +257,50 @@ async fn new_redis_pool_helper(
211
257
}
212
258
}
213
259
214
- async fn new_redis_unpooled_helper ( redis_dsn : & str ) -> RedisPool {
215
- let cli = redis:: cluster:: ClusterClient :: builder ( vec ! [ redis_dsn] )
216
- . retries ( 1 )
217
- . connection_timeout ( REDIS_CONN_TIMEOUT )
218
- . build ( )
219
- . expect ( "Error initializing redis-unpooled client" ) ;
220
- let con = cli
221
- . get_async_connection ( )
260
+ async fn new_redis_unpooled_helper ( redis_dsn : & str , clustered : bool ) -> RedisPool {
261
+ if clustered {
262
+ let cli = redis:: cluster:: ClusterClient :: builder ( vec ! [ redis_dsn] )
263
+ . retries ( 1 )
264
+ . connection_timeout ( REDIS_CONN_TIMEOUT )
265
+ . build ( )
266
+ . expect ( "Error initializing redis-unpooled cluster client" ) ;
267
+ let con = cli
268
+ . get_async_connection ( )
269
+ . await
270
+ . expect ( "Failed to get redis-cluster-unpooled connection" ) ;
271
+ RedisPool :: ClusteredUnpooled ( ClusteredRedisUnpooled { con } )
272
+ } else {
273
+ let cli = redis:: Client :: open ( redis_dsn) . expect ( "Error initializing redis unpooled client" ) ;
274
+ let con = redis:: aio:: ConnectionManager :: new_with_backoff_and_timeouts (
275
+ cli,
276
+ 2 ,
277
+ 100 ,
278
+ 1 ,
279
+ Duration :: MAX ,
280
+ REDIS_CONN_TIMEOUT ,
281
+ )
222
282
. await
223
- . expect ( "Failed to get redis-cluster-unpooled connection" ) ;
224
- RedisPool :: ClusteredUnpooled ( ClusteredRedisUnpooled { con } )
283
+ . expect ( "Failed to get redis-unpooled connection manager" ) ;
284
+ RedisPool :: NonClusteredUnpooled ( NonClusteredRedisUnpooled { con } )
285
+ }
225
286
}
226
287
227
- pub async fn new_redis_pool_clustered ( redis_dsn : & str , cfg : & Configuration ) -> RedisPool {
288
+ pub async fn new_redis_clustered_pooled ( redis_dsn : & str , cfg : & Configuration ) -> RedisPool {
228
289
new_redis_pool_helper ( redis_dsn, true , cfg. redis_pool_max_size ) . await
229
290
}
230
291
231
292
pub async fn new_redis_clustered_unpooled ( redis_dsn : & str ) -> RedisPool {
232
- new_redis_unpooled_helper ( redis_dsn) . await
293
+ new_redis_unpooled_helper ( redis_dsn, true ) . await
233
294
}
234
295
235
- pub async fn new_redis_pool ( redis_dsn : & str , cfg : & Configuration ) -> RedisPool {
296
+ pub async fn new_redis_pooled ( redis_dsn : & str , cfg : & Configuration ) -> RedisPool {
236
297
new_redis_pool_helper ( redis_dsn, false , cfg. redis_pool_max_size ) . await
237
298
}
238
299
300
+ pub async fn new_redis_unpooled ( redis_dsn : & str ) -> RedisPool {
301
+ new_redis_unpooled_helper ( redis_dsn, false ) . await
302
+ }
303
+
239
304
#[ cfg( test) ]
240
305
mod tests {
241
306
use redis:: AsyncCommands ;
@@ -246,7 +311,7 @@ mod tests {
246
311
async fn get_pool ( redis_dsn : & str , cfg : & Configuration ) -> RedisPool {
247
312
match cfg. cache_type {
248
313
CacheType :: RedisCluster => super :: new_redis_clustered_unpooled ( redis_dsn) . await ,
249
- CacheType :: Redis => super :: new_redis_pool ( redis_dsn, cfg ) . await ,
314
+ CacheType :: Redis => super :: new_redis_unpooled ( redis_dsn) . await ,
250
315
_ => panic ! (
251
316
"This test should only be run when redis is configured as the cache provider"
252
317
) ,
0 commit comments