HTTP: Implement max request limits

This commit is contained in:
rinpatch 2020-05-17 22:16:02 +03:00
commit 4128e3a84a
6 changed files with 49 additions and 11 deletions

View file

@ -244,7 +244,8 @@ defmodule Pleroma.Application do
end
defp http_children(Tesla.Adapter.Gun, _) do
Pleroma.Gun.ConnectionPool.children()
Pleroma.Gun.ConnectionPool.children() ++
[{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
end
defp http_children(_, _), do: []

View file

@ -49,4 +49,25 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
err -> err
end
end
@prefix Pleroma.Gun.ConnectionPool
def limiter_setup do
wait = Pleroma.Config.get([:connections_pool, :connection_acquisition_wait])
retries = Pleroma.Config.get([:connections_pool, :connection_acquisition_retries])
:pools
|> Pleroma.Config.get([])
|> Enum.each(fn {name, opts} ->
max_running = Keyword.get(opts, :size, 50)
max_waiting = Keyword.get(opts, :max_waiting, 10)
:ok =
ConcurrentLimiter.new(:"#{@prefix}.#{name}", max_running, max_waiting,
wait: wait,
max_retries: retries
)
end)
:ok
end
end

View file

@ -71,7 +71,13 @@ defmodule Pleroma.HTTP do
adapter = Application.get_env(:tesla, :adapter)
client = Tesla.client([Pleroma.HTTP.Middleware.FollowRedirects], adapter)
request(client, request)
maybe_limit(
fn ->
request(client, request)
end,
adapter,
adapter_opts
)
# Connection release is handled in a custom FollowRedirects middleware
err ->
@ -92,4 +98,13 @@ defmodule Pleroma.HTTP do
|> Builder.add_param(:query, :query, params)
|> Builder.convert_to_keyword()
end
@prefix Pleroma.Gun.ConnectionPool
defp maybe_limit(fun, Tesla.Adapter.Gun, opts) do
ConcurrentLimiter.limit(:"#{@prefix}.#{opts[:pool] || :default}", fun)
end
defp maybe_limit(fun, _, _) do
fun.()
end
end