Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/game_api/src/http/player_finished.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ where
.ignore()
.zcount(&map_key, "-inf", new - 1);

let mut redis_conn = redis_pool.get().await.with_api_err()?;
Comment thread
ahmadbky marked this conversation as resolved.
let (count,): (i32,) = pipe.query_async(&mut redis_conn).await.with_api_err()?;
let new_rank = count + 1;

Expand Down
2 changes: 0 additions & 2 deletions crates/records_lib/src/mappack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,6 @@ async fn calc_scores<C: ConnectionTrait + StreamTrait>(

let mut scores = Vec::<PlayerScore>::with_capacity(mappack.len());

let mut redis_conn = redis_pool.get().await?;

for (i, map) in mappack.iter().enumerate() {
let mut query = Query::select();
query
Expand Down
37 changes: 33 additions & 4 deletions crates/records_lib/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use sea_orm::DbConn;

use crate::RedisPool;

const REDIS_POOL_MAX_SIZE: usize = 16;
const REDIS_POOL_WAIT_TIMEOUT_MS: u64 = 2_000;

/// Represents the database of the API, meaning an SQL database, and a Redis database.
pub struct Database {
/// The SQL database connection pool. This can also be a testing database [`DbConn::MockDatabaseConnection`].
Expand Down Expand Up @@ -160,10 +163,36 @@ impl Clone for Database {

/// Creates and returns the Redis pool with the provided URL.
pub fn get_redis_pool(url: String) -> Result<RedisPool, deadpool_redis::CreatePoolError> {
let cfg = deadpool_redis::Config {
let cfg = redis_pool_config(url);
cfg.create_pool(Some(Runtime::Tokio1))
}

fn redis_pool_config(url: String) -> deadpool_redis::Config {
let mut pool = deadpool_redis::PoolConfig::new(REDIS_POOL_MAX_SIZE);
pool.timeouts = deadpool_redis::Timeouts::wait_millis(REDIS_POOL_WAIT_TIMEOUT_MS);

deadpool_redis::Config {
url: Some(url),
connection: None,
pool: None,
};
cfg.create_pool(Some(Runtime::Tokio1))
pool: Some(pool),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn redis_pool_uses_fixed_size_and_wait_timeout() {
let cfg = redis_pool_config("redis://127.0.0.1:6379".to_string());
let pool = cfg.pool.expect("pool config should be set");

assert_eq!(pool.max_size, REDIS_POOL_MAX_SIZE);
assert_eq!(
pool.timeouts.wait,
Some(std::time::Duration::from_millis(REDIS_POOL_WAIT_TIMEOUT_MS))
);
assert!(pool.timeouts.create.is_none());
assert!(pool.timeouts.recycle.is_none());
}
}
Loading