From eaa8fe8e9816d1339a05b3747e40ecd73e033b8e Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Tue, 10 Dec 2024 23:22:40 +0800 Subject: [PATCH 01/15] feat: addClientMaxConnectionLimits(#104) --- src/kiwi.cc | 2 ++ src/net/event_server.h | 5 +++++ src/net/thread_manager.h | 21 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/kiwi.cc b/src/kiwi.cc index 1ac5bb46..61b10a8d 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -223,6 +223,8 @@ bool KiwiDB::Init() { event_server_->InitTimer(10); + event_server_->SetMaxConnCount(g_config.max_clients.load()); + auto timerTask = std::make_shared(1000); timerTask->SetCallback([]() { PREPL.Cron(); }); event_server_->AddTimerTask(timerTask); diff --git a/src/net/event_server.h b/src/net/event_server.h index 4c353520..5b71eb3f 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -49,6 +49,8 @@ class EventServer final { inline void DelTimerTask(int64_t timerId) { timer_->DelTask(timerId); } + inline void SetMaxConnCount(int64_t count) { maxConnCount_ = count; } + std::pair StartServer(int64_t interval = 0); std::pair StartClientServer(); @@ -101,6 +103,8 @@ class EventServer final { std::condition_variable cv_; std::shared_ptr timer_; + + int64_t maxConnCount_; }; template @@ -132,6 +136,7 @@ requires HasSetFdFunction std::pair EventServer::StartS tm->SetOnConnect(onConnect_); tm->SetOnMessage(onMessage_); tm->SetOnClose(onClose_); + tm->SetMaxConnCount(maxConnCount_); threadsManager_.emplace_back(std::move(tm)); } diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 07271172..d4b2040f 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -16,6 +16,7 @@ #include "callback_function.h" #include "config.h" #include "io_thread.h" +#include "log.h" #if defined(HAVE_EPOLL) @@ -80,6 +81,12 @@ class ThreadManager { // Send message to the client void SendPacket(const T &conn, std::string &&msg); + // Set the maximum number of connections + void SetMaxConnCount(uint64_t maxConnCount) { maxConnCount_ = maxConnCount; } + + uint64_t getConnCount(){return this->connCount_.load();}; // Get the number of connections + void addConnCount(){this->connCount_.fetch_add(1);}; // Add the number of connections + void subConnCount(){this->connCount_.fetch_sub(1);}; // Subtract the number of connections private: // Create read thread bool CreateReadThread(const std::shared_ptr &listen, const std::shared_ptr &timer); @@ -89,11 +96,15 @@ class ThreadManager { uint64_t DoTCPConnect(T &t, int fd, const std::shared_ptr &conn); + private: const bool rwSeparation_ = true; // Whether to separate read and write threads const int8_t index_ = 0; // The index of the thread std::atomic running_ = true; // Whether the thread is running + inline static std::atomic connCount_{0}; + std::atomic maxConnCount_; // The maximum number of connections + std::unique_ptr readThread_; // Read thread std::unique_ptr writeThread_; // Write thread @@ -113,6 +124,8 @@ class ThreadManager { OnClose onClose_; }; + + template requires HasSetFdFunction ThreadManager::~ThreadManager() { Stop(); } @@ -153,7 +166,11 @@ void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr= maxConnCount_) { + // 返回给客户端 "too many connections" + INFO("too many connections"); + return; + } { std::lock_guard lock(mutex_); connections_.emplace(connId, std::make_pair(t, conn)); @@ -163,6 +180,7 @@ void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptrAddNewEvent(connId, fd, BaseEvent::EVENT_NULL); // add null event to write_thread epoll } + addConnCount(); onCreate_(connId, t, conn->addr_); } @@ -200,6 +218,7 @@ void ThreadManager::OnNetEventClose(uint64_t connId, std::string &&err) { iter->second.second->netEvent_->Close(); // close socket onClose_(iter->second.first, std::move(err)); connections_.erase(iter); + subConnCount(); // decrease connection count } template From e01a0310ef27e237c83f9b270ee65440e5402b07 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Wed, 11 Dec 2024 18:28:41 +0800 Subject: [PATCH 02/15] feat: addClientsMaxConnectionLimits --- src/net/thread_manager.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index d4b2040f..58311e34 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -14,8 +14,10 @@ #include #include "callback_function.h" +#include "client.h" #include "config.h" #include "io_thread.h" +#include "kiwi.h" #include "log.h" #if defined(HAVE_EPOLL) @@ -102,7 +104,7 @@ class ThreadManager { const int8_t index_ = 0; // The index of the thread std::atomic running_ = true; // Whether the thread is running - inline static std::atomic connCount_{0}; + inline static std::atomic connCount_{0}; // The number of connections std::atomic maxConnCount_; // The maximum number of connections std::unique_ptr readThread_; // Read thread @@ -167,8 +169,9 @@ void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr= maxConnCount_) { - // 返回给客户端 "too many connections" - INFO("too many connections"); + + INFO("too many connections"); + onClose_(t, "too many connections"); return; } { From a3d3ba32292a7a23ccd18cd59dce7effd215a92a Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Sat, 21 Dec 2024 20:08:37 +0800 Subject: [PATCH 03/15] remove unused header --- src/net/thread_manager.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 58311e34..f448a3a5 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -14,11 +14,8 @@ #include #include "callback_function.h" -#include "client.h" #include "config.h" #include "io_thread.h" -#include "kiwi.h" -#include "log.h" #if defined(HAVE_EPOLL) @@ -86,7 +83,7 @@ class ThreadManager { // Set the maximum number of connections void SetMaxConnCount(uint64_t maxConnCount) { maxConnCount_ = maxConnCount; } - uint64_t getConnCount(){return this->connCount_.load();}; // Get the number of connections + uint64_t getConnCount(){return this->connCount_.load(std::memory_order_acquire);}; // Get the number of connections void addConnCount(){this->connCount_.fetch_add(1);}; // Add the number of connections void subConnCount(){this->connCount_.fetch_sub(1);}; // Subtract the number of connections private: @@ -169,8 +166,6 @@ void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr= maxConnCount_) { - - INFO("too many connections"); onClose_(t, "too many connections"); return; } From 3e07a202f5321399f30f28b1ffc46a093c02e8f3 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Fri, 10 Jan 2025 19:29:48 +0800 Subject: [PATCH 04/15] add response to client --- src/net/thread_manager.h | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 32c76095..66028899 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -95,18 +95,20 @@ class ThreadManager { void clientCountIncrement() { clientCount_.fetch_add(1, std::memory_order_relaxed); } void clientCountDecrement() { clientCount_.fetch_sub(1, std::memory_order_relaxed); } + private: const int8_t index_ = 0; // The index of the thread std::atomic running_ = true; // Whether the thread is running NetOptions netOptions_; - inline static std::atomic clientCount_{0}; + inline static std::atomic clientCount_{0}; std::unique_ptr readThread_; // Read thread std::unique_ptr writeThread_; // Write thread - std::unordered_map>> connections_; // All connections for the current thread + std::unordered_map>> + connections_; // All connections for the current thread std::shared_mutex mutex_; @@ -121,10 +123,11 @@ class ThreadManager { OnClose onClose_; }; - - template -requires HasSetFdFunction ThreadManager::~ThreadManager() { Stop(); } +requires HasSetFdFunction +ThreadManager::~ThreadManager() { + Stop(); +} template requires HasSetFdFunction @@ -153,7 +156,10 @@ void ThreadManager::Stop() { template requires HasSetFdFunction void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr &conn) { - if(getClientCount() >= netOptions_.GetMaxClients()){ + if (getClientCount() >= netOptions_.GetMaxClients()) { + INFO("Max client connentions, refuse new connection fd:{%d}", fd); + std::string response = "-ERR max clients reached\r\n"; + ::send(fd, response.c_str(), response.size(), 0); ::close(fd); return; } @@ -221,7 +227,9 @@ void ThreadManager::OnNetEventClose(uint64_t connId, std::string &&err) { template requires HasSetFdFunction -void ThreadManager::CloseConnection(uint64_t connId) { OnNetEventClose(connId, ""); } +void ThreadManager::CloseConnection(uint64_t connId) { + OnNetEventClose(connId, ""); +} template requires HasSetFdFunction @@ -345,8 +353,8 @@ bool ThreadManager::CreateWriteThread() { } template -requires HasSetFdFunction uint64_t ThreadManager::DoTCPConnect(T &t, int fd, - const std::shared_ptr &conn) { +requires HasSetFdFunction +uint64_t ThreadManager::DoTCPConnect(T &t, int fd, const std::shared_ptr &conn) { auto connId = getConnId(); if constexpr (IsPointer_v) { t->SetConnId(connId); From 80da4ab6c3db2529a241b2c3b0cca58beaaf6243 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Fri, 10 Jan 2025 19:55:13 +0800 Subject: [PATCH 05/15] update format --- src/net/thread_manager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 34a4f268..c3482d6f 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -157,7 +157,7 @@ template requires HasSetFdFunction void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr &conn) { if (getClientCount() >= netOptions_.GetMaxClients()) { - INFO("Max client connentions, refuse new connection fd:{%d}", fd); + INFO("Max client connetions, refuse new connection fd: %d", fd); std::string response = "-ERR max clients reached\r\n"; ::send(fd, response.c_str(), response.size(), 0); ::close(fd); From e555f0771b9f787dac60da831b958daa1484279d Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Fri, 10 Jan 2025 20:10:58 +0800 Subject: [PATCH 06/15] after clang format --- src/net/event_server.h | 8 +++++--- src/net/thread_manager.h | 13 ++++--------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/net/event_server.h b/src/net/event_server.h index dd838c19..583b8d1a 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -95,14 +95,15 @@ class EventServer final { std::vector>> threadsManager_; std::mutex mtx_; + std::condition_variable cv_; std::shared_ptr timer_; - }; template -requires HasSetFdFunction std::pair EventServer::StartServer(int64_t interval) { +requires HasSetFdFunction +std::pair EventServer::StartServer(int64_t interval) { if (opt_.GetThreadNum() <= 0) { return std::pair(false, "thread num must be greater than 0"); } @@ -141,7 +142,8 @@ requires HasSetFdFunction std::pair EventServer::StartS } template -requires HasSetFdFunction std::pair EventServer::StartClientServer() { +requires HasSetFdFunction +std::pair EventServer::StartClientServer() { if (opt_.GetThreadNum() <= 0) { return std::pair(false, "thread num must be greater than 0"); } diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index c3482d6f..1e8ea716 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -124,10 +124,7 @@ class ThreadManager { }; template -requires HasSetFdFunction -ThreadManager::~ThreadManager() { - Stop(); -} +requires HasSetFdFunction ThreadManager::~ThreadManager() { Stop(); } template requires HasSetFdFunction @@ -227,9 +224,7 @@ void ThreadManager::OnNetEventClose(uint64_t connId, std::string &&err) { template requires HasSetFdFunction -void ThreadManager::CloseConnection(uint64_t connId) { - OnNetEventClose(connId, ""); -} +void ThreadManager::CloseConnection(uint64_t connId) { OnNetEventClose(connId, ""); } template requires HasSetFdFunction @@ -353,8 +348,8 @@ bool ThreadManager::CreateWriteThread() { } template -requires HasSetFdFunction -uint64_t ThreadManager::DoTCPConnect(T &t, int fd, const std::shared_ptr &conn) { +requires HasSetFdFunction uint64_t ThreadManager::DoTCPConnect(T &t, int fd, + const std::shared_ptr &conn) { auto connId = getConnId(); if constexpr (IsPointer_v) { t->SetConnId(connId); From 174398685eb47af8b1e855d8c49c78ab01ba85c9 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Fri, 10 Jan 2025 20:15:56 +0800 Subject: [PATCH 07/15] after clang format --- src/net/event_server.h | 6 ++---- src/net/thread_manager.h | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/net/event_server.h b/src/net/event_server.h index 583b8d1a..4b7d8fca 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -102,8 +102,7 @@ class EventServer final { }; template -requires HasSetFdFunction -std::pair EventServer::StartServer(int64_t interval) { +requires HasSetFdFunction std::pair EventServer::StartServer(int64_t interval) { if (opt_.GetThreadNum() <= 0) { return std::pair(false, "thread num must be greater than 0"); } @@ -142,8 +141,7 @@ std::pair EventServer::StartServer(int64_t interval) { } template -requires HasSetFdFunction -std::pair EventServer::StartClientServer() { +requires HasSetFdFunction std::pair EventServer::StartClientServer() { if (opt_.GetThreadNum() <= 0) { return std::pair(false, "thread num must be greater than 0"); } diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 1e8ea716..7f687c42 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -348,8 +348,7 @@ bool ThreadManager::CreateWriteThread() { } template -requires HasSetFdFunction uint64_t ThreadManager::DoTCPConnect(T &t, int fd, - const std::shared_ptr &conn) { +requires HasSetFdFunction uint64_t ThreadManager::DoTCPConnect(T &t, int fd, const std::shared_ptr &conn) { auto connId = getConnId(); if constexpr (IsPointer_v) { t->SetConnId(connId); From e79f013eceb55a0677a1e3e22be65dd92f79f573 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Fri, 10 Jan 2025 20:19:54 +0800 Subject: [PATCH 08/15] after clang format --- src/net/thread_manager.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 7f687c42..71ceba5c 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -348,7 +348,8 @@ bool ThreadManager::CreateWriteThread() { } template -requires HasSetFdFunction uint64_t ThreadManager::DoTCPConnect(T &t, int fd, const std::shared_ptr &conn) { +requires HasSetFdFunction uint64_t ThreadManager::DoTCPConnect(T &t, int fd, + const std::shared_ptr &conn) { auto connId = getConnId(); if constexpr (IsPointer_v) { t->SetConnId(connId); From 92fec595cc1ffa45c880095728f010c6b58bec21 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Sat, 18 Jan 2025 22:36:00 +0800 Subject: [PATCH 09/15] merge with unstable --- src/net/net_options.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/net/net_options.h b/src/net/net_options.h index 33b36501..c791706a 100644 --- a/src/net/net_options.h +++ b/src/net/net_options.h @@ -22,26 +22,20 @@ class NetOptions { bool GetRwSeparation() const { return rwSeparation_; } -<<<<<<< HEAD void SetMaxClients(uint32_t maxClients) { maxClients_ = maxClients; } uint32_t GetMaxClients() const { return maxClients_; } -======= void SetOpTcpKeepAlive(uint32_t tcpKeepAlive) { tcpKeepAlive_ = tcpKeepAlive; } uint32_t GetOpTcpKeepAlive() const { return tcpKeepAlive_; } ->>>>>>> 9937125d749015d74eef320413e99668a4a4a12b private: bool rwSeparation_ = true; // Whether to separate read and write int8_t threadNum_ = 1; // The number of threads -<<<<<<< HEAD uint32_t maxClients_ = 1; // The maximum number of connections(default 40000) -======= uint32_t tcpKeepAlive_ = 300; // The timeout of the keepalive connection in seconds ->>>>>>> 9937125d749015d74eef320413e99668a4a4a12b }; } // namespace net From 30a2327369dc0957330464d6b1ac6318011254e6 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Sat, 18 Jan 2025 22:36:32 +0800 Subject: [PATCH 10/15] rewrite codes with workflows --- src/net/thread_manager.h | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index ad4aaa26..932a11aa 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -90,11 +90,11 @@ class ThreadManager { uint64_t DoTCPConnect(T &t, int fd, const std::shared_ptr &conn); - uint32_t getClientCount() const { return clientCount_.load(); } + uint32_t get_client_count() const { return clientCount_.load(); } - void clientCountIncrement() { clientCount_.fetch_add(1, std::memory_order_relaxed); } + void client_count_increment() { clientCount_.fetch_add(1, std::memory_order_relaxed); } - void clientCountDecrement() { clientCount_.fetch_sub(1, std::memory_order_relaxed); } + void client_count_decrement() { clientCount_.fetch_sub(1, std::memory_order_relaxed); } private: const int8_t index_ = 0; // The index of the thread @@ -157,11 +157,21 @@ void ThreadManager::Stop() { template requires HasSetFdFunction void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr &conn) { - if (getClientCount() >= netOptions_.GetMaxClients()) { + if (!clientCount_.compare_exchange_strong( + expected, + expected + 1, + std::memory_order_seq_cst, + std::memory_order_seq_cst) || + expected >= netOptions_.GetMaxClients()) { INFO("Max client connetions, refuse new connection fd: %d", fd); std::string response = "-ERR max clients reached\r\n"; - ::send(fd, response.c_str(), response.size(), 0); - ::close(fd); + ssize_t sent = ::send(fd, response.c_str(), response.size(), 0); + if (sent < 0) { + ERROR("Failed to send error response to fd: %d, errno: %d", fd, errno); + } + if (::close(fd) < 0) { + ERROR("Failed to close fd: %d, errno: %d", fd, errno); + } return; } @@ -186,7 +196,7 @@ void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptraddr_); - clientCountIncrement(); + client_count_increment(); } template @@ -223,7 +233,7 @@ void ThreadManager::OnNetEventClose(uint64_t connId, std::string &&err) { iter->second.second->netEvent_->Close(); // close socket onClose_(iter->second.first, std::move(err)); connections_.erase(iter); - clientCountDecrement(); + client_count_decrement(); } template From c7b7c7914ab8868cd923912551dac9271b76d877 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Sat, 18 Jan 2025 22:41:17 +0800 Subject: [PATCH 11/15] after clang-format --- src/net/net_options.h | 2 +- src/net/thread_manager.h | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/net/net_options.h b/src/net/net_options.h index c791706a..421cc01a 100644 --- a/src/net/net_options.h +++ b/src/net/net_options.h @@ -34,7 +34,7 @@ class NetOptions { int8_t threadNum_ = 1; // The number of threads - uint32_t maxClients_ = 1; // The maximum number of connections(default 40000) + uint32_t maxClients_ = 1; // The maximum number of connections(default 40000) uint32_t tcpKeepAlive_ = 300; // The timeout of the keepalive connection in seconds }; diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 932a11aa..b6bb838e 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -157,20 +157,16 @@ void ThreadManager::Stop() { template requires HasSetFdFunction void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr &conn) { - if (!clientCount_.compare_exchange_strong( - expected, - expected + 1, - std::memory_order_seq_cst, - std::memory_order_seq_cst) || - expected >= netOptions_.GetMaxClients()) { + if (!clientCount_.compare_exchange_strong(expected, expected + 1, std::memory_order_seq_cst, + std::memory_order_seq_cst) || expected >= netOptions_.GetMaxClients()) { INFO("Max client connetions, refuse new connection fd: %d", fd); std::string response = "-ERR max clients reached\r\n"; ssize_t sent = ::send(fd, response.c_str(), response.size(), 0); if (sent < 0) { - ERROR("Failed to send error response to fd: %d, errno: %d", fd, errno); + ERROR("Failed to send error response to fd: %d, errno: %d", fd, errno); } if (::close(fd) < 0) { - ERROR("Failed to close fd: %d, errno: %d", fd, errno); + ERROR("Failed to close fd: %d, errno: %d", fd, errno); } return; } From 239f72f3e409a6714314b840ed80b860e153f4b9 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Sat, 18 Jan 2025 22:43:42 +0800 Subject: [PATCH 12/15] after clang-format --- src/net/thread_manager.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index b6bb838e..36cebe5a 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -158,7 +158,8 @@ template requires HasSetFdFunction void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr &conn) { if (!clientCount_.compare_exchange_strong(expected, expected + 1, std::memory_order_seq_cst, - std::memory_order_seq_cst) || expected >= netOptions_.GetMaxClients()) { + std::memory_order_seq_cst) || + expected >= netOptions_.GetMaxClients()) { INFO("Max client connetions, refuse new connection fd: %d", fd); std::string response = "-ERR max clients reached\r\n"; ssize_t sent = ::send(fd, response.c_str(), response.size(), 0); From 50a39293e0e143e1a05ebe5f4dcf73f4e3c7a8c4 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Sat, 18 Jan 2025 22:46:30 +0800 Subject: [PATCH 13/15] after clang-format --- src/net/thread_manager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 36cebe5a..7ab84426 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -158,7 +158,7 @@ template requires HasSetFdFunction void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr &conn) { if (!clientCount_.compare_exchange_strong(expected, expected + 1, std::memory_order_seq_cst, - std::memory_order_seq_cst) || + std::memory_order_seq_cst) || expected >= netOptions_.GetMaxClients()) { INFO("Max client connetions, refuse new connection fd: %d", fd); std::string response = "-ERR max clients reached\r\n"; From 4e9c5005c911333210b9bca9ae15a694f102d431 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Sat, 18 Jan 2025 23:15:12 +0800 Subject: [PATCH 14/15] remove increment client count function --- src/net/thread_manager.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 7ab84426..2ab9780a 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include #include @@ -18,6 +19,8 @@ #include "io_thread.h" #include "net_options.h" +#include "log.h" + #if defined(HAVE_EPOLL) # include "epoll_event.h" @@ -92,8 +95,6 @@ class ThreadManager { uint32_t get_client_count() const { return clientCount_.load(); } - void client_count_increment() { clientCount_.fetch_add(1, std::memory_order_relaxed); } - void client_count_decrement() { clientCount_.fetch_sub(1, std::memory_order_relaxed); } private: @@ -157,10 +158,11 @@ void ThreadManager::Stop() { template requires HasSetFdFunction void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr &conn) { + uint32_t expected = get_client_count(); if (!clientCount_.compare_exchange_strong(expected, expected + 1, std::memory_order_seq_cst, std::memory_order_seq_cst) || expected >= netOptions_.GetMaxClients()) { - INFO("Max client connetions, refuse new connection fd: %d", fd); + INFO("Max client connections, refuse new connection fd: %d", fd); std::string response = "-ERR max clients reached\r\n"; ssize_t sent = ::send(fd, response.c_str(), response.size(), 0); if (sent < 0) { @@ -193,7 +195,6 @@ void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptraddr_); - client_count_increment(); } template From 3c5b37fd45deb4612dc164c9579be2170683ba50 Mon Sep 17 00:00:00 2001 From: Ph0m1 <2732706745@qq.com> Date: Sun, 19 Jan 2025 21:04:28 +0800 Subject: [PATCH 15/15] fix format od INFO --- src/net/thread_manager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 2ab9780a..b5f6962b 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -162,7 +162,7 @@ void ThreadManager::OnNetEventCreate(int fd, const std::shared_ptr= netOptions_.GetMaxClients()) { - INFO("Max client connections, refuse new connection fd: %d", fd); + INFO("Max client connections, refuse new connection fd:{}", fd); std::string response = "-ERR max clients reached\r\n"; ssize_t sent = ::send(fd, response.c_str(), response.size(), 0); if (sent < 0) {