Skip to content

Commit 4c9c16f

Browse files
committed
dist_opts in net_kernel
Signed-off-by: Peter M <petermm@gmail.com>
1 parent 68731e5 commit 4c9c16f

File tree

2 files changed

+9
-24
lines changed

2 files changed

+9
-24
lines changed

libs/estdlib/src/net_kernel.erl

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@
7373
%% @param Options options for distribution. Supported options are:
7474
%% - `name_domain' : whether name should be short or long
7575
%% - `proto_dist' : the module used for distribution (e.g. `socket_dist')
76-
%% - `dist_listen_min' : defines the min port range for the listener socket.
77-
%% - `dist_listen_max' : defines the max port range for the listener socket.
76+
%% - `dist_opts' : a map of options passed through to the dist module's `listen/2'
7877
%%-----------------------------------------------------------------------------
7978
-spec start(atom(), map()) -> {ok, pid()}.
8079
start(Name, Options0) when is_atom(Name) andalso is_map(Options0) ->
@@ -83,31 +82,12 @@ start(Name, Options0) when is_atom(Name) andalso is_map(Options0) ->
8382
case Key of
8483
name_domain when Val =:= shortnames orelse Val =:= longnames -> ok;
8584
proto_dist when is_atom(Val) -> ok;
86-
dist_listen_min when is_integer(Val) -> ok;
87-
dist_listen_max when is_integer(Val) -> ok;
85+
dist_opts when is_map(Val) -> ok;
8886
_ -> error({invalid_option, Key, Val}, [Name, Options0])
8987
end
9088
end,
9189
Options0
9290
),
93-
% Check that if one of dist_listen_min and dist_listen_max are configured, both are configured.
94-
% And verify dist_listen_max is larger or equal to dist_listen_min.
95-
ok =
96-
case {maps:is_key(dist_listen_min, Options0), maps:is_key(dist_listen_max, Options0)} of
97-
{true, false} ->
98-
error(missing_dist_listen_max, [Name, Options0]);
99-
{false, true} ->
100-
error(missing_dist_listen_min, [Name, Options0]);
101-
{true, true} ->
102-
Min = maps:get(dist_listen_min, Options0),
103-
Max = maps:get(dist_listen_max, Options0),
104-
if
105-
Min > Max -> error(invalid_port_range, [Name, Options0]);
106-
true -> ok
107-
end;
108-
_ ->
109-
ok
110-
end,
11191
Options1 = Options0#{name => Name},
11292
Options2 = split_name(Options1),
11393
net_kernel_sup:start(Options2);
@@ -211,14 +191,14 @@ init(Options) ->
211191
process_flag(trap_exit, true),
212192
LongNames = maps:get(name_domain, Options, longnames) =:= longnames,
213193
ProtoDist = maps:get(proto_dist, Options, socket_dist),
214-
ListenOpts = maps:with([dist_listen_min, dist_listen_max], Options),
194+
DistOpts = maps:get(dist_opts, Options, #{}),
215195
Name = maps:get(name, Options),
216196
Node = maps:get(node, Options),
217197
Cookie = crypto:strong_rand_bytes(16),
218198
TickInterval = (?NET_TICK_TIME * 1000) div ?NET_TICK_INTENSITY,
219199
Self = self(),
220200
Ticker = spawn_link(fun() -> ticker(Self, TickInterval) end),
221-
case ProtoDist:listen(Name, ListenOpts) of
201+
case ProtoDist:listen(Name, DistOpts) of
222202
{ok, {Listen, _Address, Creation}} ->
223203
true = erlang:setnode(Node, Creation),
224204
AcceptPid = ProtoDist:accept(Listen),

libs/estdlib/src/socket_dist.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ listen(Name) ->
4545
listen(Name, Opts) ->
4646
PortMin = maps:get(dist_listen_min, Opts, 0),
4747
PortMax = maps:get(dist_listen_max, Opts, 0),
48+
validate_port_range(PortMin, PortMax),
4849
try_listen_port(Name, PortMin, PortMax).
4950

51+
validate_port_range(0, 0) -> ok;
52+
validate_port_range(Min, Max) when is_integer(Min), is_integer(Max), Min =< Max -> ok;
53+
validate_port_range(Min, Max) -> error({invalid_port_range, Min, Max}).
54+
5055
try_listen_port(_Name, Port, PortMax) when Port > PortMax ->
5156
{error, no_port_available};
5257
try_listen_port(Name, Port, PortMax) ->

0 commit comments

Comments
 (0)