Skip to content

Add ability to update throttle by custom amount #27

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
{cover_export_enabled , true},
{coveralls_coverdata , "_build/test/cover/ct.coverdata"},
{coveralls_service_name , "travis-ci"},
{erl_opts, [nowarn_export_all]},
{deps, [{lager, "3.5.1"}]}]}]}.
8 changes: 4 additions & 4 deletions src/drivers/throttle_driver.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
-export([setup/0,
initialize/3,
reset/2,
update/2,
update/3,
lookup/2]).

%% Performs any global (non scope-specific) setup required by the driver.
Expand All @@ -20,7 +20,7 @@
-callback reset(throttle:scope(), NextReset :: integer()) -> ok.

%% Increase the access count for the scope/key and return its current value.
-callback update(throttle:scope(), Key :: term()) ->
-callback update(throttle:scope(), Key :: term(), Value :: pos_integer()) ->
{Count :: pos_integer(), throttle:rate_limit(), NextReset :: integer()} | rate_not_set.

%% Retrieve the current access count for the scope/key without increasing it.
Expand All @@ -39,9 +39,9 @@ reset(Scope, NextReset) ->
Module = callback_module(),
Module:reset(Scope, NextReset).

update(Scope, Key) ->
update(Scope, Key, Value) ->
Module = callback_module(),
Module:update(Scope, Key).
Module:update(Scope, Key, Value).

lookup(Scope, Key) ->
Module = callback_module(),
Expand Down
8 changes: 4 additions & 4 deletions src/drivers/throttle_ets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-export([setup/0,
initialize/3,
reset/2,
update/2,
update/3,
lookup/2]).

-define(STATE_TABLE, throttle_state_table).
Expand All @@ -27,12 +27,12 @@ reset(Scope, NextReset) ->
true = ets:insert(?STATE_TABLE, {Scope, TableId, Limit, NextReset}),
ok.

update(Scope, Key) ->
Copy link
Contributor

Choose a reason for hiding this comment

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

can you keep update/2 with a default value of 1?

Copy link
Author

Choose a reason for hiding this comment

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

For all the drivers, or? I thought the user facing api was via throttle.erl, which didn't have update/2 to begin with.

update(Scope, Key, Increment) ->
case ets:lookup(?STATE_TABLE, Scope) of
[{Scope, TableId, Limit, NextReset}] ->

%% add 1 to counter in position 2, if it's less or equal than Limit, default counter to 0
Count = ets:update_counter(TableId, Key, {2, 1, Limit, Limit}, {Key, 0}),
%% add `Increment' to counter in position 2, if it's less or equal than Limit, default counter to 0
Count = ets:update_counter(TableId, Key, {2, Increment, Limit, Limit}, {Key, 0}),

{Count, Limit, NextReset};
[] ->
Expand Down
6 changes: 3 additions & 3 deletions src/drivers/throttle_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
setup/0,
initialize/3,
reset/2,
update/2,
update/3,
lookup/2
]).

Expand Down Expand Up @@ -58,14 +58,14 @@ reset(Scope, NextReset) ->
NewState = State#scope_state{next_reset=NextReset},
ok = mnesia:dirty_write(scope_state, NewState).

update(Scope, Key) ->
update(Scope, Key, Increment) ->
case mnesia:dirty_read(scope_state, Scope) of
[#scope_state{limit=Limit,
next_reset=NextReset,
access_context=AccessContext}] ->

UpdateCounter = fun() ->
mnesia:dirty_update_counter(Scope, Key, 1)
mnesia:dirty_update_counter(Scope, Key, Increment)
end,

Count = mnesia:activity(AccessContext, UpdateCounter),
Expand Down
7 changes: 6 additions & 1 deletion src/throttle.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

-export([setup/3,
check/2,
update/3,
peek/2,

start_link/3,
Expand All @@ -25,7 +26,11 @@ setup(Scope, RateLimit, RatePeriod) ->
ok.

check(Scope, Key) ->
Result = throttle_driver:update(Scope, Key),
Result = throttle_driver:update(Scope, Key, 1),
count_result(Result).

update(Scope, Key, Value) when is_integer(Value), Value > 0 ->
Result = throttle_driver:update(Scope, Key, Value),
count_result(Result).

peek(Scope, Key) ->
Expand Down
2 changes: 1 addition & 1 deletion test/throttle_distributed_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ init_per_group(AccessContext, Config) ->
end,
[{sleep, Sleep} | Config].

end_per_group(_Context, Config) ->
end_per_group(_Context, _Config) ->
application:stop(throttle),
ok.

Expand Down
4 changes: 4 additions & 0 deletions test/throttle_test_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ test_limit(_Config) ->
{ok, 0, _} = throttle:check(test_rate, <<"john">>),
{limit_exceeded, 0, _} = throttle:check(test_rate, <<"john">>),

{ok, 1, _} = throttle:update(test_rate, <<"paul">>, 2),
{ok, 0, _} = throttle:update(test_rate, <<"paul">>, 1),
{limit_exceeded, 0, _} = throttle:update(test_rate, <<"paul">>, 2),

timer:sleep(1100),

{ok, 2, _} = throttle:check(test_rate, <<"john">>),
Expand Down