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 1 to 10

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
apr

apr OP

Thanks for the helpful links @cmkarlsson! I should have mentioned though that the process I was referring to was indeed a genserver :frowning: I will update my OP to reflect this.

jgonet

jgonet

Popcorn Core Team

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.

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?

jgonet

jgonet

Popcorn Core Team

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

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.

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

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.

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.

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.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews