Sha256: 90275c9e17b507a5384adcf29621e70d3975e422fdb2ca01b41d180633c33df9

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

## This is the default algorithm
PumaAutoTune.hooks(:ram) do |auto|
  # Runs in a continual loop controlled by PumaAutoTune.frequency
  auto.set(:cycle) do |memory, master, workers|
    if memory > PumaAutoTune.ram # mb
      auto.call(:out_of_memory)
    else
      auto.call(:under_memory) if memory + workers.last.memory
    end
  end

  # Called repeatedly for `PumaAutoTune.reap_duration`.
  # call when you think you may have too many workers
  auto.set(:reap_cycle) do |memory, master, workers|
    if memory > PumaAutoTune.ram
      auto.call(:remove_worker)
    end
  end

  # Called when puma is using too much memory
  auto.set(:out_of_memory) do |memory, master, workers|
    largest_worker = workers.last # ascending worker size
    auto.log "Potential memory leak. Reaping largest worker", largest_worker_memory_mb: largest_worker.memory
    largest_worker.restart
    auto.call(:reap_cycle)
  end

  # Called when puma is not using all available memory
  # PumaAutoTune.max_workers is tracked automatically by `remove_worker`
  auto.set(:under_memory) do |memory, master, workers|
    theoretical_max_mb = memory + workers.first.memory # assending worker size
    if theoretical_max_mb < PumaAutoTune.ram && workers.size + 1 < PumaAutoTune.max_workers
      auto.call(:add_worker)
    else
      auto.log "All is well"
    end
  end

  # Called to add an extra worker
  auto.set(:add_worker) do |memory, master, workers|
    auto.log "Cluster too small. Resizing to add one more worker"
    master.add_worker
    auto.call(:reap_cycle)
  end

  # Called to remove 1 worker from pool. Sets maximum size
  auto.set(:remove_worker) do |memory, master, workers|
    auto.log "Cluster too large. Resizing to remove one worker"
    master.remove_worker
    auto.call(:reap_cycle)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
puma_auto_tune-0.0.1 lib/puma_auto_tune/defaults/ram/hooks.rb