Using socket module in production?
pablo platt
pablo.platt@REDACTED
Fri Jun 12 12:48:32 CEST 2020
Hi,
Is it safe to use the NIF socket module for UDP in production in OTP 23?
It's working in my tests but I'm not sure if it's considered unstable or if
I'm missing configuration or error handling.
This is how I'm using it:
open() ->
{ok, Socket} = socket:open(inet, dgram, udp),
{ok, Port} = socket:bind(Socket, any),
{Socket, Port}.
active_once(Socket) ->
case socket:recvfrom(Socket, 0, [], nowait) of
{ok, {#{port := Port, addr := Addr}, Data}} ->
{udp, Addr, Port, Data};
{select, {select_info, _SelectTag, SelectRef}} ->
SelectRef;
{error, Reason} ->
exit(Reason),
error
end.
send(Socket, Addr, Port, Data) ->
Dest = #{family => inet,
port => Port,
addr => Addr},
case socket:sendto(Socket, Data, Dest, [], nowait) of
ok -> ok;
{select, {select_info, _SelectTag, _SelectRef}} ->
% need to wait for {'$socket', Socket, select, SelectRef} and
resend Data?
ok
end.
close(Socket) ->
socket:close(Socket).
% listen for select ref
handle_info({'$socket', Socket, select, SelectRef}, _State) ->
case socket:recvfrom(Socket, 0, [], nowait) of
{ok, {#{port := Port, addr := Addr}, Data}} ->
% handle Data
ok;
{select, {select_info, _SelectTag, SelectRef}} ->
SelectRef;
{error, Reason} ->
exit(Reason),
error
end,
{noreply, State}.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200612/6669808d/attachment.htm>
More information about the erlang-questions
mailing list