<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px">Okay going to take another attempt at it. In evented mode.<br><br>Some points before we get into it. gen_statem fits here because it ensures 2 key points.<br><br>#1 If we get any message while in waiting_socket state, we should not process it / crash / drop it.<br>#2 We need to transition out of waiting_socket state, before processing future messages.<br><br><div id="yui_3_16_0_ym19_1_1475720696802_51953">This code might be off as I did not test it, so treat as pseudo code.</div><div id="yui_3_16_0_ym19_1_1475720696802_51953"><br></div><div id="yui_3_16_0_ym19_1_1475720696802_51953"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_51876">init() -></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr"><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_52671"> {ok, waiting_socket, #{}, </span></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr"><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_70285"> [</span><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_52672">{state_timeout, 2000, no_socket_passed}</span><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_52673">]</span></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr"><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;">}.</span></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr"><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;"><br></span></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr"><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;"><br></span></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr">%% Timeouts</div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr">% Crash here or handle ourselves</div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54205"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_54206"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_54207">%handle_event(state_timeout, no_socket_passed, waiting_socket, D) -> </span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54208"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_54209"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_54210">% {stop, {shutdown, tcp_closed}, D};</span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54208"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif"><span style="font-size: 13px;"><br></span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54208"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_70128"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_70127">% Crash here or handle, we should NEVER get any message here</span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_70044"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_70045"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_70046">%handle_event(_, _, waiting_socket, D) -> </span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_70047"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_70048"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_70049">% {stop, {shutdown, tcp_closed}, D};</span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54208"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54218"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_54219"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_54220">handle_event(named_timeout, endpoint_timeout, _, D) -> </span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54221"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_54222"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_54223"> {stop, {shutdown, tcp_closed}, D};</span></font></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr"><br></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr"><br></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr">%% Basic socket ops</div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_52977"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_52978"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_52979">handle_event(info, {pass_socket, Socket}, waiting_socket, D) -> </span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54302"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_53298"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_53299"> {next_state, established, D#{socket=> Socket}, [{named_timeout, 15000, keep_alive}, </span></font><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_53300">{named_timeout, 120000, endpoint_timeout}</span><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_54303">]};<br><br></span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54304"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_54305"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_54306">handle_event(named_timeout, keep_alive, S, D) -> </span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54307"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_54308"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_54309"> send_keepalive(),</span></font></div><div dir="ltr"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;"></span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54310"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_54311"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_54312"> {next_state, S, D, [{named_timeout, 15000, keep_alive}]};</span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54313"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54313"><div dir="ltr" style="margin-top: 0.1em; margin-bottom: 0.1em;" id="yui_3_16_0_ym19_1_1475720696802_72721"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_72835"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_72834">%% Login chain<br>% Moves our state to 'ingame' if success</span></font></div><div dir="ltr" style="margin-top: 0.1em; margin-bottom: 0.1em;" id="yui_3_16_0_ym19_1_1475720696802_72721"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_73127"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_73126">% Not implemented in example</span></font></div></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54313"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54313"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_54313">%% Ingame</div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_72350"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_72351">handle_event(info, {tcp, Socket, Bin}, </span></font>ingame<span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_72352">, D) </span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;">-> </span><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;">case proc_on_bin(Bin) of</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_70288"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_70287"> {build, Building, X, Y} -></span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_70776"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_70777"> {BuildUUID, BuildTime} = get_build_time(Building),</span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_71755"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_71754"> register_build_uuid(BuildUUID, Building, X, Y), </span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif"><span style="font-size: 13px;"><br></span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_72361"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_72362"> %% Make some helper functions to make this cleaner</span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_73428"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_73429"> % A cleaner option is to send a message to mailbox<br> % after parsing packet</span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif"><span style="font-size: 13px;"><br id="yui_3_16_0_ym19_1_1475720696802_71024"> <span id="yui_3_16_0_ym19_1_1475720696802_72363">{next_state, </span></span></font>ingame<span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_72364">, D, [</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_71570"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_71572"><span id="yui_3_16_0_ym19_1_1475720696802_71571"> {named_timeout, BuildTime, {built, </span></span></font><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_70779">BuildUUID}</span><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_70942">}, </span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_70943"> {named_timeout, 15000, keep_alive}, </span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_70944"> {named_timeout, 120000, endpoint_timeout}</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;"> ]</span><span id="yui_3_16_0_ym19_1_1475720696802_71469" style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;">}</span><span id="yui_3_16_0_ym19_1_1475720696802_70945" style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;">;</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;"><br></span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_71274"> {cancel_build, BuildUUID} -></span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_72144"> unregister_build_uuid(BuildUUID),</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_71286"> </span><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_72373"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_72374"><span id="yui_3_16_0_ym19_1_1475720696802_72375">{next_state, </span></span></font>ingame<span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_72376">, D, [</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1475720696802_71563"><span style="font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_71562"><span id="yui_3_16_0_ym19_1_1475720696802_71561"> {named_timeout, infinity, </span></span></font><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_71290">BuildUUID</span><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_71291">}, </span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_71292"> {named_timeout, 15000, keep_alive}, </span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_71293"> {named_timeout, 120000, endpoint_timeout}</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_73955"> ]}</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;">end;</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416">handle_event(named_timeout, {built, BuildUUID}, ingame, D) -><br> Socket = maps:get(socket, D),</div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"> notify_client_building_upgraded(Socket, BuildUUID),</div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_73746"> <span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_72247">{next_state, <span style="font-family: HelveticaNeue, "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 16px;" id="yui_3_16_0_ym19_1_1475720696802_72345">ingame</span>, D};</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;"><br></span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;"><br></span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_73952">To me this named_timeout really fits this model, keeps the code clean, keeps it all in one place. state_timeout helps us correctly transition our states from outgame/ingame/lobby/etc.<br><br>Possibly we could have 1 process for loggedout/outgame/lobby and 1 process for ingame, so we can chat while playing.</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;"><br></span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1475720696802_74592">The original 'timeout' seems out of place, only use case I see for it is if the process is idle to hibernate it, or to kill the process after a certain time, or other similar.<br><br>So to summarize, gen_statem allows us to guarantee state transitions even if other messages arrive to mailbox. Thus if we were to implement this code using processes or gen_server we<br>would be recreating a lot of gen_statem. Using the handle_event_function callback mode allows us to have this kind of flexible gen_statem.<br><br>This is my final argument in support of named_timeout. Thank you for your attention.</span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;"><br></span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53416"><span style="font-size: 13px; font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif;"><br><br><br><br></span></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53418"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53360"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53363"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif"><span style="font-size: 13px;"><br></span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53363"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif"><span style="font-size: 13px;"><br></span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53363"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif"><span style="font-size: 13px;"><br></span></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1475720696802_53302"><font face="Helvetica Neue, Segoe UI, Helvetica, Arial, Lucida Grande, sans-serif"><span style="font-size: 13px;"><br></span></font></div><div id="yui_3_16_0_ym19_1_1475720696802_52580" dir="ltr"><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;"><br></span></div><span style="font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, "Lucida Grande", sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1475720696802_51734"><br></span><br><br><div class="qtdSeparateBR"><br><br></div><div class="yahoo_quoted" style="display: block;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div dir="ltr"><font size="2" face="Arial"> On Thursday, October 6, 2016 5:31 AM, Raimo Niskanen <raimo+erlang-questions@erix.ericsson.se> wrote:<br></font></div> <br><br> <div class="y_msg_container">On Wed, Oct 05, 2016 at 04:41:11PM +0000, Vans S wrote:<br clear="none">> The state_timeout that was added makes more sense to me IMO. And what Fred said about uses for the current timeout being more limited are valid.<br clear="none"><br clear="none">It seems we are reaching a common conclusion on that one...<br clear="none"><br clear="none">> <br clear="none">> <br clear="none">> <br clear="none">> I think that if there is support for the way the current timeout is, that times out regardless of the state you are currently in, what is the problem with having one vs multiple of them?<br clear="none"><br clear="none">I am confused. The current timeout {timeout,Time,Msg} can not be used<br clear="none">between different states since it is cancelled by the first event that<br clear="none">arrives, so it is cancelled with the first possibility to change states.<br clear="none"><br clear="none">So to have more than one of them would mean as they are defined now<br clear="none">that when the first generates a timeout event the others are cancelled<br clear="none">and therefore pointless. So I see no point in having multiple<br clear="none">{timeout,Time,Msg}s.<br clear="none"><br clear="none">Kind of the same applies to {state_timeout,Time,Msg}. When you change<br clear="none">states it (they) is (are) cancelled, so having multiple timers while<br clear="none">staying in one state gets kind of toothless. You could do it but it feels<br clear="none">like you will be tempted to force your machine to stay in one state when<br clear="none">it really should have more states coupled to the different timeouts.<br clear="none"><br clear="none">The reason {state_timeout,Time,Msg} is interesting to implement is that it<br clear="none">fits a very common niche of timeouts in state machines and is very simple<br clear="none">to use.<br clear="none"><br clear="none">> <br clear="none">> Is it really that much of an annoying feature to add. Where the cost of maintenance of the code will be greater then the benefit? <br clear="none">> I even mentioned I would put in the pull request for it if we can agree it would serve some use. <br clear="none"><br clear="none">The problem is not about having multiple timers, it is about having more<br clear="none">general timers since I am convinced that if you have multiple timers you<br clear="none">will want to have control over when to cancel them, and how, which forces a<br clear="none">new more general timer concept. Hence my suggestion of<br clear="none">{named_timeout,Time,Name}.<br clear="none"><br clear="none">For example I think you mentioned that it would be handy to be able to bump<br clear="none">a timer i.e add more time to a running timer, and then you need to know how<br clear="none">much time a timer has left when you cancel it, which is problematic with<br clear="none">the API that we need to use as gen_statem now looks. You have to return an<br clear="none">action from a state function to control the timer so you can not act on the<br clear="none">return value from erlang:cancel_timer/1,2, not unless you introduce a fun()<br clear="none">in the action, or define different actions for all possible operations.<br clear="none">And just for at bump action the question is what to do if the timer has<br clear="none">already expired? Time out or restart? Are there more desired actions than<br clear="none">bump?<br clear="none"><br clear="none">So I think the API in gen_statem which we are limited by makes it hard to<br clear="none">build general enough timers when it is comparatively easy to write a small<br clear="none">timer library that does what you need and keep track of the TimerRefs in<br clear="none">your state machine Data, which gives you full control over the timers.<br clear="none"><br clear="none">And in general, as Fred Hebert pointed out, the more state data you get in<br clear="none">the state machine engine, the less control the state machine implementation<br clear="none">gets which for instance decreases your freedom to do what you want during<br clear="none">code upgrade where you only may act on State and Data and can know nothing<br clear="none">about the engine state data.<br clear="none"><br clear="none">If we introduce {named_timeout,Time,Name} and it turns out to not become<br clear="none">general enough, most will use erlang:start_timer/3,4 anyway and<br clear="none">we will have seldom used code to maintain. Forcing users to use<br clear="none">erlang:start_timer/3,4 for all cases where {timeout,Time,Msg} and<br clear="none">{state_timeout,Time,Msg} does not suffice might be the better option<br clear="none">since all will have to understand them anyway, which improves readability.<br clear="none"><br clear="none">But if you have a suggestion of a good timer interface for multiple timers<br clear="none">in gen_statem at least I would be very interested to hear it. I do not<br clear="none">think the suggestions that have surfaced so far have been worthy of<br clear="none">implementation...<br clear="none"><br clear="none">/ Raimo<br clear="none"><br clear="none"><br clear="none"><br clear="none">> <br clear="none">> On Wednesday, October 5, 2016 9:45 AM, Raimo Niskanen <raimo+<a shape="rect" ymailto="mailto:erlang-questions@erix.ericsson.se" href="mailto:erlang-questions@erix.ericsson.se">erlang-questions@erix.ericsson.se</a>> wrote:<br clear="none">> <br clear="none">> <br clear="none">> On Tue, Oct 04, 2016 at 10:52:38PM -0500, Fred Hebert wrote:<br clear="none">> > On 10/04, Vans S wrote:<br clear="none">> > :<br clear="none">> > >I did not consider this. This is truly problematic BUT would not the <br clear="none">> > >current way timeout works run into this same problem? So this should <br clear="none">> > >not affect allowing multiple timeouts vs a single timeout.<br clear="none">> > <br clear="none">> > Right now what you can do if you manage your own timers is to cancel the <br clear="none">> > old ones or ignore them (you've got their refs so it's easy) and you can <br clear="none">> > set new ones right away. You can't do that however if you rely on the <br clear="none">> > return tuple of a state transition to do it, since that transition <br clear="none">> > cannot happen from a code_change callback.<br clear="none">> <br clear="none">> To some extent this cat is already out of the box thanks to the gen_fsm<br clear="none">> timeout that got inherited to gen_statem. If such a timer is running and<br clear="none">> there is a code change on the server, it may expire "earlier" than you<br clear="none">> would like or rather suddenly you might have wanted this particular timeout<br clear="none">> to be longer.<br clear="none">> <br clear="none">> If this is a real problem you can set a flag in your Data to note that<br clear="none">> there has been a code change, and then act differently when the timeout<br clear="none">> comes, or change states to one that is aware of the possibility of an early<br clear="none">> timeout.<br clear="none">> <br clear="none">> I wonder how this affects my state_timeout proposal...<br clear="none">> It has got the same corner case, and the same workarounds apply.<br clear="none">> <br clear="none">> The same would apply to named_timeout, with the same workarounds,<br clear="none">> but the more timers you do not control, the messier to work around,<br clear="none">> I guess...<br clear="none">> <br clear="none">> > <br clear="none">> > >Learning from that, I rather this be inside gen_statem. If its not, I <br clear="none">> > >would have no problem to write my own little timer library for <br clear="none">> > >cancel_timer+purging mailbox. <br clear="none">> > ><br clear="none">> > <br clear="none">> > Usually the pattern I use is to write a short 'reset_<whatever>_timer' <br clear="none">> > function that returns its ref, and then I can track the ref in my state.<br clear="none">> <br clear="none">> It may be a bit tricky to get it right, but it is educational.<br clear="none">> <br clear="none">> The problem in writing a library for this is that there are some options<br clear="none">> on both how to start a timer and how to cancel it, so a library would<br clear="none">> either not get it right for everybody or would have to take very many<br clear="none">> options to be right for everybody.<br clear="none">> <br clear="none">> So this may be a good candidate for a do-it-as-you-want-it library module.<br clear="none">> <br clear="none">> / Raimo<br clear="none">> <br clear="none">> <br clear="none">> <br clear="none">> > <br clear="none">> > I always found this fairly convenient to deal with things, and for <br clear="none">> > example, I mostly don't clean up timers very hard. I.e. you can make use <br clear="none">> > of the newer cancel_timer options for async returns with or without <br clear="none">> > info, and by matching on specific refs, I can just ignore timers that <br clear="none">> > are for events I know are no longer up for consideration and let them <br clear="none">> > disappear, and automatically can ignore a bunch of potential race <br clear="none">> > conditions (i.e. no mailbox cleanup, just ignore messages coming in)<br clear="none">> > <br clear="none">> > This is also neat to avoid whatever blocking or synchronization that <br clear="none">> > could take place on timer management, but in some cases you may still <br clear="none">> > want to do that.<br clear="none">> > <br clear="none">> > <br clear="none">> > >Since you are only in ONE state at a time, you automatically know if <br clear="none">> > >the timeout happened, it MUST correspond to the current state we are <br clear="none">> > >in. <br clear="none">> > ><br clear="none">> > <br clear="none">> > Does it? Couldn't the timeout event have been postponed, and therefore <br clear="none">> > come from a prior state change? I believe so. There's no relationship <br clear="none">> > between an event being handled in a state and that event having been <br clear="none">> > sent in that state.<br clear="none">> > <br clear="none">> > >Another option is to remove the timeout then. It just seems out of place to me. <br clear="none">> > ><br clear="none">> > <br clear="none">> > At the two extremes are the positions that all timeout management is <br clear="none">> > manual, and that gen_statem is to replace full-on control of timeouts <br clear="none">> > people can do manually (with all the cancellation options and whatnot).<br clear="none">> > <br clear="none">> > Those are the two extremes, and the current thing is where on the <br clear="none">> > gradient does the timeout implementation currently lie. So far it <br clear="none">> > appears to be a fairly minimal convenience factor. I'm just wondering if <br clear="none">> > it's worth pushing it further on the gradient of "emulating all the <br clear="none">> > flexibility of manual control."<br clear="none">> > <br clear="none">> > >What is the use case of a single timeout with a UNIQUE EventContent?<br clear="none">> > ><br clear="none">> > <br clear="none">> > The current semantics are specific about one thing:<br clear="none">> > <br clear="none">> > Generates an event of event_type() timeout after this time (in <br clear="none">> > milliseconds) unless another event arrives in which case this <br clear="none">> > time-out is cancelled. Notice that a retried or inserted event <br clear="none">> > counts like a new in this respect.<br clear="none">> > <br clear="none">> > These timeouts are mostly used for one thing in my experience: tracking <br clear="none">> > inactivity of the state machine. gen_servers and gen_fsms have the same <br clear="none">> > one. The only pragmatic use case I've made of them were to give myself a <br clear="none">> > delay of N milliseconds before which it would be reasonable to send the <br clear="none">> > process in hibernation, GCing and compacting its memory at once, since <br clear="none">> > it had not received a message in that time otherwise.<br clear="none">> > <br clear="none">> > All other timeouts I tend to manage by hand because I do not want them <br clear="none">> > to be interruptible by the arrival of other messages in the mailbox of <br clear="none">> > the process.<br clear="none">> > <br clear="none">> > This is a very, very important detail! Those semantics are also a lot <br clear="none">> > trickier to handle by yourself (you'd need to put them in every clause) <br clear="none">> > than most other timers you could imagine using. This makes them a fairly <br clear="none">> > good idea to include in the OTP behaviours themselves, as opposed to <br clear="none">> > most other timer usages, IMO.<br clear="none">> > <br clear="none">> > Regards,<br clear="none">> > Fred.<br clear="none">> <br clear="none">> -- <br clear="none">> <br clear="none">> / Raimo Niskanen, Erlang/OTP, Ericsson AB<br clear="none">> _______________________________________________<br clear="none">> erlang-questions mailing list<br clear="none">> <a shape="rect" ymailto="mailto:erlang-questions@erlang.org" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br clear="none">> <a shape="rect" href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><div class="yqt3013698945" id="yqtfd80671"><br clear="none">> <br clear="none">> <br clear="none"><br clear="none">-- <br clear="none"><br clear="none">/ Raimo Niskanen, Erlang/OTP, Ericsson AB<br clear="none">_______________________________________________<br clear="none">erlang-questions mailing list<br clear="none"><a shape="rect" ymailto="mailto:erlang-questions@erlang.org" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br clear="none"><a shape="rect" href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a></div><br><br></div> </div> </div> </div></div></body></html>