Skip to content

Commit

Permalink
Clean up and fix deadpool-redis code
Browse files Browse the repository at this point in the history
  • Loading branch information
bikeshedder committed Dec 14, 2023
1 parent a700c55 commit a80415d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 210 deletions.
3 changes: 2 additions & 1 deletion redis/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Change Log

## v0.14.0
## unreleased

* Merge `deadpool-redis-cluster` into `deadpool-redis`.
* Remove `redis_cluster_async` dependency in favor of `redis::cluster` / `redis::cluster_async`.
- Update `redis` dependency to version `0.23`

## v0.13.0

Expand Down
189 changes: 3 additions & 186 deletions redis/src/cluster/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::{fmt, path::PathBuf};
pub use crate::config::ConfigError;
use crate::ConnectionInfo;

use redis::RedisError;
#[cfg(feature = "serde")]
use serde_1::{Deserialize, Serialize};

use super::{CreatePoolError, Pool, PoolBuilder, PoolConfig, RedisResult, Runtime};
use super::{CreatePoolError, Pool, PoolBuilder, PoolConfig, Runtime};

/// Configuration object.
///
Expand Down Expand Up @@ -117,183 +114,3 @@ impl Default for Config {
}
}
}

/// This is a 1:1 copy of the [`redis::ConnectionAddr`] enumeration.
/// This is duplicated here in order to add support for the
/// [`serde::Deserialize`] trait which is required for the [`serde`] support.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(crate = "serde_1"))]
pub enum ConnectionAddr {
/// Format for this is `(host, port)`.
Tcp(String, u16),

/// Format for this is `(host, port)`.
TcpTls {
/// Hostname.
host: String,

/// Port.
port: u16,

/// Disable hostname verification when connecting.
///
/// # Warning
///
/// You should think very carefully before you use this method. If
/// hostname verification is not used, any valid certificate for any
/// site will be trusted for use from any other. This introduces a
/// significant vulnerability to man-in-the-middle attacks.
insecure: bool,
},

/// Format for this is the path to the unix socket.
Unix(PathBuf),
}

impl Default for ConnectionAddr {
fn default() -> Self {
Self::Tcp("127.0.0.1".to_string(), 6379)
}
}

impl From<ConnectionAddr> for redis::ConnectionAddr {
fn from(addr: ConnectionAddr) -> Self {
match addr {
ConnectionAddr::Tcp(host, port) => Self::Tcp(host, port),
ConnectionAddr::TcpTls {
host,
port,
insecure,
} => Self::TcpTls {
host,
port,
insecure,
},
ConnectionAddr::Unix(path) => Self::Unix(path),
}
}
}

impl From<redis::ConnectionAddr> for ConnectionAddr {
fn from(addr: redis::ConnectionAddr) -> Self {
match addr {
redis::ConnectionAddr::Tcp(host, port) => Self::Tcp(host, port),
redis::ConnectionAddr::TcpTls {
host,
port,
insecure,
} => ConnectionAddr::TcpTls {
host,
port,
insecure,
},
redis::ConnectionAddr::Unix(path) => Self::Unix(path),
}
}
}

/// This is a 1:1 copy of the [`redis::ConnectionInfo`] struct.
/// This is duplicated here in order to add support for the
/// [`serde::Deserialize`] trait which is required for the [`serde`] support.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(crate = "serde_1"))]
pub struct ConnectionInfo {
/// A connection address for where to connect to.
pub addr: ConnectionAddr,

/// A boxed connection address for where to connect to.
#[cfg_attr(feature = "serde", serde(flatten))]
pub redis: RedisConnectionInfo,
}

impl From<ConnectionInfo> for redis::ConnectionInfo {
fn from(info: ConnectionInfo) -> Self {
Self {
addr: info.addr.into(),
redis: info.redis.into(),
}
}
}

impl From<redis::ConnectionInfo> for ConnectionInfo {
fn from(info: redis::ConnectionInfo) -> Self {
Self {
addr: info.addr.into(),
redis: info.redis.into(),
}
}
}

impl redis::IntoConnectionInfo for ConnectionInfo {
fn into_connection_info(self) -> RedisResult<redis::ConnectionInfo> {
Ok(self.into())
}
}

/// This is a 1:1 copy of the [`redis::RedisConnectionInfo`] struct.
/// This is duplicated here in order to add support for the
/// [`serde::Deserialize`] trait which is required for the [`serde`] support.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(crate = "serde_1"))]
pub struct RedisConnectionInfo {
/// The database number to use. This is usually `0`.
pub db: i64,

/// Optionally a username that should be used for connection.
pub username: Option<String>,

/// Optionally a password that should be used for connection.
pub password: Option<String>,
}

impl From<RedisConnectionInfo> for redis::RedisConnectionInfo {
fn from(info: RedisConnectionInfo) -> Self {
Self {
db: info.db,
username: info.username,
password: info.password,
}
}
}

impl From<redis::RedisConnectionInfo> for RedisConnectionInfo {
fn from(info: redis::RedisConnectionInfo) -> Self {
Self {
db: info.db,
username: info.username,
password: info.password,
}
}
}

/// This error is returned if the configuration contains an error
#[derive(Debug)]
pub enum ConfigError {
/// Both url and connection were specified in the config
UrlAndConnectionSpecified,
/// The [`redis`] crate returned an error when parsing the config
Redis(RedisError),
}

impl From<RedisError> for ConfigError {
fn from(e: RedisError) -> Self {
Self::Redis(e)
}
}

impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UrlAndConnectionSpecified => write!(
f,
"url and connection must not be specified at the same time."
),
Self::Redis(e) => write!(f, "Redis: {}", e),
}
}
}

impl std::error::Error for ConfigError {}
23 changes: 1 addition & 22 deletions redis/src/cluster/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(
nonstandard_style,
rust_2018_idioms,
rustdoc::broken_intra_doc_links,
rustdoc::private_intra_doc_links
)]
#![forbid(non_ascii_idents, unsafe_code)]
#![warn(
deprecated_in_future,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
unreachable_pub,
unused_import_braces,
unused_labels,
unused_lifetimes,
unused_qualifications,
unused_results
)]
#![allow(clippy::uninlined_format_args)]

//! This module extends the library to support Redis Cluster.
mod config;

use std::{
Expand Down
2 changes: 1 addition & 1 deletion redis/tests/redis_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn create_pool() -> deadpool_redis::cluster::Pool {

#[tokio::test]
async fn test_pipeline() {
use deadpool_redis::cluster::redis::pipe;
use deadpool_redis::redis::pipe;
let pool = create_pool();
let mut conn = pool.get().await.unwrap();
let (value,): (String,) = pipe()
Expand Down

0 comments on commit a80415d

Please sign in to comment.