Queutils.BlockingProducer (queutils v1.2.1) 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, subscribe a GenStage to it.

def init(:ok) do
  {:consumer, :the_state_does_not_matter, subscribe_to: [MessageProducer]}
end
    ```

You can now 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

Get the number of items currently sitting in a queue.

Get the total count of messages that have been popped from a queue over its lifetime.

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

Get the total count of messages that have been pushed to a queue over its lifetime.

Get the number of blocked processes waiting to push to a queue.

Link to this section Functions

Get the number of items currently sitting in a queue.

Get the total count of messages that have been popped from a queue over its lifetime.

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.

Get the total count of messages that have been pushed to a queue over its lifetime.

Get the number of blocked processes waiting to push to a queue.