Skip to content

Commit fff23f8

Browse files
authored
Separate Registry pool size configuration (#198)
1 parent 95b4ad2 commit fff23f8

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

lib/phoenix/pubsub.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ defmodule Phoenix.PubSub do
164164
* `:adapter` - the adapter to use (defaults to `Phoenix.PubSub.PG2`)
165165
* `:pool_size` - number of pubsub partitions to launch
166166
(defaults to one partition for every 4 cores)
167+
* `:registry_size` - number of `Registry` partitions to launch
168+
(defaults to `:pool_size`). This controls the number of Registry partitions
169+
used for storing subscriptions and can be tuned independently from `:pool_size`
170+
for better performance characteristics.
167171
* `:broadcast_pool_size` - number of pubsub partitions used for broadcasting messages
168172
(defaults to `:pool_size`). This option is used during pool size migrations to ensure
169173
no messages are lost. See the "Safe Pool Size Migration" section in the module documentation.

lib/phoenix/pubsub/supervisor.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule Phoenix.PubSub.Supervisor do
2222
adapter_name = Module.concat(name, "Adapter")
2323

2424
partitions =
25-
opts[:pool_size] ||
25+
opts[:registry_size] || opts[:pool_size] ||
2626
System.schedulers_online() |> Kernel./(4) |> Float.ceil() |> trunc()
2727

2828
registry = [

test/shared/pubsub_test.exs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ defmodule Phoenix.PubSubTest do
4141

4242
setup config do
4343
size = config[:pool_size] || 1
44+
registry_size = config[:registry_size] || config[:registry_pool_size] || config[:pool_size] || 1
4445
{adapter, adapter_opts} = Application.get_env(:phoenix_pubsub, :test_adapter)
45-
adapter_opts = [adapter: adapter, name: config.test, pool_size: size] ++ adapter_opts
46+
adapter_opts = [adapter: adapter, name: config.test, pool_size: size, registry_size: registry_size] ++ adapter_opts
4647
start_supervised!({Phoenix.PubSub, adapter_opts})
4748

4849
opts = %{
4950
pubsub: config.test,
5051
topic: to_string(config.test),
5152
pool_size: size,
52-
node: Phoenix.PubSub.node_name(config.test)
53+
node: Phoenix.PubSub.node_name(config.test),
54+
adapter_name: Module.concat(config.test, "Adapter")
5355
}
5456

5557
{:ok, opts}
@@ -174,4 +176,23 @@ defmodule Phoenix.PubSubTest do
174176
assert_receive {:custom, :special, :none, :direct}
175177
end
176178
end
179+
180+
@tag pool_size: 4
181+
@tag registry_size: 2
182+
test "PubSub pool size can be configured separately from the Registry partitions",
183+
config do
184+
assert {:duplicate, 2, _} = :ets.lookup_element(config.pubsub, -2, 2)
185+
186+
assert :persistent_term.get(config.adapter_name) ==
187+
{config.adapter_name, :"#{config.adapter_name}_2", :"#{config.adapter_name}_3", :"#{config.adapter_name}_4"}
188+
end
189+
190+
@tag pool_size: 3
191+
test "Registry partitions are configured with the same pool size as PubSub if not specified",
192+
config do
193+
assert {:duplicate, 3, _} = :ets.lookup_element(config.pubsub, -2, 2)
194+
195+
assert :persistent_term.get(config.adapter_name) ==
196+
{config.adapter_name, :"#{config.adapter_name}_2", :"#{config.adapter_name}_3"}
197+
end
177198
end

0 commit comments

Comments
 (0)