25
25
-define (DEFAULT_STEP_SIZE , 1 ).
26
26
-define (DEFAULT_INTERVAL , 60000 ). % % one minute
27
27
-define (DEFAULT_NO_PROCESSES , 10 ).
28
+ -define (TIMEOUT (N ), (infinity =:= N orelse is_integer (N ) andalso N >= 0 )).
28
29
-define (NONNEG_INT (N ), (is_integer (N ) andalso N >= 0 )).
29
30
-define (POS_INT (N ), (is_integer (N ) andalso N > 0 )).
30
31
51
52
-type config () :: #{rate := amoc_throttle :rate (),
52
53
interval := amoc_throttle :interval (),
53
54
parallelism := non_neg_integer ()}.
54
- -type gradual_rate_change () :: #{from_rate := amoc_throttle : rate (),
55
- to_rate := amoc_throttle : rate (),
55
+ -type gradual_rate_change () :: #{from_rate := non_neg_integer (),
56
+ to_rate := non_neg_integer (),
56
57
interval := amoc_throttle :interval (),
57
58
step_interval := pos_integer (),
58
59
step_size := pos_integer (),
@@ -71,26 +72,32 @@ start_link() ->
71
72
{ok , started | already_started } |
72
73
{error , invalid_throttle | wrong_reconfiguration | wrong_no_of_procs }.
73
74
ensure_throttle_processes_started (
74
- Name , #{interarrival := EveryMs } = Config )
75
- when is_atom (Name ), ? NONNEG_INT ( EveryMs ) ->
75
+ Name , #{interarrival := Interarrival } = Config )
76
+ when is_atom (Name ), ? TIMEOUT ( Interarrival ) ->
76
77
raise_event_on_slave_node (Name , init ),
77
- Config1 = #{rate => ? DEFAULT_INTERVAL div EveryMs , interval => ? DEFAULT_INTERVAL },
78
+ Config1 = #{rate => ? DEFAULT_INTERVAL div Interarrival , interval => ? DEFAULT_INTERVAL },
78
79
Config2 = Config1 #{parallelism => maps :get (parallelism , Config , ? DEFAULT_NO_PROCESSES )},
79
80
gen_server :call (? MASTER_SERVER , {start_processes , Name , Config2 });
80
81
ensure_throttle_processes_started (
81
82
Name , #{rate := Rate , interval := Interval , parallelism := NoOfProcesses } = Config )
82
- when is_atom (Name ), ? POS_INT (Rate ), ? NONNEG_INT (Interval ), ? POS_INT (NoOfProcesses ) ->
83
+ when is_atom (Name ), ? TIMEOUT (Rate ), ? NONNEG_INT (Interval ), ? POS_INT (NoOfProcesses ) ->
83
84
raise_event_on_slave_node (Name , init ),
84
85
gen_server :call (? MASTER_SERVER , {start_processes , Name , Config });
85
86
ensure_throttle_processes_started (
86
87
Name , #{rate := Rate , interval := Interval } = Config )
87
- when is_atom (Name ), ? POS_INT (Rate ), ? NONNEG_INT (Interval ) ->
88
+ when is_atom (Name ), ? TIMEOUT (Rate ), ? NONNEG_INT (Interval ) ->
88
89
raise_event_on_slave_node (Name , init ),
89
90
Config1 = Config #{parallelism => ? DEFAULT_NO_PROCESSES },
90
91
gen_server :call (? MASTER_SERVER , {start_processes , Name , Config1 });
92
+ ensure_throttle_processes_started (
93
+ Name , #{rate := Rate , parallelism := NoOfProcesses } = Config )
94
+ when is_atom (Name ), ? TIMEOUT (Rate ), ? POS_INT (NoOfProcesses ) ->
95
+ raise_event_on_slave_node (Name , init ),
96
+ Config1 = Config #{interval => ? DEFAULT_INTERVAL },
97
+ gen_server :call (? MASTER_SERVER , {start_processes , Name , Config1 });
91
98
ensure_throttle_processes_started (
92
99
Name , #{rate := Rate } = Config )
93
- when is_atom (Name ), ? POS_INT (Rate ) ->
100
+ when is_atom (Name ), ? TIMEOUT (Rate ) ->
94
101
raise_event_on_slave_node (Name , init ),
95
102
Config1 = Config #{interval => ? DEFAULT_INTERVAL , parallelism => ? DEFAULT_NO_PROCESSES },
96
103
gen_server :call (? MASTER_SERVER , {start_processes , Name , Config1 });
@@ -147,7 +154,7 @@ init([]) ->
147
154
From :: {pid (), Tag :: term ()}, state ()) ->
148
155
{reply , {ok , started | already_started }, state ()} |
149
156
{reply , {error , wrong_reconfiguration | wrong_no_of_procs }, state ()};
150
- ({pause | resume | stop }, From :: {pid (), Tag :: term ()}, state ()) ->
157
+ ({pause | resume | unlock | stop }, From :: {pid (), Tag :: term ()}, state ()) ->
151
158
{reply , ok , state ()} |
152
159
{reply , Error :: any (), state ()};
153
160
({change_rate , name (), amoc_throttle :rate (), amoc_throttle :interval ()},
@@ -175,8 +182,8 @@ handle_call({pause, Name}, _From, State) ->
175
182
Error ->
176
183
{reply , Error , State }
177
184
end ;
178
- handle_call ({resume , Name }, _From , State ) ->
179
- case run_in_all_processes (Name , resume ) of
185
+ handle_call ({Op , Name }, _From , State ) when unlock =:= Op ; resume =:= Op ->
186
+ case run_in_all_processes (Name , Op ) of
180
187
ok ->
181
188
Info = maps :get (Name , State ),
182
189
{reply , ok , State #{Name => Info # throttle_info {active = true }}};
@@ -266,6 +273,7 @@ continue_plan(Name, State, Info, Plan) ->
266
273
State #{Name => Info # throttle_info {rate = NewRate , change_plan = NewPlan }}.
267
274
268
275
-spec rate_per_minute (amoc_throttle :rate (), amoc_throttle :interval ()) -> amoc_throttle :rate ().
276
+ rate_per_minute (infinity , _ ) -> infinity ;
269
277
rate_per_minute (_ , 0 ) -> 0 ;
270
278
rate_per_minute (Rate , Interval ) ->
271
279
(Rate * 60000 ) div Interval .
@@ -275,7 +283,7 @@ start_processes(Name, #{rate := Rate, interval := Interval, parallelism := NoOfP
275
283
raise_event (Name , init ),
276
284
RatePerMinute = rate_per_minute (Rate , Interval ),
277
285
report_rate (Name , RatePerMinute ),
278
- RealNoOfProcs = min (Rate , NoOfProcesses ),
286
+ RealNoOfProcs = expected_no_of_processes (Rate , NoOfProcesses ),
279
287
start_throttle_processes (Name , Interval , Rate , RealNoOfProcs ),
280
288
# throttle_info {rate = Rate , interval = Interval , active = true , no_of_procs = RealNoOfProcs }.
281
289
@@ -334,7 +342,7 @@ run_in_all_processes(Name, Cmd) ->
334
342
335
343
verify_new_start_matches_running (Name , Config , Group , State ) ->
336
344
#{rate := Rate , interval := Interval , parallelism := NoOfProcesses } = Config ,
337
- ExpectedNoOfProcesses = min (Rate , NoOfProcesses ),
345
+ ExpectedNoOfProcesses = expected_no_of_processes (Rate , NoOfProcesses ),
338
346
case {length (Group ), State } of
339
347
{ExpectedNoOfProcesses , #{Name := # throttle_info {rate = Rate , interval = Interval }}} ->
340
348
{reply , {ok , already_started }, State };
@@ -344,6 +352,11 @@ verify_new_start_matches_running(Name, Config, Group, State) ->
344
352
{reply , {error , wrong_no_of_procs }, State }
345
353
end .
346
354
355
+ expected_no_of_processes (0 , NoOfProcesses ) ->
356
+ min (1 , NoOfProcesses );
357
+ expected_no_of_processes (Rate , NoOfProcesses ) ->
358
+ min (Rate , NoOfProcesses ).
359
+
347
360
run_cmd (Pid , stop ) ->
348
361
amoc_throttle_process :stop (Pid );
349
362
run_cmd (Pid , pause ) ->
0 commit comments