View Source ExKits.Utils.Buffer (ex_kits v0.2.7)

A simple buffer queue implementation using ETS tables.

This module provides a way to create a buffer queue with a specified capacity and allows items to be put into and taken out of the queue. If the queue is full, put will return {:error, :full}. If the queue is empty, take will block until an item is available.

Examples

iex> {:ok, pid} = ExKits.Utils.Buffer.start_link(name: :my_queue, size: 10)
iex> ExKits.Utils.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExKits.Utils.Buffer.take(:my_queue)
[1, 2, 3]

Summary

Functions

Returns a specification to start this module under a supervisor.

Adds the given items to the buffer queue.

Returns the number of items in the buffer queue.

Starts a new buffer process.

Removes and returns items from the buffer queue.

Types

@type t() :: %ExKits.Utils.Buffer{
  buff: atom(),
  buff_size: non_neg_integer(),
  capacity: non_neg_integer(),
  gc_freq: non_neg_integer(),
  touch: non_neg_integer()
}

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

put(name, items, timeout \\ 5000)

View Source
@spec put(atom(), [any()], non_neg_integer()) :: :ok | {:error, :full}

Adds the given items to the buffer queue.

If the buffer queue is full, this function will return {:error, :full}.

Examples

iex> {:ok, _pid} = ExKits.Utils.Buffer.start_link(:my_queue, 10)
iex> ExKits.Utils.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExKits.Utils.Buffer.put(:my_queue, [4, 5, 6], 1000)
:ok
iex> ExKits.Utils.Buffer.put(:my_queue, [7, 8, 9, 10, 11])
{:error, :full}
Link to this function

size(name, timeout \\ 5000)

View Source
@spec size(atom(), non_neg_integer()) :: non_neg_integer()

Returns the number of items in the buffer queue.

Examples

iex> {:ok, _pid} = ExKits.Utils.Buffer.start_link(:my_queue, 10)
iex> ExKits.Utils.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExKits.Utils.Buffer.size(:my_queue)
3
@spec start_link(keyword()) :: Agent.on_start()

Starts a new buffer process.

Options

  • :name - The name of the buffer process. Must be an atom
  • :size - The maximum number of items the buffer can hold

Examples

iex> {:ok, pid} = ExKits.Utils.Buffer.start_link(name: :my_queue, size: 10)
iex> ExKits.Utils.Buffer.put(:my_queue, [1, 2, 3])
@spec stop(atom()) :: :ok
Link to this function

take(name, timeout \\ 5000)

View Source
@spec take(atom(), non_neg_integer()) :: [any()]

Removes and returns items from the buffer queue.

If the buffer queue is empty, this function will block until an item is available.

Examples

iex> {:ok, _pid} = ExKits.Utils.Buffer.start_link(:my_queue, 10)
iex> ExKits.Utils.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExKits.Utils.Buffer.take(:my_queue)
[1, 2, 3]
iex> ExKits.Utils.Buffer.take(:my_queue)
[]