File tree Expand file tree Collapse file tree 1 file changed +14
-4
lines changed
lib/ecto/adapters/sqlite3 Expand file tree Collapse file tree 1 file changed +14
-4
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments