Skip to content

Commit

Permalink
[cli] detect number of CPU cores
Browse files Browse the repository at this point in the history
* use nproc to detect number of cores
  - nproc correctly detects limits set by docker
  • Loading branch information
mikz committed Jan 18, 2018
1 parent a006dbe commit 0813608
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix lua docs formatting in the CORS policy [PR #530](https://github.com/3scale/apicast/pull/530)
- `post_action` phase not being called in the policy_chain [PR #539](https://github.com/3scale/apicast/pull/539)
- Failing to execute `libexec/boot` on some systems [PR #544](https://github.com/3scale/apicast/pull/544)
- Detect number of CPU cores in containers by using `nproc` [PR #554](https://github.com/3scale/apicast/pull/554)

## Changed

Expand Down
1 change: 0 additions & 1 deletion gateway/config/production.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
return {
worker_processes = 'auto',
master_process = 'on',
lua_code_cache = 'on',
}
2 changes: 1 addition & 1 deletion gateway/src/apicast/cli/command/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ local function configure(cmd)
cmd:flag("-d --daemon", "Daemonize.")
cmd:option("-w --workers",
"Number of worker processes to start.",
resty_env.value('APICAST_WORKERS') or 1)
resty_env.value('APICAST_WORKERS') or Environment.default_config.worker_processes)
cmd:option("-p --pid", "Path to the PID file.")
cmd:mutex(
cmd:flag('-b --boot',
Expand Down
9 changes: 9 additions & 0 deletions gateway/src/apicast/cli/environment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
local pl_path = require('pl.path')
local resty_env = require('resty.env')
local linked_list = require('apicast.linked_list')
local util = require('apicast.util')
local setmetatable = setmetatable
local loadfile = loadfile
local pcall = pcall
Expand Down Expand Up @@ -40,6 +41,13 @@ local function parse_nameservers()
end
end

local function cpus()
-- TODO: support /sys/fs/cgroup/cpuset/cpuset.cpus
-- see https://github.com/sclorg/rhscl-dockerfiles/blob/ff912d8764af9a41096e63064bbc325395afa608/rhel7.sti-base/bin/cgroup-limits#L55-L75
local nproc = util.system('nproc')
return tonumber(nproc)
end


local _M = {}
---
Expand All @@ -58,6 +66,7 @@ _M.default_config = {
ca_bundle = resty_env.value('SSL_CERT_FILE'),
policy_chain = require('apicast.policy_chain').default(),
nameservers = parse_nameservers(),
worker_processes = cpus() or 'auto',
package = {
path = package.path,
cpath = package.cpath,
Expand Down

0 comments on commit 0813608

Please sign in to comment.