elixirnewbie

elixirnewbie

How can I get the message queue length of a GenServer? I have tried

def handle_call({:add,number},_from, state) do
  
  {_,num}=Process.info(self(), :message_queue_len)
 IO.inspect num
  {:reply,reply state}
end

but getting 0.

Showing Posts 20 to 11

boilercoding

boilercoding

Thanks a lot @msimonborg it makes more sense now.

msimonborg

msimonborg

You have 16 processes at work, each one with a job to do and each one can do its job asynchronously.

Your first process is the “caller” of your script or iex terminal, the one that is executing your code. It starts your 2nd process, the gen_server, which can then immediately go about its work asynchronously. Its job is to process messages in its queue and put the queue length to the terminal with each message it receives.

Once your gen_server is already doing its job waiting for messages, the caller starts up 14 new processes one by one, then waits until it receives messages from all of them. All 14 of those processes can do asynchronous work but they start their work in order one at a time. Their job is to immediately start sending messages to your gen_server and wait for its reply.

By the time your gen_server is processing its first message there are already 5 in the queue, but not 14, because it can start doing its job at the same time the caller is doing its job. Messages are piling up faster than the gen_server can process them, but they are finite, and once it catches up the length peaks and declines by 1 with each new message it processes.

boilercoding

boilercoding

Thanks for replying @Nicd, the desired output is the following:

queue length: 13
queue length: 12
queue length: 11
queue length: 10
queue length: 9
queue length: 8
queue length: 7
queue length: 6
queue length: 5
queue length: 4
queue length: 3
queue length: 2
queue length: 1
queue length: 0

I was trying to do a rate limit on an application with the message queue length, so I needed to keep track of the number of requests, now I know that the message queue length is not the best approach but I don’t understand why if I don’t block on the init function then the count of request will not be as I would expect.

Nicd

Nicd

What do you mean by “as expected”? How do you want it to work?

boilercoding

boilercoding

Hello @msimonborg thank you very much for replying to my message. After inspecting the length I was wrong is not actually 0, the code that I’m using is the following:

defmodule Demo do

  defp do_it(state) do
    {_, num} = Process.info(self(), :message_queue_len)
    IO.puts "queue length: #{inspect num}"

    {:reply, :hello, state}
  end

  def init(_args) do
    {:ok, []}
  end

  def handle_call(:hi, _from, state),
    do: do_it(state)

end

{:ok, pid} = GenServer.start_link(Demo,[])
f = fn ->
  GenServer.call(pid, :hi)
  :ok
end

(for _ <- 1..14, do: Task.async(f))
|> Enum.map(&Task.await(&1))

with output

queue length: 5
queue length: 12
queue length: 11
queue length: 10
queue length: 9
queue length: 8
queue length: 7
queue length: 6
queue length: 5
queue length: 4
queue length: 3
queue length: 2
queue length: 1
queue length: 0

But if I block as following:

defmodule Demo do

  defp do_it(state) do
    {_, num} = Process.info(self(), :message_queue_len)
    IO.puts "queue length: #{inspect num}"

    {:reply, :hello, state}
  end

  def init(_args) do
    {:ok, [], {:continue, :block}}
  end

  def handle_call(:hi, _from, state),
    do: do_it(state)

  def handle_continue(:block, state) do
    Process.sleep(1000) # block for one second
    {:noreply, state}
  end

end

{:ok, pid} = GenServer.start_link(Demo,[])
f = fn ->
  GenServer.call(pid, :hi)
  :ok
end

(for _ <- 1..14, do: Task.async(f))
|> Enum.map(&Task.await(&1))

Then it works as expected. So my question is why does blocking the process on initialization make it work as expected?

Thanks!

msimonborg

msimonborg

Hi @boilercoding , it would be helpful if you paste your code and your outputs. Are you copy and pasting the exact code above, or is it altered? Are you inspecting the length to verify that it’s 0?

boilercoding

boilercoding

@jeffweiss @peerreynders or anybody that can know the answer, why if I don’t block the init function with at least Process.sleep(50) the message queue length will always be 0?

elixirnewbie

elixirnewbie OP

Thanks @jeffweiss

jeffweiss

jeffweiss

If you proceed down this path and wish to return an {:error, :queue_full} you probably don’t want to do it from the handle_call. I’m assuming that if there are lots of messages that you want to discard the newest messages: the ones at the end of the queue. By putting the condition in handle_call if will affect the response to the message at the head of the queue.

In sandwich shop terms (because I’m hungry right now), putting the check and error response in handle_call is equivalent to getting in line at a sandwich shop before the lunch rush, waiting a little bit, lunch rush hits, getting to the counter to order and being told, “I’m sorry. I can’t make you a sandwich; the line is too long.” When I think what you want is a bouncer at the door that says, “I’m sorry. The line can’t extend outside.”

If you must implement this yourself rather than using some of the suggestions from other folks, you may wish to put the check in the client function rather than handle_call.

Here’s a quick proof of concept

defmodule OverloadedGenServer do
  use GenServer

  def start_link do
    GenServer.start_link(__MODULE__, [])
  end

  def init(_) do
    {:ok, 0}
  end

  def add_num(pid, num) do
    case Process.info(pid, :message_queue_len) do
      {_, len} when len > 5 ->
        {:error, :queue_full}
      _ ->
        GenServer.call(pid, {:add_num, num})
    end
  end

  def handle_call({:add_num, num}, _from, prior_num) do
    new_num = prior_num + num
    Process.sleep(500)
    {:reply, new_num, new_num}
  end
end

{:ok, pid} = OverloadedGenServer.start_link()

for n <- 1..10 do
  Task.async(fn -> OverloadedGenServer.add_num(pid, n) end)
end
|> Enum.map(&Task.await/1)
|> IO.inspect

Process.exit(pid, :kill)

with output

$ elixir queue_test.exs
[
  1,
  3,
  6,
  10,
  15,
  21,
  {:error, :queue_full},
  {:error, :queue_full},
  {:error, :queue_full},
  {:error, :queue_full}
]
** (EXIT from #PID<0.73.0>) killed
elixirnewbie

elixirnewbie OP

@peerreynders thanks once again

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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews