Can I inject a message to the first of the message queue of process?

Is the order of message queue unchangeable?

I want to know how GenServer.handle_continue/2 works.

The order of the queue is unchangeable - but you can executive a “selective receive” out of the queue using pattern matching. I’m in China at the moment and can’t access a lot of stuff but googling should get you on the right track.

No, you can not re-order messages.

But receive is selective, one could match on messages only which match a certain shape.

But I doubt that this is the way how handle_continue works.

The controll loop will just “see” that there is the command to run handle_continue in the return value of init, and then call that function early/first during the receive-loop.

1 Like

The {:ok, state, {:continue, continue}} tuple is simply a special case of the {:ok, state, timeout} tuple.

For startup the {:ok, state, {:continue, continue}} value from init enters here, where {:continue, continue} is the timeout value:

This in turn immediately executes this loop clause:

So as @NobbZ alluded to - there is no continue message on the process mailbox - in fact it is treated as a {:continue, continue} timeout value (other values being a number representing an actual timeout, :infinity, and :hibernate).


Some blog posts that really helped me understand handle_continue when it first came out:

Hope it helps!


Also note that gen_statem from Erlang stdlib has first-class support for the notion of incoming messages generating new internal actions that “jump the line” to occur before other pending OTP messages. It is perhaps a heavy tool to wield depending on your surrounding context and requirements, but it’s extremely powerful in the right scenario. Works totally fine in Elixir, with or without the Hex wrapper package.

1 Like

Thanks a lot!