From 271b1723ac3de9960aa50f403a63df8729687b98 Mon Sep 17 00:00:00 2001 From: Doug Hoyte Date: Thu, 2 Feb 2023 15:42:05 -0500 Subject: [PATCH] setrlimit nofiles config --- golpe.yaml | 4 ++++ src/onAppStartup.cpp | 25 ++++++++++++++++++++++++- strfry.conf | 17 ++++++++++++++--- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/golpe.yaml b/golpe.yaml index 9603fdb..a47e032 100644 --- a/golpe.yaml +++ b/golpe.yaml @@ -105,6 +105,10 @@ config: desc: "Port to open for the nostr websocket protocol" default: 7777 noReload: true + - name: relay__nofiles + desc: "Set OS-limit on maximum number of open files/sockets (if 0, don't attempt to set)" + default: 1000000 + noReload: true - name: relay__info__name desc: "NIP-11: Name of this server. Short/descriptive (< 30 characters)" diff --git a/src/onAppStartup.cpp b/src/onAppStartup.cpp index 60c0cd6..26e810d 100644 --- a/src/onAppStartup.cpp +++ b/src/onAppStartup.cpp @@ -1,8 +1,13 @@ +#include +#include +#include +#include + #include "golpe.h" const size_t CURR_DB_VERSION = 1; -void onAppStartup(lmdb::txn &txn, const std::string &cmd) { +static void dbCheck(lmdb::txn &txn, const std::string &cmd) { auto dbTooOld = [&](uint64_t ver) { LE << "Database version too old: " << ver << ". Expected version " << CURR_DB_VERSION; LE << "You should 'strfry export' your events, delete (or move) the DB files, and 'strfry import' them"; @@ -47,3 +52,21 @@ void onAppStartup(lmdb::txn &txn, const std::string &cmd) { dbTooNew(s->dbVersion()); } } + +static void setRLimits() { + if (!cfg().relay__nofiles) return; + struct rlimit curr; + + if (getrlimit(RLIMIT_NOFILE, &curr)) throw herr("couldn't call getrlimit: ", strerror(errno)); + + if (cfg().relay__nofiles > curr.rlim_max) throw herr("Unable to set NOFILES limit to ", cfg().relay__nofiles, ", exceeds max of ", curr.rlim_max); + + curr.rlim_cur = cfg().relay__nofiles; + + if (setrlimit(RLIMIT_NOFILE, &curr)) throw herr("Failed setting NOFILES limit to ", cfg().relay__nofiles, ": ", strerror(errno)); +} + +void onAppStartup(lmdb::txn &txn, const std::string &cmd) { + dbCheck(txn, cmd); + setRLimits(); +} diff --git a/strfry.conf b/strfry.conf index 6b9f2a2..7308c20 100644 --- a/strfry.conf +++ b/strfry.conf @@ -2,9 +2,17 @@ ## Default strfry config ## -# Directory that contains strfry database (restart required) +# Directory that contains the strfry LMDB database (restart required) db = "./strfry-db/" +dbParams { + # Maximum number of threads/processes that can simultaneously have LMDB transactions open (restart required) + maxreaders = 256 + + # Size of mmap() to use when loading LMDB (does *not* correspond to disk-space used, default is 10TB) (restart required) + mapsize = 10995116277760 +} + relay { # Interface to listen on. Use 0.0.0.0 to listen on all interfaces (restart required) bind = "127.0.0.1" @@ -12,6 +20,9 @@ relay { # Port to open for the nostr websocket protocol (restart required) port = 7777 + # Set OS-limit on maximum number of open files/sockets (if 0, don't attempt to set) (restart required) + nofiles = 1000000 + info { # NIP-11: Name of this server. Short/descriptive (< 30 characters) name = "strfry default" @@ -42,10 +53,10 @@ relay { maxFilterLimit = 500 compression { - # Use permessage-deflate compression if supported by client. Reduces bandwidth, but slight increase in CPU + # Use permessage-deflate compression if supported by client. Reduces bandwidth, but slight increase in CPU (restart required) enabled = true - # Maintain a sliding window buffer for each connection. Improves compression, but uses more memory + # Maintain a sliding window buffer for each connection. Improves compression, but uses more memory (restart required) slidingWindow = true }