apr
Message Prioritization inside GenServer
Hello!
Is there a way to send a message to a Genserver process such that the message skips all other messages in the genserver’s message queue, and will be the next message to be processed?
Or would the solution involve using a custom queue in front of the genserver worker that exposes an API to achieve this?
Thanks!
Marked As Solved
axelson
For anyone that comes here from search like I have, Erlang/OTP 28 now supports this: Erlang/OTP 28 Highlights - Erlang/OTP
Priority Messages
Sometimes, it is important for urgent messages to skip the queue and be read by the receiving process as soon as possible. Erlang/OTP 28 introduces priority messages, an opt-in mechanism that allows the receiving process to let certain messages get priority status.
I haven’t tried to use this from Elixir yet, but it ought to work.
Also Liked
cmkarlsson
You can use something called a selective receive. The basic idea is to just pattern match on the priority message first and the rest of them later. There are some drawbacks doing this especially for large mailboxes. Also note, that you must do this in your own processes and can’t use GenServer behaviour (as far as I know).
Some links I found.
- https://medium.com/@ErlangSolutions/receiving-messages-in-elixir-or-a-few-things-you-need-to-know-in-order-to-avoid-performance-issues-2b3ce2bf84d8
- More On Multiprocessing | Learn You Some Erlang for Great Good! (erlang code)
- Side Notes: Erlang explained: Selective receive (erlang code)
If you google for selective receive you may found more information.
An example:
def check_important_messages_first() do
receive do
{:important, message} ->
do_something_imporant(message)
after 0 ->
receive do
{:important, message} ->
do_something_important(message);
{:normal, message} ->
do_something_else(message)
end
end
end
jgonet
I dunno efficiency of this solution, but here’s idea: receive all messages and store it in priority queue, then process them one by one from this queue. Higher priority events will appear first to be processed.
OvermindDL1
GenServer’s process in order, that is the purpose of their design.
There are alternative GenServer’s out in the erlang world (and thus useable via Elixir) that are Priority based genservers, so you can send messages and process them in different orders (first by priority then by received order).
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








