apr

apr

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!

Showing Posts 13 to 4

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.

peerreynders

peerreynders

Alternately promote the priority of messages-in-waiting in order to return most, if not all responses “sometime within the timeout”- which means “managing priority” becomes much more complicated and is tightly coupled to what the GenServer is actually doing.

OvermindDL1

OvermindDL1

Not necessarily, but you’d need to have both cast’s and call’s return immediately with a 0-sleep, so that all messages will be received and buffered and then you can handle a given message in the 0-sleep timeout function.

In general absolutely this, but there are definite use-cases where a priority queue is very important.

You don’t have to return a message to a call immediately, you are just supposed to do it ‘sometime within the timeout’. :slight_smile:

peerreynders

peerreynders

http://www1.erlang.org/pipermail/erlang-questions/2005-October/017520.html

i.e.

def handle(state),
  do: handle([:high, :medium, :low], state).
    
def handle([], state) do
    receive do
      {_priority, msg} ->
        do_it(msg, state)
    end
end

def handle([priority | tail], state) do
    receive do
      {priority, msg} ->
        do_it(msg, state)
    after 0 ->
      handle(tail, state)
    end
 end

def do_it(msg, state) do
  # ...

  handle(state)
end

:icon_biggrin:

A normal process can send messages to a GenServer - it would just mean that the priority management is in a separate process (which really shouldn’t be a big deal - Tasks for example are raw processes.) If the priority management process uses a blocking call/3 to the GenServer it wouldn’t select the next message from the process mailbox until the call returns.

NobbZ

NobbZ

Creating and managing a priority queue which you update on new messages is probably more expensive than just processing the messages in order.

apr

apr OP

So @OvermindDL1, you’d consider managing priority state explicitly via queues stored in GenServer state bad practice? I want to use elixir modules as much as I can and managing priority manually doesn’t seem too hard.

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

apr

apr OP

I couldn’t find any in particular, so I decided to use this: GitHub - discord/deque: Fast bounded deque using two rotating lists. · GitHub to simulate 3 queues with 3 priorities and store them in the GenServer’s state.

jgonet

jgonet

Popcorn Core Team

Unfortunately, no. That was just abstract idea, but I’m sure there’s library for that.

apr

apr OP

So you are suggesting the custom queue in front of the worker approach? I think that is probably the way to go. Are you aware of any good libraries that tackle this use case?

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews