Dispenser.Server.BatchingBufferServer (dispenser v0.1.0) View Source

A BatchingBufferServer is an example GenServer that uses Dispenser.Buffer. It can receive events and send them to subscriber processes.

The BatchingBufferServer works like BufferServer, but tries to minimize the number of messages sent to subscribers by only sending events when there is a large enough number of them.

Subscribers can control the flow by telling the BatchingBufferServer how many events they want, using ask/3. See ask/3 for more information about how events are sent to subscribers.

Link to this section Summary

Types

The arguments required to create a BatchingBufferServer.

The opaque internal state of the BatchingBufferServer.

Functions

Add events to the BatchingBufferServer.

Ask for events from the BatchingBufferServer.

Returns a specification to start this module under a supervisor.

Start a new BatchingBufferServer GenServer.

Get various statistics about the BatchingBufferServer for use when debugging and generating metrics.

Unsubscribe from the BatchingBufferServer.

Link to this section Types

Specs

init_args(event) :: %{
  buffer: Dispenser.Buffer.t(event, pid()),
  batch_size: pos_integer(),
  max_delay: pos_integer()
}

The arguments required to create a BatchingBufferServer.

:buffer defines the Buffer used internally by the BatchingBufferServer.

:batch_size defines the minimum batch size of events to gather before sending them to subscribers.

:max_delay defines the maximum amount of time in milliseconds to wait for more events when there are fewer than batch_size events in the buffer. Once max_delay is reached, events will be sent to subscribers even if there are fewer than batch_size events.

See start_link/1.

Specs

t(event)

The opaque internal state of the BatchingBufferServer.

Link to this section Functions

Specs

append(GenServer.server(), [event]) :: {:ok, dropped :: non_neg_integer()}
when event: any()

Add events to the BatchingBufferServer.

If the buffer reaches its capacity, an error is returned with the number of events that were were dropped.

Specs

Ask for events from the BatchingBufferServer.

Events will be delivered asynchronously to the subscribed pid in the shape of:

{:handle_assigned_events, sender, events}

where:

  • sender is the pid of this BatchingBufferServer.
  • events is a list of events that were appended to the BatchingBufferServer.
Link to this function

ask(server, subscriber, amount)

View Source

Specs

ask(GenServer.server(), pid(), non_neg_integer()) :: :ok

Returns a specification to start this module under a supervisor.

See Supervisor.

Specs

start_link(init_args(event)) :: {:ok, pid()} | {:error, term()}
when event: any()

Start a new BatchingBufferServer GenServer.

See init_args/0 and GenServer.start_link/2

Specs

Get various statistics about the BatchingBufferServer for use when debugging and generating metrics.

Specs

unsubscribe(GenServer.server()) :: :ok | {:error, :not_subscribed}

Unsubscribe from the BatchingBufferServer.

Link to this function

unsubscribe(server, subscriber)

View Source

Specs

unsubscribe(GenServer.server(), subscriber :: pid()) ::
  :ok | {:error, :not_subscribed}