Skip to content

Commit

Permalink
feat: add InitLimit (OpenAtomFoundation#346)
Browse files Browse the repository at this point in the history
* add InitLimit
  • Loading branch information
luky116 authored and chanfun-ren committed Jul 20, 2024
1 parent 20a1c15 commit c345341
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 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,29 @@ 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 +282,7 @@ int main(int ac, char* av[]) {
daemonize();
}

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

0 comments on commit c345341

Please sign in to comment.