View Source ConcurrencyLimiter (ConcurrencyLimiter v1.0.1)

A simple resource pool that limits concurrency up to a number. Processes can wait in queue with a timeout.

Powered by Nimble Pool.

Processes can wait in queue with a timeout.

Examples:

iex> {:ok, pid} = ConcurrencyLimiter.start_link(max_concurrency: 5)
{:ok, pid}

iex> ConcurrencyLimiter.run!(pid, 5000, fn ->
  # Do some work
  :ok
end)
:ok

# Also accepts a name:
iex> {:ok, pid} = ConcurrencyLimiter.start_link(name: MyLimiter, max_concurrency: 5)
{:ok, pid}

iex> ConcurrencyLimiter.run!(MyLimiter, 5000, fn ->
  # Do some work
  :ok
end)
:ok

# Or with a supervisor:
iex> Supervisor.start_link([
  {ConcurrencyLimiter, name: LimiterA, max_concurrency: 5},
  {ConcurrencyLimiter, name: LimiterB, max_concurrency: 10},
  {ConcurrencyLimiter, name: LimiterC, max_concurrency: 50}
], strategy: :one_for_one)

iex> ConcurrencyLimiter.run!(LimiterB, 5000, fn ->
  # Do some work
  :ok
end)
:ok

Summary

Functions

Defines a ConcurrencyLimiter to be started under a supervisor.

Run a function with concurrency limited

Starts a concurrency limiter

Functions

@spec child_spec(Keyword.t()) :: Supervisor.child_spec()

Defines a ConcurrencyLimiter to be started under a supervisor.

Accepts the same options as start_link/1, plus:

  • :id - Optional. Defaults to the value of the :name option. If :name is not present, defaults to ConcurrencyLimiter. See Supervisor documentation for more info
  • :restart - Optional. See Supervisor documentation for more info
  • :shutdown - Optional. See Supervisor documentation for more info
Link to this function

run!(limiter, timeout, fun)

View Source
@spec run!(GenServer.server(), timeout(), function) :: result
when function: (-> result), result: var

Run a function with concurrency limited

Exits if the ConcurrencyLimiter doesn't exist, or if time out.

@spec start_link(Keyword.t()) :: GenServer.on_start()

Starts a concurrency limiter

Accepts two options:

  • :max_concurrency - Maximum concurrency. Required.
  • :name - name of the concurrency limiter