BlockingQueue

BlockingQueue is a simple queue implemented as a GenServer. It has a fixed maximum length.

The queue is designed to decouple but limit the latency of a producer and consumer. When pushing to a full queue the push operation blocks preventing the producer from making progress until the consumer catches up. Likewise, when calling pop on an empty queue the call blocks until there is work to do.

Examples

{:ok, pid} = BlockingQueue.start_link(5)
BlockingQueue.push(pid, "Hi")
BlockingQueue.pop(pid) # should return "Hi"

Summary

pop(pid)

Pops the least recently pushed item from the queue. Blocks if the queue is empty until an item is available

push(pid, item)

Pushes a new item into the queue. Blocks if the queue is full

start_link(n)

Start a queue process with GenServer.start_link/2

Types

on_start :: {:ok, pid} | :ignore | {:error, {:already_started, pid} | term}

Functions

pop(pid)

Specs:

  • pop(pid) :: any

Pops the least recently pushed item from the queue. Blocks if the queue is empty until an item is available.

pid is the process ID of the BlockingQueue server.

push(pid, item)

Specs:

  • push(pid, any) :: nil

Pushes a new item into the queue. Blocks if the queue is full.

pid is the process ID of the BlockingQueue server. item is the value to be pushed into the queue. This can be anything.

start_link(n)

Specs:

Start a queue process with GenServer.start_link/2.

n Is the maximum queue depth.