mirror of
https://github.com/alibaba/higress.git
synced 2026-05-09 05:17:27 +08:00
feat: Rust WASM supports Redis database configuration option (#2704)
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
|
||||
use crate::{cluster_wrapper::Cluster, internal};
|
||||
use proxy_wasm::{hostcalls::RedisCallbackFn, types::Status};
|
||||
use redis::{Cmd, ToRedisArgs, Value};
|
||||
|
||||
use crate::{cluster_wrapper::Cluster, internal};
|
||||
|
||||
pub type RedisValueCallbackFn = dyn FnOnce(&Result<Value, String>, usize, u32);
|
||||
|
||||
fn gen_callback(call_fn: Box<RedisValueCallbackFn>) -> Box<RedisCallbackFn> {
|
||||
@@ -25,6 +24,7 @@ pub struct RedisClientBuilder {
|
||||
username: Option<String>,
|
||||
password: Option<String>,
|
||||
timeout: Duration,
|
||||
database: Option<u32>,
|
||||
}
|
||||
|
||||
impl RedisClientBuilder {
|
||||
@@ -34,6 +34,7 @@ impl RedisClientBuilder {
|
||||
username: None,
|
||||
password: None,
|
||||
timeout,
|
||||
database: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,9 +48,24 @@ impl RedisClientBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn database(mut self, database: Option<u32>) -> Self {
|
||||
self.database = database;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> RedisClient {
|
||||
let upstream = if let Some(db) = self.database {
|
||||
if db != 0 {
|
||||
format!("{}?db={}", self.upstream, db)
|
||||
} else {
|
||||
self.upstream
|
||||
}
|
||||
} else {
|
||||
self.upstream
|
||||
};
|
||||
|
||||
RedisClient {
|
||||
upstream: self.upstream,
|
||||
upstream,
|
||||
username: self.username,
|
||||
password: self.password,
|
||||
timeout: self.timeout,
|
||||
@@ -62,6 +78,7 @@ pub struct RedisClientConfig {
|
||||
username: Option<String>,
|
||||
password: Option<String>,
|
||||
timeout: Duration,
|
||||
database: Option<u32>,
|
||||
}
|
||||
|
||||
impl RedisClientConfig {
|
||||
@@ -71,6 +88,7 @@ impl RedisClientConfig {
|
||||
username: None,
|
||||
password: None,
|
||||
timeout,
|
||||
database: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +101,11 @@ impl RedisClientConfig {
|
||||
self.password = password.map(|p| p.as_ref().to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn database(&mut self, database: Option<u32>) -> &Self {
|
||||
self.database = database;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -95,8 +118,18 @@ pub struct RedisClient {
|
||||
|
||||
impl RedisClient {
|
||||
pub fn new(config: &RedisClientConfig) -> Self {
|
||||
let upstream = if let Some(db) = config.database {
|
||||
if db != 0 {
|
||||
format!("{}?db={}", config.upstream, db)
|
||||
} else {
|
||||
config.upstream.clone()
|
||||
}
|
||||
} else {
|
||||
config.upstream.clone()
|
||||
};
|
||||
|
||||
RedisClient {
|
||||
upstream: config.upstream.clone(),
|
||||
upstream,
|
||||
username: config.username.clone(),
|
||||
password: config.password.clone(),
|
||||
timeout: config.timeout,
|
||||
|
||||
Reference in New Issue
Block a user