Skip to content

Commit

Permalink
feat(oom): let worker 0 process oom score lower than other worker
Browse files Browse the repository at this point in the history
  • Loading branch information
oowl committed Sep 12, 2023
1 parent ddc1400 commit 0df52ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
27 changes: 27 additions & 0 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0df52ba

Please sign in to comment.