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

feat: improve inspect function #7

Merged
merged 1 commit into from
May 7, 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
21 changes: 7 additions & 14 deletions lib/leaky.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule Leaky do
@spec acquire(bucket :: bucket(), cost :: integer, name :: GenServer.name()) ::
{:allow, integer} | :deny
def acquire(bucket, cost, name \\ __MODULE__) do
GenServer.call(GenServer.whereis(name), {:acquire, bucket, cost})
GenServer.call(name, {:acquire, bucket, cost})
end

@doc """
Expand All @@ -85,7 +85,10 @@ defmodule Leaky do
"""
@spec inspect(bucket :: bucket(), name :: GenServer.name()) :: integer | nil
def inspect(bucket, name \\ __MODULE__) do
GenServer.call(GenServer.whereis(name), {:inspect, bucket})
case GenServer.call(name, {:acquire, bucket, 0}) do
{:allow, tokens_left} -> tokens_left
:deny -> 0
end
end

@doc """
Expand All @@ -94,7 +97,7 @@ defmodule Leaky do
"""
@spec adjust_tokens(bucket :: bucket(), amount :: integer, name :: GenServer.name()) :: :ok
def adjust_tokens(bucket, amount, name \\ __MODULE__) do
GenServer.cast(GenServer.whereis(name), {:increment_tokens_left, bucket, amount})
GenServer.cast(name, {:increment_tokens_left, bucket, amount})
end

@doc """
Expand All @@ -116,7 +119,7 @@ defmodule Leaky do
"""
@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})
GenServer.cast(name, {:update_configuration, opts})
end

def start_link(opts) do
Expand Down Expand Up @@ -182,16 +185,6 @@ defmodule Leaky do
end

@impl GenServer
def handle_call({:inspect, bucket}, _from, %State{} = state) do
available_tokens =
case :ets.lookup(state.bucket_name, bucket) do
[] -> nil
[{_bucket, tokens_left, _last_inserted_at}] -> tokens_left
end

{:reply, available_tokens, state}
end

def handle_call({:acquire, bucket, cost}, _from, %State{} = state) do
now = :erlang.system_time(:milli_seconds)
table = state.bucket_name
Expand Down
7 changes: 4 additions & 3 deletions test/leaky_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ defmodule LeakyTest do

@bucket :bucket
@cost 1
@max_accumulated 4

setup do
configuration = [max_accumulated: 4, refill: 1, interval: 10, bucket_name: @bucket]
configuration = [max_accumulated: @max_accumulated, refill: 1, interval: 10, bucket_name: @bucket]
start_supervised!({Leaky, configuration})

:ok
Expand All @@ -31,7 +32,7 @@ defmodule LeakyTest do
test "returns amount of available tokens" do
assert {:allow, 3} == Leaky.acquire(@bucket, @cost)

assert 3 == Leaky.inspect(@bucket)
assert 3.0 == Leaky.inspect(@bucket)
end
end

Expand All @@ -50,7 +51,7 @@ defmodule LeakyTest do

:timer.sleep(2)

assert 2 == Leaky.inspect(@bucket)
assert 2 < Leaky.inspect(@bucket)
end
end

Expand Down
Loading