Skip to content

Commit af068a6

Browse files
committed
Introduce interarrival api to amoc throttle
1 parent 47e7f6c commit af068a6

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/throttle/amoc_throttle.erl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@
2323
%% controlled, which corresponds to the number of processes started
2424
-type throttle() :: #{rate := rate(),
2525
interval := interval()}.
26+
-type interarrival() :: #{interarrival := non_neg_integer()}.
2627
%% Throttle unit of measurement
2728
-type config() :: #{rate := rate(),
2829
interval => interval(),
30+
parallelism => non_neg_integer()}
31+
| #{interarrival := non_neg_integer(),
2932
parallelism => non_neg_integer()}.
30-
%% Literal throttle configuration
33+
%% Literal throttle configuration. It can state `interarrival', in milliseconds,
34+
%% in which case the rate per interval is calculated to allow one event every given milliseconds,
35+
%% or, literally give the rate per interval.
36+
3137
-type gradual_rate_config() :: #{from_rate := rate(),
3238
to_rate := rate(),
3339
interval => interval(),

src/throttle/amoc_throttle_controller.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ start_link() ->
7070
-spec ensure_throttle_processes_started(name(), amoc_throttle:config()) ->
7171
{ok, started | already_started} |
7272
{error, invalid_throttle | wrong_reconfiguration | wrong_no_of_procs}.
73+
ensure_throttle_processes_started(
74+
Name, #{interarrival := EveryMs} = Config)
75+
when is_atom(Name), ?NONNEG_INT(EveryMs) ->
76+
raise_event_on_slave_node(Name, init),
77+
Config1 = #{rate => ?DEFAULT_INTERVAL div EveryMs, interval => ?DEFAULT_INTERVAL},
78+
Config2 = Config1#{parallelism => maps:get(parallelism, Config, ?DEFAULT_NO_PROCESSES)},
79+
gen_server:call(?MASTER_SERVER, {start_processes, Name, Config2});
7380
ensure_throttle_processes_started(
7481
Name, #{rate := Rate, interval := Interval, parallelism := NoOfProcesses} = Config)
7582
when is_atom(Name), ?POS_INT(Rate), ?NONNEG_INT(Interval), ?POS_INT(NoOfProcesses) ->

test/throttle_SUITE.erl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ groups() ->
1919
[
2020
start,
2121
start_descriptive,
22+
start_interarrival,
2223
rate_zero_is_not_accepted,
2324
low_rate_gets_remapped,
2425
low_interval_get_remapped,
@@ -77,6 +78,17 @@ start_descriptive(_) ->
7778
Description = #{rate => 100, interval => 5000, parallelism => 12},
7879
?assertMatch({ok, started}, amoc_throttle:start(?FUNCTION_NAME, Description)).
7980

81+
start_interarrival(_) ->
82+
%% Starts successfully
83+
Description = #{interarrival => 50, parallelism => 1},
84+
?assertMatch({ok, started}, amoc_throttle:start(?FUNCTION_NAME, Description)),
85+
State = get_state_of_one_process(?FUNCTION_NAME),
86+
?assertMatch(#{name := ?FUNCTION_NAME,
87+
interval := 60000,
88+
delay_between_executions := 50,
89+
n := 1200},
90+
State).
91+
8092
rate_zero_is_not_accepted(_) ->
8193
?assertMatch({error, invalid_throttle}, amoc_throttle:start(?FUNCTION_NAME, 0, 100, 1)).
8294

0 commit comments

Comments
 (0)