26
1
mirror of https://github.com/processone/ejabberd.git synced 2025-01-03 18:02:28 +01:00

Don't leak with UDP sockets

This commit is contained in:
Evgeniy Khramtsov 2017-05-22 11:29:53 +03:00
parent 3a96d72a7f
commit 504860f065

View File

@ -41,6 +41,7 @@
remove_user/2, register_user/2]). remove_user/2, register_user/2]).
-define(SOCKET_NAME, mod_metrics_udp_socket). -define(SOCKET_NAME, mod_metrics_udp_socket).
-define(SOCKET_REGISTER_RETRIES, 10).
%%==================================================================== %%====================================================================
%% API %% API
@ -139,7 +140,7 @@ send_metrics(Host, Probe, Peer, Port) ->
[Node|_] = binary:split(FQDN, <<".">>), [Node|_] = binary:split(FQDN, <<".">>),
BaseId = <<Host/binary, "/", Node/binary, ".">>, BaseId = <<Host/binary, "/", Node/binary, ".">>,
TS = integer_to_binary(p1_time_compat:system_time(seconds)), TS = integer_to_binary(p1_time_compat:system_time(seconds)),
case get_socket() of case get_socket(?SOCKET_REGISTER_RETRIES) of
{ok, Socket} -> {ok, Socket} ->
case Probe of case Probe of
{Key, Val} -> {Key, Val} ->
@ -156,15 +157,16 @@ send_metrics(Host, Probe, Peer, Port) ->
Err Err
end. end.
get_socket() -> get_socket(N) ->
case whereis(?SOCKET_NAME) of case whereis(?SOCKET_NAME) of
undefined -> undefined ->
case gen_udp:open(0) of case gen_udp:open(0) of
{ok, Socket} -> {ok, Socket} ->
try register(?SOCKET_NAME, Socket) of try register(?SOCKET_NAME, Socket) of
true -> {ok, Socket} true -> {ok, Socket}
catch _:badarg -> catch _:badarg when N > 1 ->
{ok, Socket} gen_udp:close(Socket),
get_socket(N-1)
end; end;
{error, Reason} = Err -> {error, Reason} = Err ->
?ERROR_MSG("can not open udp socket to grapherl: ~s", ?ERROR_MSG("can not open udp socket to grapherl: ~s",