From 0df52ba8209221b4ec3214711589bb296fa2914b Mon Sep 17 00:00:00 2001 From: owl Date: Tue, 12 Sep 2023 14:57:28 +0800 Subject: [PATCH] feat(oom): let worker 0 process oom score lower than other worker --- kong/init.lua | 6 ++++++ kong/tools/utils.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) 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