Skip to content

Commit 28336c0

Browse files
committed
PoC for dist min max
Needs handling of port collision and probably use random instead of sequential.
1 parent be62d3a commit 28336c0

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

libs/estdlib/src/net_kernel.erl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
-define(SETUPTIME, 7000).
6565
-define(NET_TICK_INTENSITY, 4).
6666
-define(NET_TICK_TIME, 60).
67+
% Define distribution port range
68+
-define(DIST_PORT_MIN, 5001).
69+
-define(DIST_PORT_MAX, 5003).
6770

6871
%%-----------------------------------------------------------------------------
6972
%% @doc Start erlang distribution
@@ -179,7 +182,9 @@ init(Options) ->
179182
TickInterval = (?NET_TICK_TIME * 1000) div ?NET_TICK_INTENSITY,
180183
Self = self(),
181184
Ticker = spawn_link(fun() -> ticker(Self, TickInterval) end),
182-
case ProtoDist:listen(Name) of
185+
%- case ProtoDist:listen(Name) of
186+
% Try ports in range until one succeeds
187+
case try_listen_ports(ProtoDist, Name) of
183188
{ok, {Listen, _Address, Creation}} ->
184189
true = erlang:setnode(Node, Creation),
185190
AcceptPid = ProtoDist:accept(Listen),
@@ -198,6 +203,18 @@ init(Options) ->
198203
{stop, Reason}
199204
end.
200205

206+
% Add new function to try ports in range
207+
try_listen_ports(ProtoDist, Name) ->
208+
try_listen_ports(ProtoDist, Name, ?DIST_PORT_MIN).
209+
210+
try_listen_ports(_ProtoDist, _Name, Port) when Port > ?DIST_PORT_MAX ->
211+
{error, no_port_available};
212+
try_listen_ports(ProtoDist, Name, Port) ->
213+
case ProtoDist:listen(Name, Port) of
214+
{ok, _} = Success -> Success;
215+
{error, _} -> try_listen_ports(ProtoDist, Name, Port + 1)
216+
end.
217+
201218
%% @hidden
202219
handle_call(get_state, _From, #state{longnames = Longnames} = State) ->
203220
NameDomain =

libs/estdlib/src/socket_dist.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
% dist interface
2323
-export([
2424
listen/1,
25+
% Add new export
26+
listen/2,
2527
accept/1,
2628
accept_connection/5,
2729
setup/5,
@@ -37,10 +39,15 @@
3739

3840
-spec listen(string()) -> {ok, {any(), #net_address{}, pos_integer()}} | {error, any()}.
3941
listen(Name) ->
42+
listen(Name, 0).
43+
44+
-spec listen(string(), non_neg_integer()) ->
45+
{ok, {any(), #net_address{}, pos_integer()}} | {error, any()}.
46+
listen(Name, Port) ->
4047
{ok, LSock} = socket:open(inet, stream, tcp),
4148
ok = socket:bind(LSock, #{
4249
family => inet,
43-
port => 0,
50+
port => Port,
4451
addr => {0, 0, 0, 0}
4552
}),
4653
ok = socket:listen(LSock),

0 commit comments

Comments
 (0)