Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion lib/event_store/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ defmodule EventStore.Config do
:backoff_max,
:backoff_min,
:backoff_type,
:checkout_retries,
:configure,
:connect_timeout,
:connection_listeners,
Expand All @@ -124,6 +125,8 @@ defmodule EventStore.Config do
:handshake_timeout,
:hostname,
:idle_interval,
:idle_limit,
:max_lifetime,
Comment thread
yordis marked this conversation as resolved.
:max_restarts,
:max_seconds,
:parameters,
Expand All @@ -148,8 +151,12 @@ defmodule EventStore.Config do
:username
]

# Only the main pool may recycle connections, every other connection holds
# state tied to its lifetime (advisory locks, LISTEN registrations).
def default_postgrex_opts(config) do
Keyword.take(config, @postgrex_connection_opts)
config
|> Keyword.take(@postgrex_connection_opts)
|> Keyword.delete(:max_lifetime)
end

def postgrex_opts(config, name) do
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule EventStore.Mixfile do

defp deps do
[
{:db_connection, "~> 2.10"},
{:gen_stage, "~> 1.2"},
{:postgrex, "~> 0.22"},
{:telemetry, "~> 1.0"},
Expand Down
34 changes: 34 additions & 0 deletions test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,40 @@ defmodule EventStore.ConfigTest do
]
end

describe "connection pool options" do
setup do
config =
Config.parse(
url: "postgres://username:password@localhost/database",
checkout_retries: 5,
idle_limit: 2,
max_lifetime: 480_000..540_000
)

[config: config]
end

test "main pool accepts every option", %{config: config} do
opts = Config.postgrex_opts(config, :name)

assert opts[:checkout_retries] == 5
assert opts[:idle_limit] == 2
assert opts[:max_lifetime] == 480_000..540_000
end

test "connections other than the main pool are never recycled", %{config: config} do
for opts <- [
Config.default_postgrex_opts(config),
Config.advisory_locks_postgrex_opts(config),
Config.postgrex_notifications_opts(config, :name)
] do
assert opts[:checkout_retries] == 5
assert opts[:idle_limit] == 2
refute Keyword.has_key?(opts, :max_lifetime)
end
end
end

test "parse url with query parameters" do
config = [
url: "postgres://username:password@localhost/database?ssl=true&pool_size=5&timeout=120000"
Expand Down
Loading