hackney_load_regulation (hackney v2.0.0-beta.1)
View SourcePer-host connection load regulation using ETS counting semaphore.
This module provides per-host connection limits using an atomic counting semaphore pattern. It tracks the number of active connections per {Host, Port} and blocks new requests when the limit is reached.
Usage:
case hackney_load_regulation:acquire(Host, Port, MaxPerHost, Timeout) of
ok ->
try
%% Do work with connection
after
hackney_load_regulation:release(Host, Port)
end;
{error, timeout} ->
{error, checkout_timeout}
end.
Summary
Functions
Acquire a slot for the given host. Blocks with exponential backoff until a slot is available or timeout. Returns ok if slot acquired, {error, timeout} otherwise.
Get the current number of active connections for a host.
Initialize the load regulation ETS table. Should be called once during application startup.
Release a slot for the given host. Should always be called after acquire, typically in an after block.
Reset the counter for a host (for testing).
Functions
-spec acquire(Host :: string() | binary(), Port :: inet:port_number(), MaxPerHost :: pos_integer(), Timeout :: timeout()) -> ok | {error, timeout}.
Acquire a slot for the given host. Blocks with exponential backoff until a slot is available or timeout. Returns ok if slot acquired, {error, timeout} otherwise.
-spec current(Host :: string() | binary(), Port :: inet:port_number()) -> non_neg_integer().
Get the current number of active connections for a host.
-spec init() -> ok.
Initialize the load regulation ETS table. Should be called once during application startup.
-spec release(Host :: string() | binary(), Port :: inet:port_number()) -> ok.
Release a slot for the given host. Should always be called after acquire, typically in an after block.
-spec reset(Host :: string() | binary(), Port :: inet:port_number()) -> ok.
Reset the counter for a host (for testing).