Using socket module in production?

Raimo Niskanen raimo+erlang-questions@REDACTED
Fri Jun 12 15:00:20 CEST 2020


Hi Pablo!

We will still make backwards incompatible changes.  But we do not know of
any stability issues, and welcome all that stress test the code.

So your code might stop working work in a future 23.X release.

/ Raimo


On Fri, Jun 12, 2020 at 01:48:32PM +0300, pablo platt wrote:
> 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}.

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list