It is a bit more complicated than that - a producer has to buffer demand and events. Consider the following scenario:
- When the producer is initialized there is neither buffered demand nor buffered events.
- Once a consumer is subscribed it can demand events via
handle_demand
. It could request 1000 events. But as the producer doesn’t have any events it has to buffer demand somewhere innew_producer_state
. Sohandle_demand
returns{:noreply, [], new_producer_state}
, i.e.handle_demand
returns without releasing any events. - The producer may then get a
handle_info
indicating that raw data for new events is available. That raw data my be enough to generate 1200 events. Given that demand for 1000 events is bufferedhandle_info
can return{:noreply, events, new2_producer_state}
whereevents
is a list of 1000 events. The remaining 200 events (or the raw data to generate them) has to be buffered somewhere innew2_producer_state
(i.e. buffered events). - The producer may now receive another
handle_demand
for another 1000 events.handle_demand
can immediately release the remaining 200 events inevents2
by returning{:noreply, events2, new3_producer_state}
while at the same time buffering demand for the remaining 800 events somewhere insidenew3_producer_state
. - etc.