Skip to content

Commit

Permalink
Avoid checking if all workers reached timeout unless idle timeout is …
Browse files Browse the repository at this point in the history
…configured (#3341)

Puma::Cluster#run constantly checks if all workers have reached their idle timeout even when the feature isn't being used. This change ensures that this check only runs when idle_timeout is configured.

Also includes a micro-optimisation which ensures the idle workers state hash isn't unnecessarily initialised until actually required (either a worker indicates timeout reached via pipe or Puma::Cluster#run checks if all_workers_idle_timed_out?).

All of these changes are covered by existing tests.
  • Loading branch information
joshuay03 committed Mar 11, 2024
1 parent 618257c commit 58c31b2
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/puma/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,6 @@ def setup_signals
def run
@status = :run

@idle_workers = {}

output_header "cluster"

# This is aligned with the output from Runner, see Runner#output_header
Expand Down Expand Up @@ -440,7 +438,7 @@ def run

while @status == :run
begin
if all_workers_idle_timed_out?
if @options[:idle_timeout] && all_workers_idle_timed_out?
log "- All workers reached idle timeout"
break
end
Expand Down Expand Up @@ -500,10 +498,10 @@ def run
booted = true
end
when Puma::Const::PipeRequest::IDLE
if @idle_workers[pid]
@idle_workers.delete pid
if idle_workers[pid]
idle_workers.delete pid
else
@idle_workers[pid] = true
idle_workers[pid] = true
end
end
else
Expand Down Expand Up @@ -608,7 +606,11 @@ def timeout_workers
end

def idle_timed_out_worker_pids
@idle_workers.keys
idle_workers.keys
end

def idle_workers
@idle_workers ||= {}
end
end
end

0 comments on commit 58c31b2

Please sign in to comment.