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 @@ -216,6 +217,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",
old_limit, strerror(errno));

return -1;
}
}
return 0;
}

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

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