[erlang-questions] gen_statem and multiple timeout vs 1

Vans S vans_163@REDACTED
Sun Sep 25 19:32:19 CEST 2016


Learning the new gen_statem made me desire for one extra feature.

Say you have a common use case of a webserver /w websockets, you have a general connection timeout of 120s, if no messages are received in 120s it means the socket is in an unknown state, and should be closed.

So you write your returns like this anytime the client sends you a message:

{next_state, NextState, NewData, {timeout, 120*1000, idle_timeout}}

Now if the client does not send any messages in 120 seconds, we will get a idle_timeout message sent to the gen_statem process.

Awesome.

But enter a little complexity, enter websockets.

Now we need to send a ping from the gen_statem every 15s to the client, but we also need to consider if we did not get any messages from the client in 120s, we are in unknown state and should terminate the connection.

So now we are just itching to do this on init:

{ok, initial_state, Data, [        {timeout, 120*1000, idle_timeout},        {timeout, 15*1000, websocket_ping}
    ]}

This way we do not need to manage our own timers using erlang:send_after.  timer module is not even a consideration due to how inefficient it is at scaling.

But of course we cannot do this, the latest timeout will always override any previous.

What do you think?




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160925/3ad22263/attachment.htm>


More information about the erlang-questions mailing list