Packages

Community Theatre helps you put on shows when you don't have the resources to be on Broadway.

Current section

Files

Jump to
community_theatre lib community_theatre rate_limiters drop.ex
Raw

lib/community_theatre/rate_limiters/drop.ex

defmodule CommunityTheatre.RateLimiter.Drop do
defstruct ~w[last_message]a
alias CommunityTheatre.RateLimiter
@behaviour RateLimiter
@moduledoc """
Sample dropping ratae limiter
Only keeps the last message it receives for passing on to the subscriber.
"""
@impl RateLimiter
def init(_update_frequency), do: {:ok, new()}
@impl RateLimiter
def merge(%__MODULE__{}, message), do: {:ok, %__MODULE__{last_message: message}}
@impl RateLimiter
def get(%__MODULE__{last_message: message}), do: {:ok, message, new()}
defp new, do: %__MODULE__{last_message: nil}
end