I’m by no means a GenStage expert - I have just happened to play around with it for a few days 4 months ago - and looking at the documentation right now I still find it a bit hard to digest.
For example it’s only when I started to write the code that I noticed that any of the callbacks that can return a tuple that includes
[events] can release events to the next stage - skimming through the examples in the documentation you could easily be left with the impression that
handle_demand/2 is the place where events are released - when in fact it is only one place where events are released.
If you have events during the
handle_demand/2 callback then by all means release what you have that doesn’t exceed the demand. But as a producer any unfulfilled demand has to be “stored” - because the consumer is only going to issue another “demand” if it wants more than it already asked for.
So whenever a producer “acquires events” it can release them immediately provided it has “stored demand”.
So there are situations where
handle_demand/2 will simply return an empty event list (because there are simply none available at the time) - and the events are only released later when the producer somehow gets ahold of them - and then they are released as a result of the callback that “delivers” the events to the producer.
Now a consumer is a
GenStage that is at very end of the pipeline and is ultimately considered to be the “constraining operation” - that is why it gets to set the demand that propagates up all the way to the producer at the beginning of the pipeline (who has to honour that constraint). So
ConsumerSupervisor is inherently designed to be at the end of that pipeline.
My guess is that it doesn’t need to be a producer because conceptually as it is the “constraining operation” back pressure isn’t an issue with any processing stages that follow - they can simply be implemented as
GenServers because they can deal with any volume that the
ConsumerSupervisor is capable of throwing at them.
Which brings me to the pertinent point - why would you think that you need
ConsumerSupervisor feeding another
ConsumerSupervisor? It’s a necessary question to ensure that we aren’t running into the XY problem - i.e. there may be a solution to your actual problem that has nothing to do with