hackney_load_regulation (hackney v2.0.0)

View Source

Per-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

acquire(Host, Port, MaxPerHost, Timeout)

-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.

current(Host, Port)

-spec current(Host :: string() | binary(), Port :: inet:port_number()) -> non_neg_integer().

Get the current number of active connections for a host.

init()

-spec init() -> ok.

Initialize the load regulation ETS table. Should be called once during application startup.

release(Host, Port)

-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.

reset(Host, Port)

-spec reset(Host :: string() | binary(), Port :: inet:port_number()) -> ok.

Reset the counter for a host (for testing).