@@ -7,7 +7,7 @@ use bb8_redis::RedisConnectionManager;
7
7
use redis:: { FromRedisValue , RedisError , RedisResult } ;
8
8
9
9
pub use self :: cluster:: RedisClusterConnectionManager ;
10
- use crate :: cfg:: Configuration ;
10
+ use crate :: cfg:: { CacheBackend , QueueBackend } ;
11
11
12
12
pub const REDIS_CONN_TIMEOUT : Duration = Duration :: from_secs ( 2 ) ;
13
13
@@ -20,6 +20,73 @@ pub enum RedisManager {
20
20
}
21
21
22
22
impl RedisManager {
23
+ async fn new_pooled ( dsn : & str , clustered : bool , max_conns : u16 ) -> Self {
24
+ if clustered {
25
+ let mgr = RedisClusterConnectionManager :: new ( dsn)
26
+ . expect ( "Error initializing redis cluster client" ) ;
27
+ let pool = bb8:: Pool :: builder ( )
28
+ . max_size ( max_conns. into ( ) )
29
+ . build ( mgr)
30
+ . await
31
+ . expect ( "Error initializing redis cluster connection pool" ) ;
32
+ let pool = ClusteredRedisPool { pool } ;
33
+ RedisManager :: Clustered ( pool)
34
+ } else {
35
+ let mgr = RedisConnectionManager :: new ( dsn) . expect ( "Error initializing redis client" ) ;
36
+ let pool = bb8:: Pool :: builder ( )
37
+ . max_size ( max_conns. into ( ) )
38
+ . build ( mgr)
39
+ . await
40
+ . expect ( "Error initializing redis connection pool" ) ;
41
+ let pool = NonClusteredRedisPool { pool } ;
42
+ RedisManager :: NonClustered ( pool)
43
+ }
44
+ }
45
+
46
+ async fn new_unpooled ( dsn : & str , clustered : bool ) -> Self {
47
+ if clustered {
48
+ let cli = redis:: cluster:: ClusterClient :: builder ( vec ! [ dsn] )
49
+ . retries ( 1 )
50
+ . connection_timeout ( REDIS_CONN_TIMEOUT )
51
+ . build ( )
52
+ . expect ( "Error initializing redis-unpooled cluster client" ) ;
53
+ let con = cli
54
+ . get_async_connection ( )
55
+ . await
56
+ . expect ( "Failed to get redis-cluster-unpooled connection" ) ;
57
+ RedisManager :: ClusteredUnpooled ( ClusteredRedisUnpooled { con } )
58
+ } else {
59
+ let cli = redis:: Client :: open ( dsn) . expect ( "Error initializing redis unpooled client" ) ;
60
+ let con = redis:: aio:: ConnectionManager :: new_with_backoff_and_timeouts (
61
+ cli,
62
+ 2 ,
63
+ 100 ,
64
+ 1 ,
65
+ Duration :: MAX ,
66
+ REDIS_CONN_TIMEOUT ,
67
+ )
68
+ . await
69
+ . expect ( "Failed to get redis-unpooled connection manager" ) ;
70
+ RedisManager :: NonClusteredUnpooled ( NonClusteredRedisUnpooled { con } )
71
+ }
72
+ }
73
+
74
+ pub async fn from_cache_backend ( cache_backend : & CacheBackend < ' _ > ) -> Self {
75
+ match cache_backend {
76
+ CacheBackend :: Redis ( dsn) => Self :: new_unpooled ( dsn, false ) . await ,
77
+ CacheBackend :: RedisCluster ( dsn) => Self :: new_unpooled ( dsn, true ) . await ,
78
+ _ => panic ! ( "Queue type not supported with redis" ) ,
79
+ }
80
+ }
81
+
82
+ pub async fn from_queue_backend ( queue_backend : & QueueBackend < ' _ > , max_conns : u16 ) -> Self {
83
+ match queue_backend {
84
+ QueueBackend :: Redis ( dsn) => Self :: new_pooled ( dsn, false , max_conns) . await ,
85
+ QueueBackend :: RedisCluster ( dsn) => Self :: new_pooled ( dsn, true , max_conns) . await ,
86
+ _ => panic ! ( "Queue type not supported with redis" ) ,
87
+ }
88
+ }
89
+
23
90
pub async fn get ( & self ) -> Result < PooledConnection < ' _ > , RunError < RedisError > > {
24
91
match self {
25
92
Self :: Clustered ( pool) => pool. get ( ) . await ,
@@ -230,93 +297,11 @@ impl ClusteredUnpooledConnection {
230
297
}
231
298
}
232
299
233
- async fn new_redis_pool_helper (
234
- redis_dsn : & str ,
235
- clustered : bool ,
236
- max_connections : u16 ,
237
- ) -> RedisManager {
238
- if clustered {
239
- let mgr = RedisClusterConnectionManager :: new ( redis_dsn)
240
- . expect ( "Error initializing redis cluster client" ) ;
241
- let pool = bb8:: Pool :: builder ( )
242
- . max_size ( max_connections. into ( ) )
243
- . build ( mgr)
244
- . await
245
- . expect ( "Error initializing redis cluster connection pool" ) ;
246
- let pool = ClusteredRedisPool { pool } ;
247
- RedisManager :: Clustered ( pool)
248
- } else {
249
- let mgr = RedisConnectionManager :: new ( redis_dsn) . expect ( "Error initializing redis client" ) ;
250
- let pool = bb8:: Pool :: builder ( )
251
- . max_size ( max_connections. into ( ) )
252
- . build ( mgr)
253
- . await
254
- . expect ( "Error initializing redis connection pool" ) ;
255
- let pool = NonClusteredRedisPool { pool } ;
256
- RedisManager :: NonClustered ( pool)
257
- }
258
- }
259
-
260
- async fn new_redis_unpooled_helper ( redis_dsn : & str , clustered : bool ) -> RedisManager {
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
- RedisManager :: 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
- )
282
- . await
283
- . expect ( "Failed to get redis-unpooled connection manager" ) ;
284
- RedisManager :: NonClusteredUnpooled ( NonClusteredRedisUnpooled { con } )
285
- }
286
- }
287
-
288
- pub async fn new_redis_clustered_pooled ( redis_dsn : & str , cfg : & Configuration ) -> RedisManager {
289
- new_redis_pool_helper ( redis_dsn, true , cfg. redis_pool_max_size ) . await
290
- }
291
-
292
- pub async fn new_redis_clustered_unpooled ( redis_dsn : & str ) -> RedisManager {
293
- new_redis_unpooled_helper ( redis_dsn, true ) . await
294
- }
295
-
296
- pub async fn new_redis_pooled ( redis_dsn : & str , cfg : & Configuration ) -> RedisManager {
297
- new_redis_pool_helper ( redis_dsn, false , cfg. redis_pool_max_size ) . await
298
- }
299
-
300
- pub async fn new_redis_unpooled ( redis_dsn : & str ) -> RedisManager {
301
- new_redis_unpooled_helper ( redis_dsn, false ) . await
302
- }
303
-
304
300
#[ cfg( test) ]
305
301
mod tests {
306
302
use redis:: AsyncCommands ;
307
303
308
304
use super :: RedisManager ;
309
- use crate :: cfg:: { CacheType , Configuration } ;
310
-
311
- async fn get_pool ( redis_dsn : & str , cfg : & Configuration ) -> RedisManager {
312
- match cfg. cache_type {
313
- CacheType :: RedisCluster => super :: new_redis_clustered_unpooled ( redis_dsn) . await ,
314
- CacheType :: Redis => super :: new_redis_unpooled ( redis_dsn) . await ,
315
- _ => panic ! (
316
- "This test should only be run when redis is configured as the cache provider"
317
- ) ,
318
- }
319
- }
320
305
321
306
// Ensure basic set/get works -- should test sharding as well:
322
307
#[ tokio:: test]
@@ -326,8 +311,8 @@ mod tests {
326
311
dotenvy:: dotenv ( ) . ok ( ) ;
327
312
let cfg = crate :: cfg:: load ( ) . unwrap ( ) ;
328
313
329
- let pool = get_pool ( cfg. redis_dsn . as_ref ( ) . unwrap ( ) . as_str ( ) , & cfg ) . await ;
330
- let mut conn = pool . get ( ) . await . unwrap ( ) ;
314
+ let mgr = RedisManager :: from_cache_backend ( & cfg. cache_backend ( ) ) . await ;
315
+ let mut conn = mgr . get ( ) . await . unwrap ( ) ;
331
316
332
317
for ( val, key) in "abcdefghijklmnopqrstuvwxyz" . chars ( ) . enumerate ( ) {
333
318
let key = key. to_string ( ) ;
0 commit comments