Skip to content
Open
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
52 changes: 52 additions & 0 deletions lib/ex_rated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ defmodule ExRated do
GenServer.call(:ex_rated, {:inspect_bucket, id, scale, limit})
end

@doc """
Decrease the amount remaining in the current bucket. If the number remaining is already
less than new_remaining, do nothing.

## Arguments:

- `id` (Erlang term()) name of the bucket
- `scale` (Integer) of time the bucket you want to inspect was created with.
- `limit` (Integer) representing the max counter size the bucket was created with.
- `new_remaining` (Integer) the new value for remaining, will replace current remaining iff it is less.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo in this line "iff"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
@spec decrease_remaining_to(
id :: any,
scale :: integer,
limit :: integer,
new_remaining :: integer
) :: :ok
def decrease_remaining_to(id, scale, limit, new_remaining) do
GenServer.cast(:ex_rated, {:decrease_remaining_to, id, scale, limit, new_remaining})
end

@doc """
Delete bucket to reset the counter.

Expand Down Expand Up @@ -144,6 +165,12 @@ defmodule ExRated do
{:reply, :ok, state}
end

def handle_cast({:decrease_remaining_to, id, scale, limit, new_remaining}, state) do
ets_table_name = ets_table_name()
decrease_reamaining_to(id, scale, limit, new_remaining, ets_table_name)
{:noreply, state}
end

def handle_cast(_msg, state) do
{:noreply, state}
end
Expand Down Expand Up @@ -257,6 +284,31 @@ defmodule ExRated do
end
end

defp decrease_reamaining_to(id, scale, limit, new_remaining, ets_table_name) do
{stamp, key} = stamp_key(id, scale)
new_count = limit - new_remaining

if 0 <= new_count and new_count <= limit do
case :ets.member(ets_table_name, key) do
false ->
# Insert Key {bucket_number, id} with counter (1), created_at (timestamp), updated_at (timestamp)
# The first element of the four element Tuple becomes the key.
true = :ets.insert(ets_table_name, {key, new_count, stamp, stamp})

true ->
import Ex2ms

:ets.select_replace(
ets_table_name,
fun do
{^key, count, created_at, _} when count < ^new_count ->
{^key, ^new_count, created_at, ^stamp}
end
)
end
end
end

defp stamp_key(id, scale) do
stamp = timestamp()
# with scale = 1 bucket changes every millisecond
Expand Down
23 changes: 23 additions & 0 deletions test/ex_rated_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ defmodule ExRatedServerTest do
end)
end

describe "decrease_remaining_to/4" do
test "decreases count on new bucket" do
assert {0, 10, _, nil, nil} = ExRated.inspect_bucket("my-bucket1", 1000, 10)
assert :ok == ExRated.decrease_remaining_to("my-bucket1", 1000, 10, 2)
assert {8, 2, _, _, _} = ExRated.inspect_bucket("my-bucket1", 1000, 10)
end

test "decreases count on existing bucket" do
assert {:ok, 1} = ExRated.check_rate("my-bucket1", 1000, 10)
assert :ok == ExRated.decrease_remaining_to("my-bucket1", 1000, 10, 2)
assert {8, 2, _, _, _} = ExRated.inspect_bucket("my-bucket1", 1000, 10)
end

test "if it is already below value, it leaves it unchanged" do
assert {0, 10, _, nil, nil} = ExRated.inspect_bucket("my-bucket1", 1000, 10)
assert {:ok, 1} = ExRated.check_rate("my-bucket1", 1000, 10)
assert {:ok, 2} = ExRated.check_rate("my-bucket1", 1000, 10)
assert {:ok, 3} = ExRated.check_rate("my-bucket1", 1000, 10)
assert :ok == ExRated.decrease_remaining_to("my-bucket1", 1000, 10, 9)
assert {3, 7, _, _, _} = ExRated.inspect_bucket("my-bucket1", 1000, 10)
end
end

defp start_server(_table, persistent) do
args = [{:timeout, 10_000}, {:cleanup_rate, 10_000}, {:persistent, persistent}]
opts = [name: :ex_rated]
Expand Down