Skip to content

Commit 61b507f

Browse files
committed
add fix for thundering herd on initial connection pool setup #8
1 parent 1c09199 commit 61b507f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,28 @@ defmodule Ecto.Adapters.SQLite3.Connection do
1313
import Ecto.Adapters.SQLite3.DataType
1414

1515
@parent_as __MODULE__
16-
@connect_buffer 50
16+
@idx_connect_buffer 5
17+
@rand_connect_buffer 50
18+
19+
# db_connection eagerly obtains and holds the configured
20+
# pool_size of connections, which can cause us to hit
21+
# "SQLite Busy" issues. we solve this by making sure to wait
22+
# a bit by hooking into the :configure callback opt and
23+
# sleeping an amount of time which is a function of the pool_index
24+
def handle_thundering_herd(opts) do
25+
case Keyword.get(opts, :pool_index) do
26+
idx -> :timer.sleep(idx * @idx_connect_buffer)
27+
nil -> :timer.sleep(:rand.uniform(@connect_buffer))
28+
end
1729

18-
def sleep(opts) do
19-
:timer.sleep(:rand.uniform(@connect_buffer))
2030
opts
2131
end
2232

2333
defp default_opts(opts) do
2434
# todo: we may want to consider wrapping any provided :configure
2535
# with our custom connection buffering logic
2636
opts
27-
|> Keyword.put_new(:configure, {__MODULE__, :sleep, []})
37+
|> Keyword.put_new(:configure, &handle_thundering_herd/1)
2838
end
2939

3040
def start_link(opts) do

0 commit comments

Comments
 (0)