queutils v1.1.0 Queutils.BlockingProducer View Source

A GenStage producer that acts as a blocking queue, with a fixed length. Blocks any time Queutils.BlockingProducer.push/2 is called when the queue is at its maximum length.

This can be used as an entry-point to a GenStage pipeline, since the max queue length provides for back-pressure. You can even set the queue's length to zero in order to block all pushes until demand comes in.

Usage

Add it to your application supervisor's start/2 function like this:

def start(_type, _args) do
  children = [
    ...
    {Queutils.BlockingProducer, name: MessageProducer, max_length: 10_000},
    ...
  ]

  opts = [strategy: :one_for_one, name: MyApplication.Supervisor]
  Supervisor.start_link(children, opts)
end

Then, you can push messages to the queue like this:

:ok = Queutils.BlockingProducer.push(MessageProducer, :my_message)

Broadway users be forewared! A Broadway module needs to start its producer itself, so it's not possible to customize the process ID a la the :name option documented below. If you're in that boat, you should use a Queutils.BlockingQueue along with a Queutils.BlockingQueueProducer, so you can customize your reference to your BlockingQueue.

Options

  • :name - the ID of the queue. This will be the first argument to the push/2 function.
  • :max_length - The maximum number of messages that this process will store until it starts blocking. Default is 1,000.
  • :dispatcher - The GenStage dispatcher that this producer should use. Default is GenStage.DemandDispatcher.

Link to this section Summary

Functions

Push an item onto the queue. This function will block if the queue is full, and unblock once it's not.

Link to this section Functions

Specs

push(term(), term()) :: :ok

Push an item onto the queue. This function will block if the queue is full, and unblock once it's not.