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.
Trending in Questions
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 20 to 11- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
boilercoding
Thanks a lot @msimonborg it makes more sense now.
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
1with each new message it processes.boilercoding
Thanks for replying @Nicd, the desired output is the following:
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
What do you mean by “as expected”? How do you want it to work?
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:
with output
But if I block as following:
Then it works as expected. So my question is why does blocking the process on initialization make it work as expected?
Thanks!
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
@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
Thanks @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 thehandle_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 inhandle_callif 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_callis 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
with output
elixirnewbie
@peerreynders thanks once again