derek-zhou

derek-zhou

Is there any easy way to coalesce messages in the mailbox?

Right now I am using PubSub to send messages to a dedicated genServer, which will handle them in order. The handling of a message may take some time, and in the mean time a newer, but otherwise same message could come in. My message handing is idempotent so some amount of duplication is ok; However, it is still a waste of computation. Can I peek ahead in my mailbox during handle_info/2, and cancel all pending messages that match a certain pattern?

Marked As Solved

dominicletz

dominicletz

Creator of Elixir Desktop

I’ve done selective receive with after 0 a couple of times to batch messages. But you can also de-duplicate. e.g.

defmodule Dedup do
  def example_process() do
    Process.sleep(100)
    receive do
      message ->
        dedup(message)
        dosomething(message)
    end

    example_process()
  end

  def dedup(message) do
    receive do
      ^message ->
        dedup(message)
    after
      0 -> :ok
    end
  end

  defp dosomething(message) do
    IO.puts("Got #{inspect(message)}")
  end
end

You probably only need the dedup() method. The rest is just to run the example. Every number will be printed only once even though there are a total of 100 messages sent.

iex(1)> pid = spawn_link &Dedup.process/0
#PID<0.383.0>
iex(2)> for i <- 1..100, do: send(pid, div(i, 10))
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, ...]
Got 0
Got 1   
Got 2   
Got 3   
Got 4   
Got 5   
Got 6   
Got 7   
Got 8   
Got 9   
Got 10  
iex(3)> 

Also Liked

hauleth

hauleth

You can do selective receive, but that will take longer with growth of the message queue. Instead I would add some idempotency token to the message and use that to reject the messages that were already processed, so instead of rejecting messages that you have already computed during computing them, discard them as soon as these arrive.

Where Next?

Popular in Questions 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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
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
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

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement