|
| 1 | +defmodule Queutils.BlockingProducer do |
| 2 | + use GenStage |
| 3 | + |
| 4 | + @moduledoc """ |
| 5 | + A `GenStage` producer that acts as a blocking queue, with a fixed length. |
| 6 | + Blocks any time `Queutils.BlockingProducer.push/2` is called when the queue |
| 7 | + is at its maximum length. |
| 8 | +
|
| 9 | + This can be used as an entry-point to a `GenStage` pipeline, since the |
| 10 | + max queue length provides for back-pressure. |
| 11 | + You can even set the queue's length to zero in order to block all pushes |
| 12 | + until demand comes in. |
| 13 | +
|
| 14 | +
|
| 15 | + ## Usage |
| 16 | +
|
| 17 | + Add it to your application supervisor's `start/2` function like this: |
| 18 | +
|
| 19 | + def start(_type, _args) do |
| 20 | + children = [ |
| 21 | + ... |
| 22 | + {Queutils.BlockingProducer, name: MessageProducer, max_length: 10_000}, |
| 23 | + ... |
| 24 | + ] |
| 25 | +
|
| 26 | + opts = [strategy: :one_for_one, name: MyApplication.Supervisor] |
| 27 | + Supervisor.start_link(children, opts) |
| 28 | + end |
| 29 | +
|
| 30 | + Then, you can push messages to the queue like this: |
| 31 | +
|
| 32 | + :ok = Queutils.BlockingProducer.push(MessageProducer, :my_message) |
| 33 | +
|
| 34 | +
|
| 35 | + Broadway users be forewared! A Broadway module needs to start its producer itself, |
| 36 | + so it's not possible to customize the process ID a la the `:name` option documented below. |
| 37 | + If you're in that boat, you should use a `Queutils.BlockingQueue` along with |
| 38 | + a `Queutils.BlockingQueueProducer`, so you can customize your reference to your `BlockingQueue`. |
| 39 | +
|
| 40 | + ## Options |
| 41 | +
|
| 42 | + - `:name` - the ID of the queue. This will be the first argument to the `push/2` function. |
| 43 | + - `:max_length` - The maximum number of messages that this process will store until it starts blocking. Default is 1,000. |
| 44 | + - `:dispatcher` - The `GenStage` dispatcher that this producer should use. Default is `GenStage.DemandDispatcher`. |
| 45 | + """ |
| 46 | + |
| 47 | + def start_link(opts) do |
| 48 | + case Keyword.fetch(opts, :name) do |
| 49 | + {:ok, name} -> GenStage.start_link(__MODULE__, opts, name: name) |
| 50 | + :error -> GenStage.start_link(__MODULE__, opts) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + def child_spec(opts) do |
| 55 | + %{ |
| 56 | + id: Keyword.get(opts, :name, Queutils.BlockingProducer), |
| 57 | + start: {__MODULE__, :start_link, [opts]}, |
| 58 | + type: :supervisor |
| 59 | + } |
| 60 | + end |
| 61 | + |
| 62 | + def push(queue, msg) do |
| 63 | + GenStage.call(queue, {:push, msg}) |
| 64 | + end |
| 65 | + |
| 66 | + @impl true |
| 67 | + def init(opts) do |
| 68 | + dispatcher = Keyword.get(opts, :dispatcher, GenStage.DemandDispatcher) |
| 69 | + max_length = Keyword.get(opts, :max_length, 1_000) |
| 70 | + unless max_length >= 0 do |
| 71 | + raise "Invalid argument :max_length. Must be an integer zero or greater, but was #{inspect max_length}" |
| 72 | + end |
| 73 | + {:producer, %{queue: [], waiting: [], demand: 0, max_length: max_length}, dispatcher: dispatcher} |
| 74 | + end |
| 75 | + |
| 76 | + @impl true |
| 77 | + def handle_call({:push, msg}, from, state) do |
| 78 | + :ok = validate_state(state) |
| 79 | + cond do |
| 80 | + state.demand > 0 -> |
| 81 | + remaining_demand = state.demand - 1 |
| 82 | + {:reply, :ok, [msg], %{state | demand: remaining_demand}} |
| 83 | + Enum.count(state.queue) >= state.max_length -> |
| 84 | + waiting = state.waiting ++ [{from, msg}] |
| 85 | + {:noreply, [], %{state | waiting: waiting}} |
| 86 | + true -> |
| 87 | + queue = state.queue ++ [msg] |
| 88 | + {:reply, :ok, [], %{state | queue: queue}} |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + @impl true |
| 93 | + def handle_demand(demand, state) do |
| 94 | + :ok = validate_state(state) |
| 95 | + |
| 96 | + total_demand = demand + state.demand |
| 97 | + |
| 98 | + {popped, remaining} = Enum.split(state.queue, total_demand) |
| 99 | + {popped_waiters, still_waiting} = Enum.split(state.waiting, total_demand) |
| 100 | + |
| 101 | + msgs_from_waiters = Enum.map(popped_waiters, fn {from, msg} -> |
| 102 | + GenStage.reply(from, :ok) |
| 103 | + msg |
| 104 | + end) |
| 105 | + |
| 106 | + queue = remaining ++ msgs_from_waiters |
| 107 | + remaining_demand = total_demand - Enum.count(queue) |
| 108 | + |
| 109 | + {:noreply, popped, %{state | demand: remaining_demand, queue: queue, waiting: still_waiting}} |
| 110 | + end |
| 111 | + |
| 112 | + defp validate_state(state) do |
| 113 | + # If we have a non-zero demand, it is assumed that we will not have |
| 114 | + # anyone waiting and that the queue is empty, since we would have |
| 115 | + # popped off all those messages before building up any demand. |
| 116 | + |
| 117 | + cond do |
| 118 | + state.demand < 0 -> |
| 119 | + raise "Incorrect state: BlockingProducer has a negative demand (demand is #{inspect state.demand})." |
| 120 | + state.demand > 0 && not Enum.empty?(state.queue) -> |
| 121 | + raise "Incorrect state: BlockingProducer has demand but also has items in its queue." |
| 122 | + state.demand > 0 && not Enum.empty?(state.waiting) -> |
| 123 | + raise "Incorrect state: BlockingProducer has demand but also has processes waiting to insert." |
| 124 | + true -> :ok |
| 125 | + end |
| 126 | + end |
| 127 | +end |
0 commit comments