Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/pgbus/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ def recurring_execution_retention=(value)
# default formula provides.
POOL_SIZE_WARN_THRESHOLD = 50

# Connections needed per async worker: one for the reactor's serial
# execution, one for polling, one for headroom. Fibers share connections
# because only one runs at a time per reactor thread.
ASYNC_POOL_CONNECTIONS = 3

def resolved_pool_size
return pool_size if pool_size

Expand Down Expand Up @@ -624,10 +629,19 @@ def sum_thread_counts(entries, default_threads:, group:)
raise ArgumentError,
"#{group} threads must be a positive integer, got #{threads.inspect}"
end
threads

if async_execution_mode?(entry)
ASYNC_POOL_CONNECTIONS
else
threads
end
end
end

def async_execution_mode?(entry)
execution_mode_for(entry) == :async
end

def warn_if_oversized(size)
return unless size > POOL_SIZE_WARN_THRESHOLD

Expand Down
33 changes: 33 additions & 0 deletions spec/pgbus/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,39 @@
expect(warned_message).to match(/pool_size .* 62/)
end

it "uses fewer connections for async workers (fibers share connections)" do
config.pool_size = nil
config.workers = [{ queues: %w[webhooks], threads: 100, execution_mode: :async }]
# Async workers need ~3 connections (reactor + polling + headroom),
# not 100 (one per fiber). Total: 3 + 1 dispatcher + 1 scheduler = 5
expect(config.resolved_pool_size).to eq(5)
end

it "mixes async and thread workers correctly" do
config.pool_size = nil
config.workers = [
{ queues: %w[webhooks], threads: 50, execution_mode: :async },
{ queues: %w[default], threads: 5 }
]
# 3 (async) + 5 (threads) + 1 dispatcher + 1 scheduler = 10
expect(config.resolved_pool_size).to eq(10)
end

it "uses fewer connections for fiber mode (alias for async)" do
config.pool_size = nil
config.workers = [{ queues: %w[llm], threads: 200, execution_mode: :fiber }]
# 3 + 1 + 1 = 5
expect(config.resolved_pool_size).to eq(5)
end

it "honors global execution_mode when workers have no per-entry override" do
config.pool_size = nil
config.execution_mode = :async
config.workers = [{ queues: %w[default], threads: 50 }]
# Global async: 3 + 1 dispatcher + 1 scheduler = 5
expect(config.resolved_pool_size).to eq(5)
end

it "does not warn for normal sizes" do
config.pool_size = nil
config.workers = [{ queues: %w[default], threads: 5 }]
Expand Down