-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure throttlers are supervised and pooled
- Loading branch information
1 parent
fcbdeda
commit 9613c17
Showing
6 changed files
with
158 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
%% @private | ||
%% @see amoc_throttle | ||
%% @copyright 2023 Erlang Solutions Ltd. | ||
-module(amoc_throttle_pool). | ||
|
||
-behaviour(supervisor). | ||
|
||
-export([start_process_pool/4]). | ||
-export([start_link/4, init/1]). | ||
|
||
-spec start_process_pool( | ||
amoc_throttle:name(), | ||
amoc_throttle:interval(), | ||
amoc_throttle:rate(), | ||
pos_integer() | ||
) -> {ok, [Pid :: pid()]}. | ||
start_process_pool(Name, Interval, Rate, NoOfProcesses) -> | ||
{ok, Sup} = supervisor:start_child(amoc_throttle_sup, [Name, Interval, Rate, NoOfProcesses]), | ||
Children = supervisor:which_children(Sup), | ||
{ok, [ Pid || {_, Pid, _, _} <- Children ]}. | ||
|
||
-spec start_link( | ||
amoc_throttle:name(), | ||
amoc_throttle:interval(), | ||
amoc_throttle:rate(), | ||
pos_integer() | ||
) -> {ok, Pid :: pid()}. | ||
start_link(Name, Interval, Rate, NoOfProcesses) -> | ||
supervisor:start_link({local, ?MODULE}, ?MODULE, {Name, Interval, Rate, NoOfProcesses}). | ||
|
||
-spec init(term()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}. | ||
init({Name, Interval, Rate, NoOfProcesses}) -> | ||
Children = [ | ||
#{id => amoc_throttle_process, | ||
start => {amoc_throttle_process, start_link, [Name, Interval, RatePerProcess]}, | ||
type => worker, | ||
shutdown => timer:seconds(5), | ||
restart => permanent, | ||
modules => [amoc_throttle_process] | ||
} | ||
|| RatePerProcess <- calculate_rate_per_process(Rate, NoOfProcesses) | ||
], | ||
|
||
SupFlags = #{strategy => one_for_one}, | ||
{ok, {SupFlags, Children}}. | ||
|
||
%% Helpers | ||
calculate_rate_per_process(Rate, NoOfProcesses) -> | ||
calculate_rate_per_process([], Rate, NoOfProcesses). | ||
|
||
calculate_rate_per_process(Acc, Rate, 1) -> | ||
[Rate | Acc]; | ||
calculate_rate_per_process(Acc, Rate, N) when is_integer(N), N > 1 -> | ||
ProcessRate = Rate div N, | ||
calculate_rate_per_process([ProcessRate | Acc], Rate - ProcessRate, N - 1). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
%% @private | ||
%% @see amoc_throttle | ||
%% @copyright 2023 Erlang Solutions Ltd. | ||
-module(amoc_throttle_pooler). | ||
|
||
-behaviour(supervisor). | ||
|
||
-export([start_link/0, init/1]). | ||
|
||
-spec start_link() -> {ok, Pid :: pid()}. | ||
start_link() -> | ||
supervisor:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
-spec init(term()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}. | ||
init([]) -> | ||
ChildSpec = #{id => amoc_throttle_pool, | ||
start => {amoc_throttle_pool, start_link, []}, | ||
type => supervisor, | ||
shutdown => infinity, | ||
restart => permanent, | ||
modules => [amoc_throttle_pool] }, | ||
SupFlags = #{strategy => simple_one_for_one}, | ||
{ok, {SupFlags, [ChildSpec]}}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
%% @private | ||
%% @see amoc_throttle | ||
%% @copyright 2023 Erlang Solutions Ltd. | ||
%% @doc Top supervisor for the controller, the pooler, and the process group | ||
%% | ||
%% The supervision tree is as follows | ||
%% | ||
%% amoc_sup | ||
%% | | ||
%% amoc_throttle_sup | ||
%% / | \ | ||
%% / | \ | ||
%% / | \ | ||
%% pooler controller pg | ||
%% || | ||
%% (dynamically) | ||
%% || | ||
%% pool | ||
%% / | \ | ||
%% [processes()] | ||
%% | ||
%% Where the pool, on creation, subscribes all its children to the named process group | ||
%% @end | ||
-module(amoc_throttle_sup). | ||
|
||
-behaviour(supervisor). | ||
|
||
-define(PG_SCOPE, amoc_throttle). | ||
|
||
-export([start_link/0, init/1]). | ||
|
||
-spec start_link() -> {ok, Pid :: pid()}. | ||
start_link() -> | ||
supervisor:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
-spec init(term()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}. | ||
init([]) -> | ||
SupFlags = #{strategy => one_for_one}, | ||
Pooler = #{id => amoc_throttle_pooler, | ||
start => {amoc_throttle_pooler, start_link, []}, | ||
type => supervisor, | ||
shutdown => infinity, | ||
restart => permanent, | ||
modules => [amoc_throttle_pooler]}, | ||
Controller = #{id => amoc_throttle_controller, | ||
start => {amoc_throttle_controller, start_link, []}, | ||
type => worker, | ||
shutdown => timer:seconds(5), | ||
restart => permanent, | ||
modules => [amoc_throttle_controller]}, | ||
Pg = #{id => pg, | ||
start => {pg, start_link, [?PG_SCOPE]}, | ||
type => worker, | ||
shutdown => timer:seconds(5), | ||
restart => permanent, | ||
modules => [pg]}, | ||
{ok, {SupFlags, [Pooler, Controller, Pg]}}. |