[erlang-questions] erlang:restart_timer

Loïc Hoguin <>
Wed Nov 5 14:59:51 CET 2014


> I propose a new BIF:
>
> 	erlang:restart_timer(Ref, Time) -> false | RemainingTime

This is definitely something that could see use in a few places inside 
Cowboy.

> This will restart a time if it is active and return the time that remained when it was restarted.
> If restart_timer return false either the timer has already time out or the reference is not a timer.

In some cases a "start_or_restart_timer" scenario could also be useful. 
Otherwise I'm stuck having to check the return value and having to call 
start_timer as before, making it not as useful as it could be. For 
example if I have a timeout to close the socket if no data is received, 
and I have both data and the timeout in my mailbox, I don't really want 
to process the data and then immediately close the connection. There's 
data, so the connection is still up, so I want to restart the timer even 
if it already expired.

> The benefit is a speedup with at least 2.5 times. comparing:
>
> 	erlang:restart_timer(Ref, Time)
> vs
> 	erlang:cancel_timer(Ref)
> 	Ref1 = erlang:start_timer(Time, self(), Message)
>
> The cost saving is that restart_timer preserve already stored messages. And also internal timer structures are
> preserved. Resulting in less overhead.
>
> The interface is simple and do not require a new reference every restart, this could also save the time
> and heap updating a server state.

That part sounds good.

-- 
Loïc Hoguin
http://ninenines.eu


More information about the erlang-questions mailing list