aboutsummaryrefslogtreecommitdiffstats
path: root/src/ranch_tcp.erl
diff options
context:
space:
mode:
authorjuhlig <[email protected]>2020-04-20 17:03:17 +0200
committerjuhlig <[email protected]>2020-04-20 17:03:17 +0200
commit9765f305e1f55f758a683bd95665fd0ab84a52c3 (patch)
treea45cf397516b3e59f7a8065e7f76bba46a64e3d7 /src/ranch_tcp.erl
parent821937cea1a9afacd2dc2448440ad6f64d3a29f1 (diff)
downloadranch-9765f305e1f55f758a683bd95665fd0ab84a52c3.tar.gz
ranch-9765f305e1f55f758a683bd95665fd0ab84a52c3.tar.bz2
ranch-9765f305e1f55f758a683bd95665fd0ab84a52c3.zip
Delete local socket file when a listener closes
Diffstat (limited to 'src/ranch_tcp.erl')
-rw-r--r--src/ranch_tcp.erl23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/ranch_tcp.erl b/src/ranch_tcp.erl
index 4abd088..28d221c 100644
--- a/src/ranch_tcp.erl
+++ b/src/ranch_tcp.erl
@@ -43,6 +43,7 @@
-export([sockname/1]).
-export([shutdown/2]).
-export([close/1]).
+-export([cleanup/1]).
-type opt() :: {backlog, non_neg_integer()}
| {buffer, non_neg_integer()}
@@ -86,21 +87,13 @@ messages() -> {tcp, tcp_closed, tcp_error, tcp_passive}.
-spec listen(ranch:transport_opts(opts())) -> {ok, inet:socket()} | {error, atom()}.
listen(TransOpts) ->
+ ok = cleanup(TransOpts),
Logger = maps:get(logger, TransOpts, logger),
SocketOpts0 = maps:get(socket_opts, TransOpts, []),
SocketOpts1 = ranch:set_option_default(SocketOpts0, backlog, 1024),
SocketOpts2 = ranch:set_option_default(SocketOpts1, nodelay, true),
SocketOpts3 = ranch:set_option_default(SocketOpts2, send_timeout, 30000),
SocketOpts4 = ranch:set_option_default(SocketOpts3, send_timeout_close, true),
- %% In case of a local socket, we remove the socket file first.
- %% It is possible to have multiple ip tuples in the socket options,
- %% and the last one will be used (undocumented).
- _ = case lists:keyfind(ip, 1, lists:reverse(SocketOpts0)) of
- {ip, {local, SockFile}} ->
- file:delete(SockFile);
- _ ->
- ok
- end,
%% We set the port to 0 because it is given in the Opts directly.
%% The port in the options takes precedence over the one in the
%% first argument.
@@ -271,3 +264,15 @@ shutdown(Socket, How) ->
-spec close(inet:socket()) -> ok.
close(Socket) ->
gen_tcp:close(Socket).
+
+-spec cleanup(ranch:transport_opts(opts())) -> ok.
+cleanup(#{socket_opts:=SocketOpts}) ->
+ case lists:keyfind(ip, 1, lists:reverse(SocketOpts)) of
+ {ip, {local, SockFile}} ->
+ _ = file:delete(SockFile),
+ ok;
+ _ ->
+ ok
+ end;
+cleanup(_) ->
+ ok.