community_theatre v0.1.1 CommunityTheatre.RateLimiter behaviour
The RateLimiter
behaviour allows you to define for yourself how you would
like messages to be handled in the event that they arrive faster than
required.
The behaviour specifies three callbacks;
init/1
which you can use for initialising your data structure,merge/2
which you can use to update your data when new messages are received, andget/1
which is called when it is time to send a message to a subscribee.
CommunityTheatre
provides two rate limiters out of the box; but you can
easily write your own if they don't meet your needs:
CommunityTheatre.RateLimiter.Drop
- drop any spurious messages and only pass on the last message received.CommunityTheatre.RateLimiter.Average
- computes a rolling average presuming that the inbound message payloads are numeric.
Link to this section Summary
Callbacks
Returns the next value that should be sent to a subscriber.
Called when creating a new rate limited subscription. Use this to initialise the data structure needed to keep state for rate limiting.
Called when a new message is received from a source so that you can update the limiter state.
Link to this section Types
Link to this section Callbacks
get(t)
get(t()) :: {:ok, CommunityTheatre.Message.t() | nil, t()} | {:error, any()}
Returns the next value that should be sent to a subscriber.
init(update_frequency)
Called when creating a new rate limited subscription. Use this to initialise the data structure needed to keep state for rate limiting.
merge(t, arg2)
merge(t(), CommunityTheatre.Message.t()) :: {:ok, t()} | {:error, any()}
Called when a new message is received from a source so that you can update the limiter state.