-
Notifications
You must be signed in to change notification settings - Fork 0
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: add client max connection #136
Open
Ph0m1
wants to merge
18
commits into
unstable
Choose a base branch
from
feat/add_clientMaxConnection
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
eaa8fe8
feat: addClientMaxConnectionLimits(#104)
Ph0m1 e01a031
feat: addClientsMaxConnectionLimits
Ph0m1 a3d3ba3
remove unused header
Ph0m1 ab5c2c2
feat: rewrite base on netoption
Ph0m1 3e07a20
add response to client
Ph0m1 9edb5b4
Merge branch 'unstable' of github.com:arana-db/kiwi into feat/add_cli…
Ph0m1 80da4ab
update format
Ph0m1 e555f07
after clang format
Ph0m1 1743986
after clang format
Ph0m1 e79f013
after clang format
Ph0m1 81a60af
Merge branch 'unstable' of github.com:arana-db/kiwi into feat/add_cli…
Ph0m1 92fec59
merge with unstable
Ph0m1 30a2327
rewrite codes with workflows
Ph0m1 c7b7c79
after clang-format
Ph0m1 239f72f
after clang-format
Ph0m1 50a3929
after clang-format
Ph0m1 4e9c500
remove increment client count function
Ph0m1 3c5b37f
fix format od INFO
Ph0m1 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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#pragma once | ||
|
||
#include <atomic> | ||
#include <cstdint> | ||
#include <memory> | ||
#include <mutex> | ||
#include <shared_mutex> | ||
|
@@ -18,6 +19,8 @@ | |
#include "io_thread.h" | ||
#include "net_options.h" | ||
|
||
#include "log.h" | ||
|
||
#if defined(HAVE_EPOLL) | ||
|
||
# include "epoll_event.h" | ||
|
@@ -90,18 +93,24 @@ class ThreadManager { | |
|
||
uint64_t DoTCPConnect(T &t, int fd, const std::shared_ptr<Connection> &conn); | ||
|
||
uint32_t get_client_count() const { return clientCount_.load(); } | ||
|
||
void client_count_decrement() { clientCount_.fetch_sub(1, std::memory_order_relaxed); } | ||
|
||
private: | ||
const int8_t index_ = 0; // The index of the thread | ||
uint32_t tcpKeepAlive_ = 300; // The timeout of the keepalive connection in seconds | ||
std::atomic<bool> running_ = true; // Whether the thread is running | ||
|
||
NetOptions netOptions_; | ||
|
||
inline static std::atomic<uint32_t> clientCount_{0}; | ||
|
||
std::unique_ptr<IOThread> readThread_; // Read thread | ||
std::unique_ptr<IOThread> writeThread_; // Write thread | ||
|
||
// All connections for the current thread | ||
std::unordered_map<uint64_t, std::pair<T, std::shared_ptr<Connection>>> connections_; | ||
std::unordered_map<uint64_t, std::pair<T, std::shared_ptr<Connection>>> | ||
connections_; // All connections for the current thread | ||
|
||
std::shared_mutex mutex_; | ||
|
||
|
@@ -149,6 +158,22 @@ void ThreadManager<T>::Stop() { | |
template <typename T> | ||
requires HasSetFdFunction<T> | ||
void ThreadManager<T>::OnNetEventCreate(int fd, const std::shared_ptr<Connection> &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 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) { | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 返回 redis 格式的一个 response |
||
} | ||
|
||
T t; | ||
onInit_(&t); | ||
auto connId = getConnId(); | ||
|
@@ -206,6 +231,7 @@ void ThreadManager<T>::OnNetEventClose(uint64_t connId, std::string &&err) { | |
iter->second.second->netEvent_->Close(); // close socket | ||
onClose_(iter->second.first, std::move(err)); | ||
connections_.erase(iter); | ||
client_count_decrement(); | ||
} | ||
|
||
template <typename T> | ||
|
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.
💡 Codebase verification
Fix inconsistent memory ordering in client count operations
The client count operations use inconsistent memory ordering:
compare_exchange_strong
usesmemory_order_seq_cst
fetch_sub
usesmemory_order_relaxed
This asymmetry could lead to temporary inconsistencies in client counting. Consider using consistent ordering:
🔗 Analysis chain
Consider memory ordering implications of static initialization.
The static atomic counter is correctly used for thread-safe counting, but consider potential initialization order issues across translation units. Also, the relaxed memory ordering in
client_count_decrement
might be too weak for accurate client counting.Run this script to check for other static atomics that might interact with this counter:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 130
Script:
Length of output: 2006