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
20 changes: 20 additions & 0 deletions src/pikiwidb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,25 @@ 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 ", old_limit, " is not enough for PikiwiDB to start. PikiwiDB have successfully reconfig it to " , limit.rlim_cur);
} else {
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");
return -1;
}
}
return 0;
}
Copy link

@coderabbitai coderabbitai bot Jun 10, 2024

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 ", old_limit, " is not enough for PikiwiDB to start. PikiwiDB have successfully reconfig it to " , limit.rlim_cur);
} else {
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");
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;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

已修改

Copy link

Choose a reason for hiding this comment

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

@luky116, 感谢您的修改!如果有任何其他问题或需要进一步的帮助,请随时告知。


Learnings added
Learnt from: luky116
PR: OpenAtomFoundation/pikiwidb#346
File: src/pikiwidb.cc:0-0
Timestamp: 2024-07-01T23:31:59.313Z
Learning: When suggesting changes to log messages, ensure to use formatted strings instead of concatenation for better readability and to avoid potential issues.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


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

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