From 9a4e3551a2c7d85f7568f1069f4e693b011caf1d Mon Sep 17 00:00:00 2001 From: Shiroko Date: Wed, 6 Nov 2024 16:22:12 +0800 Subject: [PATCH] feat(env): set maxreaders greater than the number nginx worker processes. --- src/ngx_lua_resty_lmdb_module.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/ngx_lua_resty_lmdb_module.c b/src/ngx_lua_resty_lmdb_module.c index 76d9f55..c097a1b 100644 --- a/src/ngx_lua_resty_lmdb_module.c +++ b/src/ngx_lua_resty_lmdb_module.c @@ -4,6 +4,9 @@ #define NGX_LUA_RESTY_LMDB_FILE_MODE 0600 #define NGX_LUA_RESTY_LMDB_DIR_MODE 0700 +/* 126 is the default number of max readers in LMDB */ +#define NGX_LUA_RESTY_LMDB_DEFAULT_READERS 126 +#define NGX_LUA_RESTY_LMDB_MAX_READERS_REDUNDANCY 16 #define NGX_LUA_RESTY_LMDB_VALIDATION_KEY "validation_tag" @@ -222,11 +225,30 @@ ngx_lua_resty_lmdb_open_file(ngx_cycle_t *cycle, { int rc; int dead; + size_t readers; + ngx_core_conf_t *ccf; if (ngx_lua_resty_lmdb_create_env(cycle, lcf, is_master) != NGX_OK) { return NGX_ERROR; } + /* Set max readers depending on the number of worker processes */ + ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); + readers = (size_t)ccf->worker_processes; + readers = readers > NGX_LUA_RESTY_LMDB_DEFAULT_READERS ? + (readers + NGX_LUA_RESTY_LMDB_MAX_READERS_REDUNDANCY) + : NGX_LUA_RESTY_LMDB_DEFAULT_READERS; + rc = mdb_env_set_maxreaders(lcf->env, readers); + if (rc != 0) { + ngx_log_error(NGX_LOG_CRIT, cycle->log, 0, + "unable to set max readers for LMDB environment: %s", + mdb_strerror(rc)); + mdb_env_close(lcf->env); + lcf->env = NULL; + + return NGX_ERROR; + } + rc = mdb_env_open(lcf->env, (const char *) lcf->env_path->name.data, 0, NGX_LUA_RESTY_LMDB_FILE_MODE);