From bdcc5ba46fbfa4c3f58b01702a197248e992b12c Mon Sep 17 00:00:00 2001 From: "He, Wanchen" Date: Wed, 6 Sep 2023 11:44:55 +0800 Subject: [PATCH 1/3] Support getting connection info from RedisClient. --- .../redis/inc/drogon/nosql/RedisClient.h | 34 +++++++++++++++++-- nosql_lib/redis/src/RedisClientImpl.cc | 30 ++++++---------- nosql_lib/redis/src/RedisClientImpl.h | 10 +----- nosql_lib/redis/src/RedisClientLockFree.cc | 24 ++++--------- nosql_lib/redis/src/RedisClientLockFree.h | 11 ++---- nosql_lib/redis/src/RedisClientManager.cc | 20 +++++------ nosql_lib/redis/src/RedisConnection.h | 10 ++++++ 7 files changed, 69 insertions(+), 70 deletions(-) diff --git a/nosql_lib/redis/inc/drogon/nosql/RedisClient.h b/nosql_lib/redis/inc/drogon/nosql/RedisClient.h index 498666c519..7140cad469 100644 --- a/nosql_lib/redis/inc/drogon/nosql/RedisClient.h +++ b/nosql_lib/redis/inc/drogon/nosql/RedisClient.h @@ -82,6 +82,14 @@ struct [[nodiscard]] RedisTransactionAwaiter class RedisTransaction; +struct RedisConnectionInfo +{ + trantor::InetAddress addr; + std::string username; + std::string password; + unsigned int db = 0; +}; + /** * @brief This class represents a redis client that contains several connections * to a redis server. @@ -100,11 +108,23 @@ class DROGON_EXPORT RedisClient * @return std::shared_ptr */ static std::shared_ptr newRedisClient( - const trantor::InetAddress &serverAddress, + trantor::InetAddress serverAddress, size_t numberOfConnections = 1, - const std::string &password = "", + std::string password = "", unsigned int db = 0, - const std::string &username = ""); + std::string username = "") + { + return newRedisClient({.addr = std::move(serverAddress), + .username = std::move(username), + .password = std::move(password), + .db = db}, + numberOfConnections); + } + + static std::shared_ptr newRedisClient( + RedisConnectionInfo connInfo, + size_t numberOfConnections = 1); + /** * @brief Execute a redis command * @@ -310,6 +330,14 @@ class DROGON_EXPORT RedisClient return internal::RedisTransactionAwaiter(this); } #endif + + const RedisConnectionInfo &connectionInfo() const + { + return connInfo_; + } + + protected: + RedisConnectionInfo connInfo_; }; class DROGON_EXPORT RedisTransaction : public RedisClient diff --git a/nosql_lib/redis/src/RedisClientImpl.cc b/nosql_lib/redis/src/RedisClientImpl.cc index 1015c7cec8..531b06d38c 100644 --- a/nosql_lib/redis/src/RedisClientImpl.cc +++ b/nosql_lib/redis/src/RedisClientImpl.cc @@ -17,37 +17,29 @@ #include "RedisSubscriberImpl.h" #include "RedisTransactionImpl.h" #include "../../lib/src/TaskTimeoutFlag.h" +#include "drogon/nosql/RedisClient.h" using namespace drogon::nosql; std::shared_ptr RedisClient::newRedisClient( - const trantor::InetAddress &serverAddress, - size_t connectionNumber, - const std::string &password, - unsigned int db, - const std::string &username) + RedisConnectionInfo connInfo, + size_t numberOfConnections) { - auto client = std::make_shared( - serverAddress, connectionNumber, username, password, db); + auto client = std::make_shared(std::move(connInfo), + numberOfConnections); client->init(); return client; } -RedisClientImpl::RedisClientImpl(const trantor::InetAddress &serverAddress, - size_t numberOfConnections, - std::string username, - std::string password, - unsigned int db) +RedisClientImpl::RedisClientImpl(RedisConnectionInfo connInfo, + size_t numberOfConnections) : loops_(numberOfConnections < std::thread::hardware_concurrency() ? numberOfConnections : std::thread::hardware_concurrency(), "RedisLoop"), - serverAddr_(serverAddress), - username_(std::move(username)), - password_(std::move(password)), - db_(db), numberOfConnections_(numberOfConnections) { + connInfo_ = std::move(connInfo); } void RedisClientImpl::init() @@ -65,8 +57,7 @@ void RedisClientImpl::init() RedisConnectionPtr RedisClientImpl::newConnection(trantor::EventLoop *loop) { - auto conn = std::make_shared( - serverAddr_, username_, password_, db_, loop); + auto conn = std::make_shared(connInfo_, loop); std::weak_ptr thisWeakPtr = shared_from_this(); conn->setConnectCallback([thisWeakPtr](RedisConnectionPtr &&conn) { auto thisPtr = thisWeakPtr.lock(); @@ -118,8 +109,7 @@ RedisConnectionPtr RedisClientImpl::newSubscribeConnection( trantor::EventLoop *loop, const std::shared_ptr &subscriber) { - auto conn = std::make_shared( - serverAddr_, username_, password_, db_, loop); + auto conn = std::make_shared(connInfo_, loop); std::weak_ptr weakThis = shared_from_this(); std::weak_ptr weakSub(subscriber); conn->setConnectCallback([weakThis, weakSub](RedisConnectionPtr &&conn) { diff --git a/nosql_lib/redis/src/RedisClientImpl.h b/nosql_lib/redis/src/RedisClientImpl.h index 6427f92d02..98e3e7460e 100644 --- a/nosql_lib/redis/src/RedisClientImpl.h +++ b/nosql_lib/redis/src/RedisClientImpl.h @@ -37,11 +37,7 @@ class RedisClientImpl final public std::enable_shared_from_this { public: - RedisClientImpl(const trantor::InetAddress &serverAddress, - size_t numberOfConnections, - std::string username = "", - std::string password = "", - unsigned int db = 0); + RedisClientImpl(RedisConnectionInfo connInfo, size_t numberOfConnections); void execCommandAsync(RedisResultCallback &&resultCallback, RedisExceptionCallback &&exceptionCallback, std::string_view command, @@ -84,10 +80,6 @@ class RedisClientImpl final std::unordered_set connections_; std::vector readyConnections_; size_t connectionPos_{0}; - const trantor::InetAddress serverAddr_; - const std::string username_; - const std::string password_; - const unsigned int db_; const size_t numberOfConnections_; double timeout_{-1.0}; std::list>> diff --git a/nosql_lib/redis/src/RedisClientLockFree.cc b/nosql_lib/redis/src/RedisClientLockFree.cc index e268fee10d..440cad2ea8 100644 --- a/nosql_lib/redis/src/RedisClientLockFree.cc +++ b/nosql_lib/redis/src/RedisClientLockFree.cc @@ -19,21 +19,13 @@ #include "../../lib/src/TaskTimeoutFlag.h" using namespace drogon::nosql; -RedisClientLockFree::RedisClientLockFree( - const trantor::InetAddress &serverAddress, - size_t numberOfConnections, - trantor::EventLoop *loop, - std::string username, - std::string password, - unsigned int db) - : loop_(loop), - serverAddr_(serverAddress), - username_(std::move(username)), - password_(std::move(password)), - db_(db), - numberOfConnections_(numberOfConnections) +RedisClientLockFree::RedisClientLockFree(RedisConnectionInfo connInfo, + size_t numberOfConnections, + trantor::EventLoop *loop) + : loop_(loop), numberOfConnections_(numberOfConnections) { assert(loop_); + connInfo_ = std::move(connInfo); for (size_t i = 0; i < numberOfConnections_; ++i) { loop_->queueInLoop([this]() { connections_.insert(newConnection()); }); @@ -43,8 +35,7 @@ RedisClientLockFree::RedisClientLockFree( RedisConnectionPtr RedisClientLockFree::newConnection() { loop_->assertInLoopThread(); - auto conn = std::make_shared( - serverAddr_, username_, password_, db_, loop_); + auto conn = std::make_shared(connInfo_, loop_); std::weak_ptr thisWeakPtr = shared_from_this(); conn->setConnectCallback([thisWeakPtr](RedisConnectionPtr &&conn) { auto thisPtr = thisWeakPtr.lock(); @@ -89,8 +80,7 @@ RedisConnectionPtr RedisClientLockFree::newSubscribeConnection( const std::shared_ptr &subscriber) { loop_->assertInLoopThread(); - auto conn = std::make_shared( - serverAddr_, username_, password_, db_, loop_); + auto conn = std::make_shared(connInfo_, loop_); std::weak_ptr weakThis = shared_from_this(); std::weak_ptr weakSub(subscriber); conn->setConnectCallback([weakThis, weakSub](RedisConnectionPtr &&conn) { diff --git a/nosql_lib/redis/src/RedisClientLockFree.h b/nosql_lib/redis/src/RedisClientLockFree.h index 8df7ac5877..9a145a432c 100644 --- a/nosql_lib/redis/src/RedisClientLockFree.h +++ b/nosql_lib/redis/src/RedisClientLockFree.h @@ -36,12 +36,9 @@ class RedisClientLockFree final public std::enable_shared_from_this { public: - RedisClientLockFree(const trantor::InetAddress &serverAddress, + RedisClientLockFree(RedisConnectionInfo conInfo, size_t numberOfConnections, - trantor::EventLoop *loop, - std::string username = "", - std::string password = "", - unsigned int db = 0); + trantor::EventLoop *loop); void execCommandAsync(RedisResultCallback &&resultCallback, RedisExceptionCallback &&exceptionCallback, std::string_view command, @@ -75,10 +72,6 @@ class RedisClientLockFree final std::unordered_set connections_; std::vector readyConnections_; size_t connectionPos_{0}; - const trantor::InetAddress serverAddr_; - const std::string username_; - const std::string password_; - const unsigned int db_; const size_t numberOfConnections_; std::list>> tasks_; diff --git a/nosql_lib/redis/src/RedisClientManager.cc b/nosql_lib/redis/src/RedisClientManager.cc index a0c77880db..f3bbfc2b45 100644 --- a/nosql_lib/redis/src/RedisClientManager.cc +++ b/nosql_lib/redis/src/RedisClientManager.cc @@ -28,6 +28,10 @@ void RedisClientManager::createRedisClients( assert(redisFastClientsMap_.empty()); for (auto &redisInfo : redisInfos_) { + RedisConnectionInfo connInfo{.addr = {redisInfo.addr_, redisInfo.port_}, + .username = redisInfo.username_, + .password = redisInfo.password_, + .db = redisInfo.db_}; if (redisInfo.isFast_) { redisFastClientsMap_[redisInfo.name_] = @@ -37,12 +41,7 @@ void RedisClientManager::createRedisClients( assert(idx == ioLoops[idx]->index()); LOG_TRACE << "create fast redis client for the thread " << idx; c = std::make_shared( - trantor::InetAddress(redisInfo.addr_, redisInfo.port_), - redisInfo.connectionNumber_, - ioLoops[idx], - redisInfo.username_, - redisInfo.password_, - redisInfo.db_); + connInfo, redisInfo.connectionNumber_, ioLoops[idx]); if (redisInfo.timeout_ > 0.0) { c->setTimeout(redisInfo.timeout_); @@ -51,12 +50,9 @@ void RedisClientManager::createRedisClients( } else { - auto clientPtr = std::make_shared( - trantor::InetAddress(redisInfo.addr_, redisInfo.port_), - redisInfo.connectionNumber_, - redisInfo.username_, - redisInfo.password_, - redisInfo.db_); + auto clientPtr = + std::make_shared(connInfo, + redisInfo.connectionNumber_); if (redisInfo.timeout_ > 0.0) { clientPtr->setTimeout(redisInfo.timeout_); diff --git a/nosql_lib/redis/src/RedisConnection.h b/nosql_lib/redis/src/RedisConnection.h index ff821807ae..9e93d13b2d 100644 --- a/nosql_lib/redis/src/RedisConnection.h +++ b/nosql_lib/redis/src/RedisConnection.h @@ -50,6 +50,16 @@ class RedisConnection : public trantor::NonCopyable, unsigned int db, trantor::EventLoop *loop); + RedisConnection(const RedisConnectionInfo &connInfo, + trantor::EventLoop *loop) + : RedisConnection(connInfo.addr, + connInfo.username, + connInfo.password, + connInfo.db, + loop) + { + } + void setConnectCallback( const std::function &&)> &callback) From 9fea4ac2318951f68571c7380f20af096ac12d3e Mon Sep 17 00:00:00 2001 From: "He, Wanchen" Date: Wed, 6 Sep 2023 12:09:46 +0800 Subject: [PATCH 2/3] Update RedisClientSkipped. --- lib/src/RedisClientSkipped.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/src/RedisClientSkipped.cc b/lib/src/RedisClientSkipped.cc index 96014ac476..c2ee200cbe 100644 --- a/lib/src/RedisClientSkipped.cc +++ b/lib/src/RedisClientSkipped.cc @@ -18,16 +18,14 @@ namespace drogon { namespace nosql { -std::shared_ptr RedisClient::newRedisClient( - const trantor::InetAddress& /*serverAddress*/, - size_t /*numberOfConnections*/, - const std::string& /*password*/, - const unsigned int /*db*/, - const std::string& /*username*/) + +std::shared_ptr newRedisClient(RedisConnectionInfo, + size_t /*numberOfConnections*/) { LOG_FATAL << "Redis is not supported by drogon, please install the " "hiredis library first."; abort(); } + } // namespace nosql } // namespace drogon From 11f489b4b9428b48aa04dd02d0dbc84167627492 Mon Sep 17 00:00:00 2001 From: "He, Wanchen" Date: Wed, 6 Sep 2023 12:36:19 +0800 Subject: [PATCH 3/3] Fix. --- lib/src/RedisClientSkipped.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/src/RedisClientSkipped.cc b/lib/src/RedisClientSkipped.cc index c2ee200cbe..1b0fba11e8 100644 --- a/lib/src/RedisClientSkipped.cc +++ b/lib/src/RedisClientSkipped.cc @@ -19,8 +19,9 @@ namespace drogon namespace nosql { -std::shared_ptr newRedisClient(RedisConnectionInfo, - size_t /*numberOfConnections*/) +std::shared_ptr RedisClient::newRedisClient( + RedisConnectionInfo, + size_t /*numberOfConnections*/) { LOG_FATAL << "Redis is not supported by drogon, please install the " "hiredis library first.";