Skip to content

Commit

Permalink
Update r2d2_redis and use exported r2d2 from it
Browse files Browse the repository at this point in the history
  • Loading branch information
spk committed Sep 7, 2019
1 parent 4fdd4e7 commit b09b317
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ matrix:
include:
- rust: stable
env: FMT=1
install:
before_script:
- rustup component add rustfmt
script:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ rand = "0.7"
serde = "1.0"
serde_json = "1.0"
r2d2 = "0.8"
r2d2_redis = "0.9"
r2d2_redis = "0.11"
22 changes: 10 additions & 12 deletions src/sidekiq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use std::error::Error;
use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};

use r2d2::Error as PoolError;
use r2d2::{Pool, PooledConnection};
use r2d2_redis::{redis, RedisConnectionManager};
use r2d2_redis::{r2d2, redis, RedisConnectionManager};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use serde::ser::SerializeStruct;
Expand All @@ -16,8 +14,8 @@ use Value;

const REDIS_URL_ENV: &str = "REDIS_URL";
const REDIS_URL_DEFAULT: &str = "redis://127.0.0.1/";
pub type RedisPooledConnection = PooledConnection<RedisConnectionManager>;
pub type RedisPool = Pool<RedisConnectionManager>;
pub type RedisPooledConnection = r2d2::PooledConnection<RedisConnectionManager>;
pub type RedisPool = r2d2::Pool<RedisConnectionManager>;

#[derive(Debug)]
pub struct ClientError {
Expand All @@ -27,15 +25,15 @@ pub struct ClientError {
#[derive(Debug)]
enum ErrorKind {
Redis(redis::RedisError),
PoolInit(PoolError),
PoolInit(r2d2::Error),
}

pub fn create_redis_pool() -> Result<RedisPool, ClientError> {
let redis_url =
&env::var(&REDIS_URL_ENV.to_owned()).unwrap_or_else(|_| REDIS_URL_DEFAULT.to_owned());
let url = redis::parse_redis_url(redis_url).unwrap();
let manager = RedisConnectionManager::new(url).unwrap();
Pool::new(manager).map_err(|err| ClientError {
r2d2::Pool::new(manager).map_err(|err| ClientError {
kind: ErrorKind::PoolInit(err),
})
}
Expand Down Expand Up @@ -67,7 +65,7 @@ impl Error for ClientError {
}
}

fn cause(&self) -> Option<&Error> {
fn cause(&self) -> Option<&dyn Error> {
match self.kind {
ErrorKind::Redis(ref err) => Some(err),
ErrorKind::PoolInit(ref err) => Some(err),
Expand All @@ -83,8 +81,8 @@ impl From<redis::RedisError> for ClientError {
}
}

impl From<PoolError> for ClientError {
fn from(error: PoolError) -> ClientError {
impl From<r2d2::Error> for ClientError {
fn from(error: r2d2::Error) -> ClientError {
ClientError {
kind: ErrorKind::PoolInit(error),
}
Expand Down Expand Up @@ -235,7 +233,7 @@ impl Client {
.map(|entry| serde_json::to_string(&entry).unwrap())
.collect::<Vec<_>>();
match self.connect() {
Ok(conn) => redis::pipe()
Ok(mut conn) => redis::pipe()
.atomic()
.cmd("SADD")
.arg("queues")
Expand All @@ -244,7 +242,7 @@ impl Client {
.cmd("LPUSH")
.arg(self.queue_name(&payload.queue))
.arg(to_push)
.query(&*conn)
.query(&mut *conn)
.map_err(|err| ClientError {
kind: ErrorKind::Redis(err),
}),
Expand Down

0 comments on commit b09b317

Please sign in to comment.