diff --git a/kong/init.lua b/kong/init.lua index 5bb4aa68c97a..8a0bc2cfe658 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -873,6 +873,12 @@ function Kong.init_worker() stash_init_worker_error(err) return end + + ok, err = utils.set_worker_oom_score(ngx.worker.pid()) + if not ok then + ngx_log(ngx_WARN, "failed to set worker oom_score_adj: ", err) + return + end end diff --git a/kong/tools/utils.lua b/kong/tools/utils.lua index e1fcfba149ee..b2092ad055c8 100644 --- a/kong/tools/utils.lua +++ b/kong/tools/utils.lua @@ -1803,4 +1803,31 @@ do end _M.get_updated_now_ms = get_updated_now_ms +function _M.set_worker_oom_score(worker_id) + if not worker_id then + return nil, "missing worker_id" + end + + if worker_id ~= 0 then + return nil + end + + local oom_score = pl_file.read("/proc/self/oom_score_adj") + if not oom_score then + return nil, "could not read oom_score_adj" + end + + local oom_score_adj = tonumber(oom_score) - 100 + if oom_score_adj < -1000 then + oom_score_adj = -1000 + end + + local ok, err = pl_file.write("/proc/self/oom_score_adj", tostring(oom_score_adj)) + if not ok then + return nil, "could not write oom_score_adj: " .. err + end + + return true +end + return _M