Skip to content

Commit

Permalink
Introduce every_ms api to amoc throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jun 14, 2024
1 parent 48538e0 commit 6fdad6c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/throttle/amoc_throttle.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
%% Throttle unit of measurement
-type config() :: #{rate := rate(),
interval => interval(),
parallelism => non_neg_integer()}
| #{every := non_neg_integer(),
parallelism => non_neg_integer()}.
%% Literal throttle configuration
%% Literal throttle configuration. It can state `every', in milliseconds,
%% in which case the rate per interval is calculated to allow one event every given milliseconds,
%% or, literally give the rate per interval.
-type gradual_rate_config() :: #{from_rate := rate(),
to_rate := rate(),
interval => interval(),
Expand Down
7 changes: 7 additions & 0 deletions src/throttle/amoc_throttle_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ start_link() ->
-spec ensure_throttle_processes_started(name(), amoc_throttle:config()) ->
{ok, started | already_started} |
{error, invalid_throttle | wrong_reconfiguration | wrong_no_of_procs}.
ensure_throttle_processes_started(
Name, #{every := EveryMs} = Config)
when is_atom(Name), ?NONNEG_INT(EveryMs) ->
raise_event_on_slave_node(Name, init),
Config1 = #{rate => ?DEFAULT_INTERVAL div EveryMs, interval => ?DEFAULT_INTERVAL},
Config2 = Config1#{parallelism => maps:get(parallelism, Config, ?DEFAULT_NO_PROCESSES)},
gen_server:call(?MASTER_SERVER, {start_processes, Name, Config2});
ensure_throttle_processes_started(
Name, #{rate := Rate, interval := Interval, parallelism := NoOfProcesses} = Config)
when is_atom(Name), ?POS_INT(Rate), ?NONNEG_INT(Interval), ?POS_INT(NoOfProcesses) ->
Expand Down
12 changes: 12 additions & 0 deletions test/throttle_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ groups() ->
[
start,
start_descriptive,
start_every,
rate_zero_is_not_accepted,
low_rate_gets_remapped,
low_interval_get_remapped,
Expand Down Expand Up @@ -77,6 +78,17 @@ start_descriptive(_) ->
Description = #{rate => 100, interval => 5000, parallelism => 12},
?assertMatch({ok, started}, amoc_throttle:start(?FUNCTION_NAME, Description)).

start_every(_) ->
%% Starts successfully
Description = #{every => 50, parallelism => 1},
?assertMatch({ok, started}, amoc_throttle:start(?FUNCTION_NAME, Description)),
State = get_state_of_one_process(?FUNCTION_NAME),
?assertMatch(#{name := ?FUNCTION_NAME,
interval := 60000,
delay_between_executions := 50,
n := 1200},
State).

rate_zero_is_not_accepted(_) ->
?assertMatch({error, invalid_throttle}, amoc_throttle:start(?FUNCTION_NAME, 0, 100, 1)).

Expand Down

0 comments on commit 6fdad6c

Please sign in to comment.