Skip to content

Commit 880ef48

Browse files
committed
socket_dist: add listen/2 with opts map
Add listen/2 accepting a generic options map instead of a raw port number, making the interface transport-agnostic per dist module convention. Port range iteration (dist_listen_min/dist_listen_max) is now handled inside socket_dist rather than net_kernel. Signed-off-by: Peter M <petermm@gmail.com>
1 parent 4545b7d commit 880ef48

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

libs/estdlib/src/net_kernel.erl

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,14 @@ init(Options) ->
211211
process_flag(trap_exit, true),
212212
LongNames = maps:get(name_domain, Options, longnames) =:= longnames,
213213
ProtoDist = maps:get(proto_dist, Options, socket_dist),
214-
DistPortMin = maps:get(dist_listen_min, Options, 0),
215-
DistPortMax = maps:get(dist_listen_max, Options, 0),
214+
ListenOpts = maps:with([dist_listen_min, dist_listen_max], Options),
216215
Name = maps:get(name, Options),
217216
Node = maps:get(node, Options),
218217
Cookie = crypto:strong_rand_bytes(16),
219218
TickInterval = (?NET_TICK_TIME * 1000) div ?NET_TICK_INTENSITY,
220219
Self = self(),
221220
Ticker = spawn_link(fun() -> ticker(Self, TickInterval) end),
222-
% Try ports in range until one succeeds
223-
case try_listen_ports(ProtoDist, Name, DistPortMin, DistPortMax) of
221+
case ProtoDist:listen(Name, ListenOpts) of
224222
{ok, {Listen, _Address, Creation}} ->
225223
true = erlang:setnode(Node, Creation),
226224
AcceptPid = ProtoDist:accept(Listen),
@@ -239,18 +237,6 @@ init(Options) ->
239237
{stop, Reason}
240238
end.
241239

242-
%% @hidden
243-
try_listen_ports(ProtoDist, Name, DistPortMin, DistPortMax) ->
244-
try_listen_ports(ProtoDist, Name, DistPortMin, DistPortMax, DistPortMin).
245-
246-
try_listen_ports(_ProtoDist, _Name, _DistPortMin, DistPortMax, Port) when Port > DistPortMax ->
247-
{error, no_port_available};
248-
try_listen_ports(ProtoDist, Name, DistPortMin, DistPortMax, Port) ->
249-
case ProtoDist:listen(Name, Port) of
250-
{ok, _} = Success -> Success;
251-
{error, _} -> try_listen_ports(ProtoDist, Name, DistPortMin, DistPortMax, Port + 1)
252-
end.
253-
254240
%% @hidden
255241
handle_call(get_state, _From, #state{longnames = Longnames} = State) ->
256242
NameDomain =

libs/estdlib/src/socket_dist.erl

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,30 @@
3838

3939
-spec listen(string()) -> {ok, {any(), #net_address{}, pos_integer()}} | {error, any()}.
4040
listen(Name) ->
41-
listen(Name, 0).
41+
listen(Name, #{}).
4242

43-
-spec listen(string(), non_neg_integer()) ->
43+
-spec listen(string(), map()) ->
4444
{ok, {any(), #net_address{}, pos_integer()}} | {error, any()}.
45-
listen(Name, SocketPort) ->
45+
listen(Name, Opts) ->
46+
PortMin = maps:get(dist_listen_min, Opts, 0),
47+
PortMax = maps:get(dist_listen_max, Opts, 0),
48+
try_listen_port(Name, PortMin, PortMax).
49+
50+
try_listen_port(_Name, Port, PortMax) when Port > PortMax ->
51+
{error, no_port_available};
52+
try_listen_port(Name, Port, PortMax) ->
53+
case do_listen(Name, Port) of
54+
{ok, _} = Ok -> Ok;
55+
{error, _} when Port < PortMax -> try_listen_port(Name, Port + 1, PortMax);
56+
{error, _} = Error -> Error
57+
end.
58+
59+
do_listen(Name, SocketPort) ->
4660
case socket:open(inet, stream, tcp) of
4761
{ok, LSock} ->
48-
try do_listen(Name, LSock, SocketPort) of
49-
{ok, _} = Ok ->
50-
Ok;
51-
Error ->
52-
socket:close(LSock),
53-
Error
62+
try do_listen_bind(Name, LSock, SocketPort) of
63+
{ok, _} = Ok -> Ok;
64+
Error -> socket:close(LSock), Error
5465
catch
5566
Class:Reason:Stack ->
5667
socket:close(LSock),
@@ -60,7 +71,7 @@ listen(Name, SocketPort) ->
6071
Error
6172
end.
6273

63-
do_listen(Name, LSock, SocketPort) ->
74+
do_listen_bind(Name, LSock, SocketPort) ->
6475
case socket:bind(LSock, #{family => inet, port => SocketPort, addr => {0, 0, 0, 0}}) of
6576
ok -> do_listen_bound(Name, LSock);
6677
{error, _} = Error -> Error

0 commit comments

Comments
 (0)