From a059d993e5dd8e6f147909e29f9625deb12f8e6c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 17 Dec 2018 17:07:07 -0500
Subject: [PATCH] manager: increase open files limit if too low

Change-Id: Ic430a8d808ba25f45eec8a92e28aa635e1b69ba9
---
 src/manager.cpp | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/manager.cpp b/src/manager.cpp
index 9955fd71ab..09f8695099 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -81,6 +81,11 @@ using random_device = dht::crypto::random_device;
 
 #include "data_transfer.h"
 
+#ifndef WIN32
+#include <sys/time.h>
+#include <sys/resource.h>
+#endif
+
 #include <cerrno>
 #include <ctime>
 #include <cstdlib>
@@ -670,6 +675,17 @@ Manager::init(const std::string &config_file)
     // FIXME: this is no good
     initialized = true;
 
+#ifndef WIN32
+    // Set the max number of open files.
+    struct rlimit nofiles;
+    if (getrlimit(RLIMIT_NOFILE, &nofiles) == 0) {
+        if (nofiles.rlim_cur < nofiles.rlim_max && nofiles.rlim_cur < 1024u) {
+            nofiles.rlim_cur = std::min<rlim_t>(nofiles.rlim_max, 8192u);
+            setrlimit(RLIMIT_NOFILE, &nofiles);
+        }
+    }
+#endif
+
 #define PJSIP_TRY(ret) do {                                  \
         if (ret != PJ_SUCCESS)                               \
             throw std::runtime_error(#ret " failed");        \
-- 
GitLab