From 84bfb08e8de480530d656b51647061920130d1e7 Mon Sep 17 00:00:00 2001 From: Nelson Vides Date: Fri, 14 Jun 2024 11:25:38 +0200 Subject: [PATCH] Introduce every_ms api to amoc throttle --- src/throttle/amoc_throttle.erl | 6 +++++- src/throttle/amoc_throttle_controller.erl | 7 +++++++ test/throttle_SUITE.erl | 12 ++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/throttle/amoc_throttle.erl b/src/throttle/amoc_throttle.erl index 9dacd81a..b54d4643 100644 --- a/src/throttle/amoc_throttle.erl +++ b/src/throttle/amoc_throttle.erl @@ -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(), diff --git a/src/throttle/amoc_throttle_controller.erl b/src/throttle/amoc_throttle_controller.erl index 50901aca..08144f55 100644 --- a/src/throttle/amoc_throttle_controller.erl +++ b/src/throttle/amoc_throttle_controller.erl @@ -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) -> diff --git a/test/throttle_SUITE.erl b/test/throttle_SUITE.erl index 9f209b62..f72026ec 100644 --- a/test/throttle_SUITE.erl +++ b/test/throttle_SUITE.erl @@ -19,6 +19,7 @@ groups() -> [ start, start_descriptive, + start_every, rate_zero_is_not_accepted, low_rate_gets_remapped, low_interval_get_remapped, @@ -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)).