From ba6faca02e7a3a8b953e1377f810da6e1a302c14 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 16 Apr 2020 13:53:45 -0400 Subject: [PATCH] make the number of workers configurable --- config/config.example.js | 8 ++++++++ lib/historyKeeper.js | 2 ++ lib/workers/index.js | 27 ++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/config/config.example.js b/config/config.example.js index 273c196d2..3826a7291 100644 --- a/config/config.example.js +++ b/config/config.example.js @@ -89,6 +89,14 @@ module.exports = { */ //httpSafePort: 3001, +/* CryptPad will launch a child process for every core available + * in order to perform CPU-intensive tasks in parallel. + * Some host environments may have a very large number of cores available + * or you may want to limit how much computing power CryptPad can take. + * If so, set 'maxWorkers' to a positive integer. + */ + // maxWorkers: 4, + /* ===================== * Admin * ===================== */ diff --git a/lib/historyKeeper.js b/lib/historyKeeper.js index 46efcc436..85bc73ab1 100644 --- a/lib/historyKeeper.js +++ b/lib/historyKeeper.js @@ -253,6 +253,8 @@ module.exports.create = function (config, cb) { channelExpirationMs: config.channelExpirationMs, verbose: config.verbose, openFileLimit: config.openFileLimit, + + maxWorkers: config.maxWorkers, }, w(function (err) { if (err) { throw new Error(err); diff --git a/lib/workers/index.js b/lib/workers/index.js index 02b3fb868..2ad7377a4 100644 --- a/lib/workers/index.js +++ b/lib/workers/index.js @@ -193,7 +193,32 @@ Workers.initialize = function (Env, config, _cb) { }; nThen(function (w) { - OS.cpus().forEach(function () { + const max = config.maxWorkers; + + var limit; + if (typeof(max) !== 'undefined') { + // the admin provided a limit on the number of workers + if (typeof(max) === 'number' && !isNaN(max)) { + if (max < 1) { + Log.info("INSUFFICIENT_MAX_WORKERS", max); + limit = 1; + } + } else { + Log.error("INVALID_MAX_WORKERS", '[' + max + ']'); + } + } + + var logged; + + OS.cpus().forEach(function (cpu, index) { + if (limit && index >= limit) { + if (!logged) { + logged = true; + Log.info('WORKER_LIMIT', "(Opting not to use available CPUs beyond " + index + ')'); + } + return; + } + initWorker(fork(DB_PATH), w(function (err) { if (!err) { return; } w.abort();