Skip to content
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 InitLimit #346

Merged
merged 14 commits into from
Jul 6, 2024
26 changes: 26 additions & 0 deletions src/pikiwidb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "pikiwidb.h"

#include <sys/fcntl.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
Expand Down Expand Up @@ -218,6 +219,30 @@ static void InitLogs() {
#endif
}

static int InitLimit() {
rlimit limit;
rlim_t maxfiles = g_config.max_clients;
if (getrlimit(RLIMIT_NOFILE, &limit) == -1) {
WARN("getrlimit error: {}", strerror(errno));
} else if (limit.rlim_cur < maxfiles) {
rlim_t old_limit = limit.rlim_cur;
limit.rlim_cur = maxfiles;
limit.rlim_max = maxfiles;
if (setrlimit(RLIMIT_NOFILE, &limit) != -1) {
WARN("your 'limit -n ' of {} is not enough for PikiwiDB to start. PikiwiDB have successfully reconfig it to ",
old_limit, limit.rlim_cur);
} else {
ERROR(
"your 'limit -n ' of {} is not enough for PikiwiDB to start. PikiwiDB can not reconfig it({}), do it by "
"yourself",
AlexStocks marked this conversation as resolved.
Show resolved Hide resolved
old_limit, strerror(errno));

return -1;
}
}
return 0;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改进日志消息的格式化。

当前使用字符串连接的方式记录日志,建议使用格式化字符串以提高可读性和性能。

- WARN("getrlimit error:  ", strerror(errno));
+ WARN("getrlimit error: %s", strerror(errno));
- WARN( "your 'limit -n ' of ", old_limit, " is not enough for PikiwiDB to start. PikiwiDB have successfully reconfig it to " , limit.rlim_cur);
+ WARN("your 'limit -n ' of %ld is not enough for PikiwiDB to start. PikiwiDB have successfully reconfig it to %ld", old_limit, limit.rlim_cur);
- ERROR("your 'limit -n ' of " , old_limit, " is not enough for PikiwiDB to start. PikiwiDB can not reconfig it(", strerror(errno), "), do it by yourself");
+ ERROR("your 'limit -n ' of %ld is not enough for PikiwiDB to start. PikiwiDB can not reconfig it(%s), do it by yourself", old_limit, strerror(errno));
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static int InitLimit() {
rlimit limit;
rlim_t maxfiles = g_config.max_clients;
if (getrlimit(RLIMIT_NOFILE, &limit) == -1) {
WARN("getrlimit error: {}", strerror(errno));
} else if (limit.rlim_cur < maxfiles) {
rlim_t old_limit = limit.rlim_cur;
limit.rlim_cur = maxfiles;
limit.rlim_max = maxfiles;
if (setrlimit(RLIMIT_NOFILE, &limit) != -1) {
WARN("your 'limit -n ' of {} is not enough for PikiwiDB to start. PikiwiDB have successfully reconfig it to ",
old_limit, limit.rlim_cur);
} else {
ERROR(
"your 'limit -n ' of {} is not enough for PikiwiDB to start. PikiwiDB can not reconfig it({}), do it by "
"yourself",
old_limit, strerror(errno));
return -1;
}
}
return 0;
}
static int InitLimit() {
rlimit limit;
rlim_t maxfiles = g_config.max_clients;
if (getrlimit(RLIMIT_NOFILE, &limit) == -1) {
WARN("getrlimit error: %s", strerror(errno));
} else if (limit.rlim_cur < maxfiles) {
rlim_t old_limit = limit.rlim_cur;
limit.rlim_cur = maxfiles;
limit.rlim_max = maxfiles;
if (setrlimit(RLIMIT_NOFILE, &limit) != -1) {
WARN("your 'limit -n ' of %ld is not enough for PikiwiDB to start. PikiwiDB have successfully reconfig it to %ld", old_limit, limit.rlim_cur);
} else {
ERROR("your 'limit -n ' of %ld is not enough for PikiwiDB to start. PikiwiDB can not reconfig it(%s), do it by yourself", old_limit, strerror(errno));
return -1;
}
}
return 0;
}


static void daemonize() {
if (fork()) {
exit(0); /* parent exits */
Expand Down Expand Up @@ -260,6 +285,7 @@ int main(int ac, char* av[]) {
daemonize();
}

InitLimit();
pstd::InitRandom();
SignalSetup();
InitLogs();
Expand Down
Loading