minor fixes

This commit is contained in:
Jordan Bracco 2020-05-08 20:48:05 +02:00
parent b541ef9674
commit f5ad9dcd31

View file

@ -8,8 +8,8 @@ defmodule Limiter do
It supports two storage methods: It supports two storage methods:
* **`[atomics](https://erlang.org/doc/man/atomics.html)`** recommended and default if your OTP is > 21.2. * **[atomics](https://erlang.org/doc/man/atomics.html)** recommended and default if your OTP is > 21.2.
* **`[ets](https://erlang.org/doc/man/ets.html)`** either with a single table per Limiter (faster) or a shared table (better for a large number of limiters). * **[ets](https://erlang.org/doc/man/ets.html)** either with a single table per Limiter (faster) or a shared table.
You would however always want to use atomics, ets is mostly there for backwards compatibility. You would however always want to use atomics, ets is mostly there for backwards compatibility.
""" """
@ -18,17 +18,15 @@ defmodule Limiter do
Initializes a `Limiter`. Initializes a `Limiter`.
""" """
@spec new(name, max_running, max_waiting, options) :: :ok | {:error, :existing} @spec new(name, max_running, max_waiting, options) :: :ok | {:error, :existing} when name: atom(),
when name: atom(), max_running: non_neg_integer(),
max_running: non_neg_integer(), max_waiting: non_neg_integer() | :infinity,
max_waiting: non_neg_integer() | :infinity, options: [option],
options: [option], option: {:wait, non_neg_integer()} | backend,
option: {:wait, non_neg_integer()} | backend, backend: :atomics | ets_backend,
backend: :atomics | ets_backend, ets_backend: :ets | {:ets, atom()} | {:ets, ets_name :: atom(), ets_options :: []}
ets_backend: :ets | {:ets, atom()} | {:ets, ets_name :: atom(), ets_options :: []}
def new(name, max_running, max_waiting, options \\ []) do def new(name, max_running, max_waiting, options \\ []) do
name = atom_name(name) name = atom_name(name)
if defined?(name) do if defined?(name) do
{:error, :existing} {:error, :existing}
else else
@ -40,24 +38,18 @@ defmodule Limiter do
end end
end end
@spec set(name, new_max_running, new_max_waiting, options) :: :ok | :error @spec set(name, new_max_running, new_max_waiting, options) :: :ok | :error when name: atom(),
when name: atom(), new_max_running: non_neg_integer(),
new_max_running: non_neg_integer(), new_max_waiting: non_neg_integer() | :infinity,
new_max_waiting: non_neg_integer() | :infinity, options: [option],
options: [option], option: {:wait, non_neg_integer()}
option: {:wait, non_neg_integer()}
@doc "Adjust the limiter limits at runtime" @doc "Adjust the limiter limits at runtime"
def set(name, new_max_running, new_max_waiting, options \\ []) do def set(name, new_max_running, new_max_waiting, options \\ []) do
name = atom_name(name) name = atom_name(name)
if defined?(name) do if defined?(name) do
new_wait = Keyword.get(options, :wait) new_wait = Keyword.get(options, :wait)
{__MODULE__, max_running, max_waiting, backend, wait} = :persistent_term.get(name) {__MODULE__, max_running, max_waiting, backend, wait} = :persistent_term.get(name)
new = {__MODULE__, new_max_running || max_running, new_max_waiting || max_waiting, backend, new_wait || wait}
new =
{__MODULE__, new_max_running || max_running, new_max_waiting || max_waiting, backend,
new_wait || wait}
:persistent_term.put(name, new) :persistent_term.put(name, new)
:ok :ok
else else
@ -107,11 +99,11 @@ defmodule Limiter do
:atomics.add_get(ref, 1, 1) :atomics.add_get(ref, 1, 1)
end end
def dec({:ets, ets}, name) do defp dec({:ets, ets}, name) do
:ets.update_counter(ets, name, {2, -1}, {name, 0}) :ets.update_counter(ets, name, {2, -1}, {name, 0})
end end
def dec({:atomics, ref}, _) do defp dec({:atomics, ref}, _) do
:atomics.sub_get(ref, 1, 1) :atomics.sub_get(ref, 1, 1)
end end
@ -148,11 +140,11 @@ defmodule Limiter do
:undefined -> :ets.new(ets_name, [:public, :named_table] ++ options) :undefined -> :ets.new(ets_name, [:public, :named_table] ++ options)
_ -> nil _ -> nil
end end
{:ok, {:ets, ets_name}} {:ok, {:ets, ets_name}}
end end
defp setup_backend(:atomics) do defp setup_backend(:atomics) do
{:ok, {:atomics, :atomics.new(1, signed: true)}} {:ok, {:atomics, :atomics.new(1, [signed: true])}}
end end
end end