Skip to content

Commit

Permalink
Document Subscription Demand in BroadcastDispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed Oct 4, 2023
1 parent 184959e commit 858c01a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/gen_stage/dispatchers/broadcast_dispatcher.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule GenStage.BroadcastDispatcher do
@moduledoc """
@moduledoc ~S"""
A dispatcher that accumulates demand from all consumers
before broadcasting events to all of them.
Expand Down Expand Up @@ -29,6 +29,27 @@ defmodule GenStage.BroadcastDispatcher do
[{producer, selector: fn %{key: key} -> String.starts_with?(key, "foo-") end}]}
end
## Demand while Setup
```
[Producer Consumer 1]
/ \
[Producer] - - [Consumer]
\ /
[Producer Consumer 2]
```
When starting `Producer Consumer 1` before `Producer Consumer 2`, it is
possible that the first batch of events is only delivered to
`Producer Consumer 1` since `Producer Consummer 2` is not registered yet.
Since `producer_consumer` is buffering, it is irrelenvant if the `Consumer`
has not been started yet, the demand arises on start of the
`producer_consumer`. The more stages you register, the more likely it is that
you will miss events.
It is therefore recommended to start the consumers with an empty
`subscribe_to` option and create the subscriptions manually after all stages
were started using `GenStage.async_subscribe/2`.
"""

@behaviour GenStage.Dispatcher
Expand Down

0 comments on commit 858c01a

Please sign in to comment.