Skip to content

Commit aae1400

Browse files
authored
Merge pull request #427 from AdExNetwork/fix-redis-test-pool-broken-pipe
Fix redis test pool broken pipe
2 parents 6287165 + e9335b7 commit aae1400

File tree

3 files changed

+50
-34
lines changed

3 files changed

+50
-34
lines changed

sentry/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ migrant_lib = { version = "^0.32", features = ["d-postgres"] }
3636
# Logger
3737
slog = { version = "^2.2.3", features = ["max_level_trace"] }
3838
# Serde
39-
serde = { version = "^1.0", features = ['derive'] }
39+
serde = { version = "^1.0", features = ["derive"] }
4040
serde_json = "^1.0"
4141
serde_urlencoded = "^0.7"
4242
# Other

sentry/src/db.rs

+48-32
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ pub mod tests_postgres {
402402
pub mod redis_pool {
403403

404404
use dashmap::DashMap;
405-
use deadpool::managed::{Manager as ManagerTrait, RecycleError, RecycleResult};
405+
use deadpool::managed::{Manager as ManagerTrait, RecycleResult};
406406
use thiserror::Error;
407407

408408
use crate::db::redis_connection;
@@ -420,6 +420,7 @@ pub mod redis_pool {
420420
#[derive(Clone)]
421421
pub struct Database {
422422
available: bool,
423+
index: u8,
423424
pub connection: MultiplexedConnection,
424425
}
425426

@@ -475,6 +476,8 @@ pub mod redis_pool {
475476
pub enum Error {
476477
#[error("A redis error occurred")]
477478
Redis(#[from] RedisError),
479+
#[error("Creation of new database connection failed")]
480+
CreationFailed,
478481
}
479482

480483
#[async_trait]
@@ -483,45 +486,58 @@ pub mod redis_pool {
483486
type Error = Error;
484487

485488
async fn create(&self) -> Result<Self::Type, Self::Error> {
486-
loop {
487-
for mut record in self.connections.iter_mut() {
488-
let database = record.value_mut().as_mut();
489-
490-
match database {
491-
Some(database) if database.available => {
492-
database.available = false;
493-
return Ok(database.clone());
494-
}
495-
// if Some but not available, skip it
496-
Some(_) => continue,
497-
None => {
498-
let mut redis_conn =
499-
redis_connection(&format!("{}{}", Self::URL, record.key())).await?;
500-
501-
// run `FLUSHDB` to clean any leftovers of previous tests
502-
// even from different test runs as there might be leftovers
503-
Self::flush_db(&mut redis_conn).await?;
504-
505-
let database = Database {
506-
available: false,
507-
connection: redis_conn,
508-
};
509-
510-
*record.value_mut() = Some(database.clone());
511-
512-
return Ok(database);
513-
}
489+
for mut record in self.connections.iter_mut() {
490+
let database = record.value_mut().as_mut();
491+
492+
match database {
493+
Some(database) if database.available => {
494+
database.available = false;
495+
return Ok(database.clone());
496+
}
497+
// if Some but not available, skip it
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+
_ => {
503+
let mut redis_conn =
504+
redis_connection(&format!("{}{}", Self::URL, record.key()))
505+
.await
506+
.expect("Should connect");
507+
508+
// run `FLUSHDB` to clean any leftovers of previous tests
509+
// even from different test runs as there might be leftovers
510+
// flush never fails as an operation
511+
Self::flush_db(&mut redis_conn).await.expect("Should flush");
512+
513+
let database = Database {
514+
available: false,
515+
index: *record.key(),
516+
connection: redis_conn,
517+
};
518+
519+
*record.value_mut() = Some(database.clone());
520+
521+
return Ok(database);
514522
}
515523
}
516524
}
525+
526+
Err(Error::CreationFailed)
517527
}
518528

519529
async fn recycle(&self, database: &mut Database) -> RecycleResult<Self::Error> {
520-
// run `FLUSHDB` to clean any leftovers of previous tests
521-
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))
522533
.await
523-
.map_err(RecycleError::Backend)?;
534+
.expect("Should connect");
535+
// make the database available
524536
database.available = true;
537+
database.connection = connection;
538+
Self::flush_db(&mut database.connection)
539+
.await
540+
.expect("Should flush");
525541

526542
Ok(())
527543
}

sentry/src/routes/campaign.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ mod test {
792792
spender: create.creator,
793793
channel: create.channel.clone(),
794794
deposit: Deposit {
795-
// a deposit equal to double the Campaign Budget
795+
// a deposit 4 times larger than the Campaign Budget
796796
total: UnifiedNum::from(200_000_000_000),
797797
still_on_create2: UnifiedNum::from(0),
798798
},

0 commit comments

Comments
 (0)