Skip to content

Commit d1a5da7

Browse files
committed
feat: implement Oban connection pooling
Add dedicated Oban repository with connection pooling to improve database performance and prevent connection starvation. Includes dynamic repo selection for transaction handling.
1 parent 3a23577 commit d1a5da7

File tree

7 files changed

+43
-1
lines changed

7 files changed

+43
-1
lines changed

config/config.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ config :algora, AlgoraWeb.Endpoint,
4141

4242
config :algora, Oban,
4343
notifier: Oban.Notifiers.PG,
44-
repo: Algora.Repo,
44+
repo: Algora.ObanRepo,
45+
get_dynamic_repo: {Algora.Repo, :oban_repo, []},
4546
queues: [
4647
# --- #
4748
event_consumers: 1,

config/dev.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ config :algora, Algora.Repo,
3333
migration_primary_key: [type: :string],
3434
migration_timestamps: [type: :utc_datetime_usec]
3535

36+
# Configure dedicated Oban database repository
37+
config :algora, Algora.ObanRepo,
38+
url: System.get_env("DATABASE_URL"),
39+
stacktrace: true,
40+
show_sensitive_data_on_connection_error: true,
41+
pool_size: 10,
42+
migration_primary_key: [type: :string],
43+
migration_timestamps: [type: :utc_datetime_usec]
44+
3645
config :ex_aws,
3746
json_codec: Jason,
3847
access_key_id: System.get_env("AWS_ACCESS_KEY_ID"),

config/runtime.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ if config_env() == :prod do
6161
migration_timestamps: [type: :utc_datetime_usec],
6262
queue_target: 5000
6363

64+
# Configure dedicated Oban database repository
65+
config :algora, Algora.ObanRepo,
66+
# ssl: true,
67+
url: database_url,
68+
pool_size: String.to_integer(System.get_env("OBAN_POOL_SIZE") || "10"),
69+
socket_options: maybe_ipv6,
70+
migration_primary_key: [type: :string],
71+
migration_timestamps: [type: :utc_datetime_usec],
72+
queue_target: 5000
73+
6474
config :ex_aws,
6575
json_codec: Jason,
6676
access_key_id: System.fetch_env!("AWS_ACCESS_KEY_ID"),

config/test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ config :algora, Algora.Repo,
1212
migration_primary_key: [type: :string],
1313
migration_timestamps: [type: :utc_datetime_usec]
1414

15+
# Configure dedicated Oban database repository for testing
16+
config :algora, Algora.ObanRepo,
17+
url: System.get_env("TEST_DATABASE_URL"),
18+
pool: Ecto.Adapters.SQL.Sandbox,
19+
pool_size: System.schedulers_online() * 2,
20+
migration_primary_key: [type: :string],
21+
migration_timestamps: [type: :utc_datetime_usec]
22+
1523
# We don't run a server during test. If one is required,
1624
# you can enable the server option below.
1725
config :algora, AlgoraWeb.Endpoint,

lib/algora/application.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule Algora.Application do
1414
{NodeJS.Supervisor, [path: LiveSvelte.SSR.NodeJS.server_path(), pool_size: 4]},
1515
AlgoraWeb.Telemetry,
1616
Algora.Repo,
17+
Algora.ObanRepo,
1718
{Oban, Application.fetch_env!(:algora, Oban)},
1819
{DNSCluster, query: Application.get_env(:algora, :dns_cluster_query) || :ignore},
1920
{Phoenix.PubSub, name: Algora.PubSub},

lib/algora/oban_repo.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule Algora.ObanRepo do
2+
use Ecto.Repo,
3+
adapter: Ecto.Adapters.Postgres,
4+
otp_app: :algora
5+
end

lib/algora/repo.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ defmodule Algora.Repo do
33
otp_app: :algora,
44
adapter: Ecto.Adapters.Postgres
55

6+
def oban_repo do
7+
if in_transaction?() do
8+
__MODULE__
9+
else
10+
Algora.ObanRepo
11+
end
12+
end
13+
614
alias Algora.Activities.Activity
715
alias Algora.Activities.Notifier
816

0 commit comments

Comments
 (0)