-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
980 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::{ | ||
error, | ||
pool::{Connection, Pool}, | ||
types, | ||
}; | ||
use async_trait::async_trait; | ||
use bb8_redis_cluster::RedisConnectionManager; | ||
use redis::aio::ConnectionLike; | ||
|
||
pub struct BB8Cluster { | ||
pool: bb8::Pool<RedisConnectionManager>, | ||
} | ||
|
||
impl BB8Cluster { | ||
pub async fn new(initial_nodes: Vec<String>, max_size: u32) -> Self { | ||
let manager = RedisConnectionManager::new(initial_nodes).unwrap(); | ||
let pool = bb8::Pool::builder() | ||
.max_size(max_size) | ||
.build(manager) | ||
.await | ||
.unwrap(); | ||
Self { pool } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Pool for BB8Cluster { | ||
async fn get_connection(&self) -> Result<Connection, error::RedisError> { | ||
let c = self.pool.get().await?; | ||
Ok(Connection { | ||
c: Box::new(c.to_owned()), | ||
}) | ||
} | ||
|
||
async fn execute( | ||
&self, | ||
cmd: &str, | ||
args: Vec<types::Arg>, | ||
) -> Result<redis::Value, error::RedisError> { | ||
let mut conn = self.pool.get().await?; | ||
let value = conn.req_packed_command(redis::cmd(cmd).arg(&args)).await?; | ||
Ok(value) | ||
} | ||
|
||
fn status(&self) -> HashMap<&str, redis::Value> { | ||
let mut result = HashMap::new(); | ||
result.insert("closed", redis::Value::Int(0)); | ||
result.insert("impl", redis::Value::Data("bb8_cluster".into())); | ||
result.insert("cluster", redis::Value::Int(1)); | ||
let state = self.pool.state(); | ||
result.insert("connections", redis::Value::Int(state.connections.into())); | ||
result.insert( | ||
"idle_connections", | ||
redis::Value::Int(state.idle_connections.into()), | ||
); | ||
result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::{ | ||
error, | ||
pool::{Connection, Pool}, | ||
types, | ||
}; | ||
use async_trait::async_trait; | ||
use bb8_redis::RedisMultiplexedConnectionManager; | ||
use redis::aio::ConnectionLike; | ||
|
||
pub struct BB8Pool { | ||
pool: bb8::Pool<RedisMultiplexedConnectionManager>, | ||
} | ||
|
||
impl BB8Pool { | ||
pub async fn new(initial_nodes: Vec<String>, max_size: u32) -> Self { | ||
let addr = initial_nodes.get(0).unwrap().to_string(); | ||
let manager = RedisMultiplexedConnectionManager::new(addr).unwrap(); | ||
let pool = bb8::Pool::builder() | ||
.max_size(max_size) | ||
.build(manager) | ||
.await | ||
.unwrap(); | ||
Self { pool } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Pool for BB8Pool { | ||
async fn get_connection(&self) -> Result<Connection, error::RedisError> { | ||
let c = self.pool.get().await?; | ||
Ok(Connection { | ||
c: Box::new(c.to_owned()), | ||
}) | ||
} | ||
|
||
async fn execute( | ||
&self, | ||
cmd: &str, | ||
args: Vec<types::Arg>, | ||
) -> Result<redis::Value, error::RedisError> { | ||
let mut conn = self.pool.get().await?; | ||
let value = conn.req_packed_command(redis::cmd(cmd).arg(&args)).await?; | ||
Ok(value) | ||
} | ||
|
||
fn status(&self) -> HashMap<&str, redis::Value> { | ||
let mut result = HashMap::new(); | ||
result.insert("impl", redis::Value::Data("bb8_redis".into())); | ||
result.insert("closed", redis::Value::Int(0)); | ||
result.insert("cluster", redis::Value::Int(0)); | ||
let state = self.pool.state(); | ||
result.insert("connections", redis::Value::Int(state.connections.into())); | ||
result.insert( | ||
"idle_connections", | ||
redis::Value::Int(state.idle_connections.into()), | ||
); | ||
result | ||
} | ||
} |
Oops, something went wrong.