erlchronos

Copyright (c) 2016 Guilherme Andrade

Version: 2.0.1

Authors: Guilherme Andrade (erlchronos(at)gandrade(dot)net).

erlchronos: Erlang/OTP gen_server wrapper with ticks


<a name="What_is_it?">What is it?</a>

erlchronos provides a gen_server wrapper, ticked_gen_server, that allows one to more easily manage triggering and dealing with ticks at regular intervals by specifying two new callbacks, tick_duration/2 and handle_tick/4.

It also does away with the usual erlang:send_after/3, timer:send_interval/2, etc. approaches, instead relying on two key mechanisms for tick enforcement:

<a name="Why?">Why?</a>

The traditional approach of having a message sent to a gen_server's inbox at regular intervals isn't always the more appropriate, and might misbehave significantly every time the system is subjected to message bursts (among other factors), even when actively accounting for drift, due to the strictly-ordered nature of inbox consumption.

An alternative would be to change the receive logic itself and pattern-match against timer / higher-priority messages, but this would quickly degrade performance on flooded inboxes and create a degeneration feedback loop.

This compromise solution tries not to fiddle too much with, nor reinvent the existing building blocks.

<a name="Pros">Pros</a>

<a name="Cons">Cons</a>

<a name="How_do_I_use_it?">How do I use it?</a>


ticked_gen_server:start(?MODULE, [], [{ticks, ["tick identifier"]}]).

% TickDuration in milliseconds
-spec tick_duration(TickId :: term(), State :: term())
        -> {TickDuration :: pos_integer(), NewState :: term()}.
tick_duration("tick identifier", State) ->
    {100, State}.

-spec handle_tick(TickId :: term(), TickGeneration :: non_neg_integer(),
                  ActualTickDuration :: non_neg_integer(), State :: term())
      -> {noreply, NewState :: term()}.
handle_tick(TickId, TickGeneration, ActualTickDuration, State) ->
   % Tick!
   {noreply, State};

Basic example under examples/.

<a name="Which_problems_doesn't_it_solve?">Which problems doesn't it solve?</a>

<a name="Future_plans">Future plans</a>

Modules

erlchronos
ticked_gen_server