-
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
fix:Synchronize config content#117 #174
base: unstable
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
#include <vector> | ||
|
||
#include "config.h" | ||
#include "kiwi.h" | ||
#include "std/std_string.h" | ||
#include "store.h" | ||
|
||
|
@@ -222,6 +223,16 @@ Status Config::Set(std::string key, const std::string& value, bool init_stage) { | |
if (iter == config_map_.end()) { | ||
return Status::NotFound("Non-existent configuration items."); | ||
} | ||
if (key == "log_level") g_kiwi->options_.SetLogLevel(value); | ||
if (key == "redis_compatible_mode") g_kiwi->options_.SetRedisCompatibleMode(stoi(value)); | ||
if (key == "worker_threads_num") { | ||
g_kiwi->options_.SetThreadNum(stoi(value) + stoi((config_map_.find("salve_threads_num"))->second->Value())); | ||
g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_); | ||
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. 不能在g_kiwi的层面UpdateOptions吗, g_kiwi->UpdateOptions(g_kiwi->options_); |
||
} | ||
if (key == "salve_threads_num") { | ||
g_kiwi->options_.SetThreadNum(stoi(value) + stoi((config_map_.find("worker_threads_num"))->second->Value())); | ||
g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_); | ||
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. 同上 |
||
} | ||
Comment on lines
+228
to
+235
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. Add proper error handling and validation for thread number updates. The thread number configuration has several issues:
if (key == "worker_threads_num") {
- g_kiwi->options_.SetThreadNum(stoi(value) + stoi((config_map_.find("salve_threads_num"))->second->Value()));
- g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_);
+ try {
+ auto slave_it = config_map_.find("salve_threads_num");
+ if (slave_it == config_map_.end()) {
+ return Status::InvalidArgument("Slave threads configuration not found");
+ }
+ int worker_threads = stoi(value);
+ int slave_threads = stoi(slave_it->second->Value());
+ if (worker_threads <= 0 || slave_threads <= 0) {
+ return Status::InvalidArgument("Thread numbers must be positive");
+ }
+ int64_t total_threads = static_cast<int64_t>(worker_threads) + static_cast<int64_t>(slave_threads);
+ if (total_threads > THREAD_MAX) {
+ return Status::InvalidArgument("Total thread count exceeds maximum limit");
+ }
+ g_kiwi->options_.SetThreadNum(static_cast<int>(total_threads));
+ g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_);
+ } catch (const std::exception& e) {
+ return Status::InvalidArgument("Invalid thread number value");
+ }
} Also, there appears to be a typo in the configuration key "salve_threads_num". Should it be "slave_threads_num"?
|
||
return iter->second->Set(value, init_stage); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,11 @@ class EventServer final { | |
|
||
void TCPConnect(const SocketAddr &addr, const std::function<void(std::string)> &cb); | ||
|
||
void UpdateOptions(const NetOptions &newOptions) { | ||
opt_ = newOptions; | ||
threadsManager_.reserve(opt_.GetThreadNum()); | ||
} | ||
|
||
Comment on lines
+75
to
+79
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. Add thread safety and validation to UpdateOptions. The
- void UpdateOptions(const NetOptions &newOptions) {
- opt_ = newOptions;
- threadsManager_.reserve(opt_.GetThreadNum());
- }
+ void UpdateOptions(const NetOptions &newOptions) {
+ if (newOptions.GetThreadNum() <= 0) {
+ throw std::invalid_argument("Thread number must be greater than 0");
+ }
+ std::lock_guard<std::mutex> lock(mtx_);
+ opt_ = newOptions;
+ if (threadsManager_.capacity() < opt_.GetThreadNum()) {
+ threadsManager_.reserve(opt_.GetThreadNum());
+ }
+ }
|
||
private: | ||
int StartThreadManager(bool serverMode); | ||
|
||
|
@@ -101,8 +106,7 @@ class EventServer final { | |
}; | ||
|
||
template <typename T> | ||
requires HasSetFdFunction<T> | ||
std::pair<bool, std::string> EventServer<T>::StartServer(int64_t interval) { | ||
requires HasSetFdFunction<T> std::pair<bool, std::string> EventServer<T>::StartServer(int64_t interval) { | ||
if (opt_.GetThreadNum() <= 0) { | ||
return std::pair(false, "thread num must be greater than 0"); | ||
} | ||
|
@@ -141,8 +145,7 @@ std::pair<bool, std::string> EventServer<T>::StartServer(int64_t interval) { | |
} | ||
|
||
template <typename T> | ||
requires HasSetFdFunction<T> | ||
std::pair<bool, std::string> EventServer<T>::StartClientServer() { | ||
requires HasSetFdFunction<T> std::pair<bool, std::string> EventServer<T>::StartClientServer() { | ||
if (opt_.GetThreadNum() <= 0) { | ||
return std::pair(false, "thread num must be greater than 0"); | ||
} | ||
|
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.
Add error handling for log level and redis mode settings.
The configuration updates lack error handling. Consider adding try-catch blocks for potential exceptions:
📝 Committable suggestion