Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add &update_configuration/2 #5

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion lib/leaky.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ defmodule Leaky do
GenServer.cast(GenServer.whereis(name), {:increment_tokens_left, bucket, amount})
end

@doc """
Updates the configuration of the rate limiter process. Changes are applied immediately, affecting the rate limiter's behavior.
It is useful for dynamically adjusting the rate limiter's settings without restarting the process.

Options which can be updated: `max_accumulated`, `interval`, and `refill`.


## Example

```
iex> Leaky.update_configuration(max_accumulated: 10, refill: 2, interval: 5)
:ok
iex> {:allow, 8} == Leaky.acquire(:user_requests, 2)
true
```

"""
@spec update_configuration(opts :: Keyword.t(), name :: GenServer.name()) :: :ok
def update_configuration(opts, name \\ __MODULE__) do
GenServer.cast(GenServer.whereis(name), {:update_configuration, opts})
end

def start_link(opts) do
bucket_name = Keyword.fetch!(opts, :bucket_name)
max_accumulated = Keyword.fetch!(opts, :max_accumulated)
Expand Down Expand Up @@ -130,7 +152,7 @@ defmodule Leaky do
end

@impl GenServer
def handle_cast({:increment_tokens_left, bucket, amount}, state) do
def handle_cast({:increment_tokens_left, bucket, amount}, %State{} = state) do
now = :erlang.system_time(:milli_seconds)
table = state.bucket_name

Expand All @@ -150,6 +172,14 @@ defmodule Leaky do
{:noreply, state}
end

def handle_cast({:update_configuration, opts}, %State{} = state) do
max_accumulated = Keyword.get(opts, :max_accumulated, state.max_accumulated)
interval = Keyword.get(opts, :interval, state.interval)
refill = Keyword.get(opts, :refill, state.refill)

{:noreply, %{state | max_accumulated: max_accumulated, interval: interval, refill: refill}}
end

@impl GenServer
def handle_call({:inspect, bucket}, _from, %State{} = state) do
available_tokens =
Expand Down
8 changes: 8 additions & 0 deletions test/leaky_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ defmodule LeakyTest do
end
end

describe "update_configuration/2" do
test "updates configuration in a running process" do
Leaky.update_configuration(max_accumulated: 10, refill: 2, interval: 5)

assert %Leaky.State{max_accumulated: 10, refill: 2, interval: 5} = :sys.get_state(Process.whereis(Leaky))
end
end

describe "naming options" do
test "works with {:via, _, _} name" do
start_supervised!({Registry, [name: Leaky.Registry, keys: :unique]})
Expand Down
Loading