-
Notifications
You must be signed in to change notification settings - Fork 472
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(core/redis): Replace client requests with connection pool #5117
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8cafcb7
use pool
q3356564 3c41e56
fix
q3356564 1a55ebb
Merge branch 'main' of https://github.com/jackyyyyyssss/opendal into …
q3356564 9a90e17
fix
q3356564 c6c9921
fix
q3356564 0c9d518
fix
q3356564 d2004d2
fix
q3356564 2ea39e4
fix
q3356564 d460415
Merge branch 'main' into redis_pool
jackyyyyyssss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,168 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::Buffer; | ||
use crate::Error; | ||
use crate::ErrorKind; | ||
|
||
use redis::aio::ConnectionLike; | ||
use redis::aio::ConnectionManager; | ||
|
||
use redis::cluster::ClusterClient; | ||
use redis::cluster_async::ClusterConnection; | ||
use redis::from_redis_value; | ||
use redis::AsyncCommands; | ||
use redis::Client; | ||
use redis::RedisError; | ||
|
||
use std::time::Duration; | ||
|
||
#[derive(Clone)] | ||
pub enum RedisConnection { | ||
Normal(ConnectionManager), | ||
Cluster(ClusterConnection), | ||
} | ||
impl RedisConnection { | ||
pub async fn get(&mut self, key: &str) -> crate::Result<Option<Buffer>> { | ||
let result: Option<bytes::Bytes> = match self { | ||
RedisConnection::Normal(ref mut conn) => { | ||
conn.get(key).await.map_err(format_redis_error) | ||
} | ||
RedisConnection::Cluster(ref mut conn) => { | ||
conn.get(key).await.map_err(format_redis_error) | ||
} | ||
}?; | ||
Ok(result.map(Buffer::from)) | ||
} | ||
|
||
pub async fn set( | ||
&mut self, | ||
key: &str, | ||
value: Vec<u8>, | ||
ttl: Option<Duration>, | ||
) -> crate::Result<()> { | ||
let value = value.to_vec(); | ||
if let Some(ttl) = ttl { | ||
match self { | ||
RedisConnection::Normal(ref mut conn) => conn | ||
.set_ex(key, value, ttl.as_secs()) | ||
.await | ||
.map_err(format_redis_error)?, | ||
RedisConnection::Cluster(ref mut conn) => conn | ||
.set_ex(key, value, ttl.as_secs()) | ||
.await | ||
.map_err(format_redis_error)?, | ||
} | ||
} else { | ||
match self { | ||
RedisConnection::Normal(ref mut conn) => { | ||
conn.set(key, value).await.map_err(format_redis_error)? | ||
} | ||
RedisConnection::Cluster(ref mut conn) => { | ||
conn.set(key, value).await.map_err(format_redis_error)? | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn delete(&mut self, key: &str) -> crate::Result<()> { | ||
match self { | ||
RedisConnection::Normal(ref mut conn) => { | ||
let _: () = conn.del(key).await.map_err(format_redis_error)?; | ||
} | ||
RedisConnection::Cluster(ref mut conn) => { | ||
let _: () = conn.del(key).await.map_err(format_redis_error)?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn append(&mut self, key: &str, value: &[u8]) -> crate::Result<()> { | ||
match self { | ||
RedisConnection::Normal(ref mut conn) => { | ||
() = conn.append(key, value).await.map_err(format_redis_error)?; | ||
} | ||
RedisConnection::Cluster(ref mut conn) => { | ||
() = conn.append(key, value).await.map_err(format_redis_error)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct RedisConnectionManager { | ||
pub client: Option<Client>, | ||
pub cluster_client: Option<ClusterClient>, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl bb8::ManageConnection for RedisConnectionManager { | ||
type Connection = RedisConnection; | ||
type Error = Error; | ||
|
||
async fn connect(&self) -> Result<RedisConnection, Self::Error> { | ||
if let Some(client) = self.client.clone() { | ||
ConnectionManager::new(client.clone()) | ||
.await | ||
.map_err(format_redis_error) | ||
.map(RedisConnection::Normal) | ||
} else { | ||
self.cluster_client | ||
.clone() | ||
.unwrap() | ||
.get_async_connection() | ||
.await | ||
.map_err(format_redis_error) | ||
.map(RedisConnection::Cluster) | ||
} | ||
} | ||
|
||
async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> { | ||
let pong_value = match conn { | ||
RedisConnection::Normal(ref mut conn) => conn | ||
.send_packed_command(&redis::cmd("PING")) | ||
.await | ||
.map_err(format_redis_error)?, | ||
|
||
RedisConnection::Cluster(ref mut conn) => conn | ||
.req_packed_command(&redis::cmd("PING")) | ||
.await | ||
.map_err(format_redis_error)?, | ||
}; | ||
let pong: String = from_redis_value(&pong_value).map_err(format_redis_error)?; | ||
|
||
if pong == "PONG" { | ||
Ok(()) | ||
} else { | ||
Err(Error::new(ErrorKind::Unexpected, "PING ERROR")) | ||
} | ||
} | ||
|
||
fn has_broken(&self, _: &mut Self::Connection) -> bool { | ||
false | ||
} | ||
} | ||
|
||
pub fn format_redis_error(e: RedisError) -> Error { | ||
Error::new(ErrorKind::Unexpected, e.category()) | ||
.set_source(e) | ||
.set_temporary() | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks a lot for this PR first. Have you communicated with the upstream first? I think it's a great feature that upstream might want to merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your guidance. I have not yet contacted the upstream. I will try to submit the PR to the upstream to see if they will merge. I will modify and replace the connection pool with the upstream code in the another PR