Can you specify :max_demand when subscribing using init/1 of a GenStage consumer?

It doesn’t appear that you can. Those opts are only listed for sync_subscribe/3 in the docs. https://hexdocs.pm/gen_stage/GenStage.html#sync_subscribe/3

Is this because you cannot guarantee the producer has implemented a handle_subscribe callback?

It can be done like this:

def init(args) do
  {:consumer, args, subscribe_to: [{producer, max_demand: 1000}]}
end
3 Likes

Brilliant, thank you!