50 lines
1.8 KiB
INI
50 lines
1.8 KiB
INI
def get_max_memory()
|
|
return ENV['MEMORY_LIMIT_IN_BYTES'].to_i if ENV.has_key? 'MEMORY_LIMIT_IN_BYTES'
|
|
|
|
# Assume unlimited memory. 0.size is the number of bytes a Ruby
|
|
# Fixnum class can hold. One bit is used for sign and one is used
|
|
# by Ruby to determine whether it's a number or pointer to an object.
|
|
# That's why we subtract two bits. This expresion should therefore be
|
|
# the largest signed Fixnum possible.
|
|
(2 ** (8*0.size - 2) - 1)
|
|
end
|
|
|
|
def get_memory_per_worker()
|
|
bytes = ENV.fetch('MEMORY_BYTES_PER_WORKER', '0').to_i
|
|
if bytes == 0
|
|
# Comment describing rationale for choosing default of 256MiB/worker is below.
|
|
bytes = 256 * (2**20)
|
|
end
|
|
bytes
|
|
end
|
|
|
|
def get_min_threads()
|
|
ENV.fetch('PUMA_MIN_THREADS', '0').to_i
|
|
end
|
|
|
|
def get_max_threads()
|
|
ENV.fetch('PUMA_MAX_THREADS', '16').to_i
|
|
end
|
|
|
|
# Determine the maximum number of workers that are allowed by the available
|
|
# memory. Puma documentation recommends the maximum number of workers to be
|
|
# set to the number of cores.
|
|
# Unless we're specifically tuned otherwise, allow one worker process per 256MiB
|
|
# memory, to a maximum of 1 worker / core. Hopefully that'll be a reasonable
|
|
# starting point for average apps; if not, it's all tunable. The simple
|
|
# OpenShift ruby/rails sample app currently requires approx. 60MiB +
|
|
# 70MiB/worker before taking its first request, so hopefully a default of
|
|
# 256MiB/worker will give other simple apps reasonable default headroom.
|
|
def get_workers()
|
|
return ENV['PUMA_WORKERS'].to_i if ENV.has_key? 'PUMA_WORKERS'
|
|
|
|
cores = ENV.fetch('NUMBER_OF_CORES', '1').to_i
|
|
max_workers = get_max_memory() / get_memory_per_worker()
|
|
|
|
[cores, max_workers].min
|
|
end
|
|
|
|
environment ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'production'
|
|
threads get_min_threads(), get_max_threads()
|
|
workers get_workers()
|
|
bind 'tcp://0.0.0.0:8080'
|