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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews