Top Level Namespace

Defined Under Namespace

Modules: PumaAutoTune

Instance Method Summary collapse

Instance Method Details

#ramObject

This is the default algorithm



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puma_auto_tune/defaults/ram/hooks.rb', line 2

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