Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(redis): Make connection pool configurable [INGEST-1557] #1418

Merged
merged 9 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Refactor tokio-based Addr from healthcheck to be generic. ([#1405](https://github.com/relay/pull/1405))

**Features**:

- Make Redis connection pool configurable. ([#1418](https://github.com/getsentry/relay/pull/1418))

## 22.8.0

**Features**:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions relay-quotas/src/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod tests {
use std::time::{SystemTime, UNIX_EPOCH};

use relay_common::{ProjectId, ProjectKey};
use relay_redis::redis::Commands;
use relay_redis::{redis::Commands, RedisConfigOptions};

use crate::quota::{DataCategories, DataCategory, ReasonCode, Scoping};
use crate::rate_limit::RateLimitScope;
Expand All @@ -261,7 +261,7 @@ mod tests {
.unwrap_or_else(|_| "redis://127.0.0.1:6379".to_owned());

RedisRateLimiter {
pool: RedisPool::single(&url).unwrap(),
pool: RedisPool::single(&url, &RedisConfigOptions::default()).unwrap(),
script: Arc::new(load_lua_script()),
max_limit: None,
}
Expand Down
3 changes: 3 additions & 0 deletions relay-redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ serde = { version = "1.0.114", features = ["derive"] }
[features]
default = []
impl = ["r2d2", "redis"]

[dev-dependencies]
serde_yaml = "0.8.13"
97 changes: 97 additions & 0 deletions relay-redis/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
use serde::{Deserialize, Serialize};

const fn default_max_connections() -> u32 {
24
}

/// Additional configuration options for a redis client.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct RedisConfigOptions {
/// Maximum number of connections managed by the pool.
#[serde(default = "default_max_connections")]
pub max_connections: u32,
}

impl Default for RedisConfigOptions {
fn default() -> Self {
Self {
max_connections: default_max_connections(),
}
}
}

/// Configuration for connecting a redis client.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(untagged)]
Expand All @@ -10,10 +30,87 @@ pub enum RedisConfig {
///
/// This can also be a single node which is configured in cluster mode.
cluster_nodes: Vec<String>,

/// Additional configuration options for the redis client and a connections pool.
#[serde(flatten)]
options: RedisConfigOptions,
},

/// Connect to a single Redis instance.
///
/// Contains the `redis://` url to the node.
Single(String),

/// Connect to a single Redis instance.
///
/// Allows to provide more configuration options, e.g. `max_connections`.
SingleWithOpts {
/// Containes the `redis://` url to the node.
server: String,

/// Additional configuration options for the redis client and a connections pool.
#[serde(flatten)]
options: RedisConfigOptions,
},
}

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

#[test]
fn test_redis_single_opts() {
let yaml = r###"
server: "redis://127.0.0.1:6379"
max_connections: 42
"###;

let config: RedisConfig = serde_yaml::from_str(yaml)
.expect("Parsed processing redis config: single with options");

match config {
RedisConfig::SingleWithOpts { server, options } => {
assert_eq!(options.max_connections, 42);
assert_eq!(server, "redis://127.0.0.1:6379");
}
e => panic!("Expected RedisConfig::SingleWithOpts but got {:?}", e),
}
}

#[test]
fn test_redis_single_opts_default() {
let yaml = r###"
server: "redis://127.0.0.1:6379"
"###;

let config: RedisConfig = serde_yaml::from_str(yaml)
.expect("Parsed processing redis config: single with options");

match config {
RedisConfig::SingleWithOpts { options, .. } => {
// check if all the defaults are correctly set
assert_eq!(options.max_connections, 24);
}
e => panic!("Expected RedisConfig::SingleWithOpts but got {:?}", e),
}
}

// To make sure that we have backwards compatibility and still support the redis configuration
// when the single `redis://...` address is provided.
#[test]
fn test_redis_single() {
let yaml = r###"
"redis://127.0.0.1:6379"
"###;

let config: RedisConfig = serde_yaml::from_str(yaml)
.expect("Parsed processing redis config: single with options");

match config {
RedisConfig::Single(server) => {
assert_eq!(server, "redis://127.0.0.1:6379");
}
e => panic!("Expected RedisConfig::Single but got {:?}", e),
}
}
}
23 changes: 15 additions & 8 deletions relay-redis/src/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use failure::Fail;
use r2d2::{Pool, PooledConnection};
use redis::ConnectionLike;

use crate::config::RedisConfig;
use crate::config::{RedisConfig, RedisConfigOptions};

pub use redis;

Expand Down Expand Up @@ -115,18 +115,25 @@ impl RedisPool {
/// Creates a `RedisPool` from configuration.
pub fn new(config: &RedisConfig) -> Result<Self, RedisError> {
match config {
RedisConfig::Cluster { ref cluster_nodes } => {
RedisConfig::Cluster {
ref cluster_nodes,
ref options,
} => {
let servers = cluster_nodes.iter().map(String::as_str).collect();
Self::cluster(servers)
Self::cluster(servers, options)
}
RedisConfig::Single(ref server) => Self::single(server),
RedisConfig::Single(ref server) => Self::single(server, &RedisConfigOptions::default()),
RedisConfig::SingleWithOpts {
ref server,
ref options,
} => Self::single(server, options),
}
}

/// Creates a `RedisPool` in cluster configuration.
pub fn cluster(servers: Vec<&str>) -> Result<Self, RedisError> {
pub fn cluster(servers: Vec<&str>, opts: &RedisConfigOptions) -> Result<Self, RedisError> {
let pool = Pool::builder()
.max_size(24)
.max_size(opts.max_connections)
.test_on_check_out(false)
.build(redis::cluster::ClusterClient::open(servers).map_err(RedisError::Redis)?)
.map_err(RedisError::Pool)?;
Expand All @@ -136,9 +143,9 @@ impl RedisPool {
}

/// Creates a `RedisPool` in single-node configuration.
pub fn single(server: &str) -> Result<Self, RedisError> {
pub fn single(server: &str, opts: &RedisConfigOptions) -> Result<Self, RedisError> {
let pool = Pool::builder()
.max_size(24)
.max_size(opts.max_connections)
.test_on_check_out(false)
.build(redis::Client::open(server).map_err(RedisError::Redis)?)
.map_err(RedisError::Pool)?;
Expand Down