@@ -402,7 +402,7 @@ pub mod tests_postgres {
402
402
pub mod redis_pool {
403
403
404
404
use dashmap:: DashMap ;
405
- use deadpool:: managed:: { Manager as ManagerTrait , RecycleError , RecycleResult } ;
405
+ use deadpool:: managed:: { Manager as ManagerTrait , RecycleResult } ;
406
406
use thiserror:: Error ;
407
407
408
408
use crate :: db:: redis_connection;
@@ -420,6 +420,7 @@ pub mod redis_pool {
420
420
#[ derive( Clone ) ]
421
421
pub struct Database {
422
422
available : bool ,
423
+ index : u8 ,
423
424
pub connection : MultiplexedConnection ,
424
425
}
425
426
@@ -473,11 +474,10 @@ pub mod redis_pool {
473
474
474
475
#[ derive( Debug , Error ) ]
475
476
pub enum Error {
476
- // when we can't create more databases and all are used
477
- #[ error( "No more databases can be created" ) ]
478
- OutOfBound ,
479
477
#[ error( "A redis error occurred" ) ]
480
478
Redis ( #[ from] RedisError ) ,
479
+ #[ error( "Creation of new database connection failed" ) ]
480
+ CreationFailed ,
481
481
}
482
482
483
483
#[ async_trait]
@@ -495,17 +495,24 @@ pub mod redis_pool {
495
495
return Ok ( database. clone ( ) ) ;
496
496
}
497
497
// if Some but not available, skip it
498
- Some ( _) => continue ,
499
- None => {
498
+ Some ( database) if !database. available => continue ,
499
+ // if there is no connection or it's available
500
+ // always create a new redis connection because of a known issue in redis
501
+ // see https://github.com/mitsuhiko/redis-rs/issues/325
502
+ _ => {
500
503
let mut redis_conn =
501
- redis_connection ( & format ! ( "{}{}" , Self :: URL , record. key( ) ) ) . await ?;
504
+ redis_connection ( & format ! ( "{}{}" , Self :: URL , record. key( ) ) )
505
+ . await
506
+ . expect ( "Should connect" ) ;
502
507
503
508
// run `FLUSHDB` to clean any leftovers of previous tests
504
509
// even from different test runs as there might be leftovers
505
- Self :: flush_db ( & mut redis_conn) . await ?;
510
+ // flush never fails as an operation
511
+ Self :: flush_db ( & mut redis_conn) . await . expect ( "Should flush" ) ;
506
512
507
513
let database = Database {
508
514
available : false ,
515
+ index : * record. key ( ) ,
509
516
connection : redis_conn,
510
517
} ;
511
518
@@ -516,15 +523,21 @@ pub mod redis_pool {
516
523
}
517
524
}
518
525
519
- Err ( Error :: OutOfBound )
526
+ Err ( Error :: CreationFailed )
520
527
}
521
528
522
529
async fn recycle ( & self , database : & mut Database ) -> RecycleResult < Self :: Error > {
523
- // run `FLUSHDB` to clean any leftovers of previous tests
524
- Self :: flush_db ( & mut database. connection )
530
+ // always make a new connection because of know redis crate issue
531
+ // see https://github.com/mitsuhiko/redis-rs/issues/325
532
+ let connection = redis_connection ( & format ! ( "{}{}" , Self :: URL , database. index) )
525
533
. await
526
- . map_err ( RecycleError :: Backend ) ?;
534
+ . expect ( "Should connect" ) ;
535
+ // make the database available
527
536
database. available = true ;
537
+ database. connection = connection;
538
+ Self :: flush_db ( & mut database. connection )
539
+ . await
540
+ . expect ( "Should flush" ) ;
528
541
529
542
Ok ( ( ) )
530
543
}
0 commit comments