apr

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

axelson

Scenic Core Team

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

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.

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

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

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).

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement