elixirnewbie
Message queue length of a GenServer
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.
Most Liked
blatyo
How are you sending messages to that process?
I notice that you’re doing this in a handle_call which is synchronous. So, if you only have one process doing a GenServer.call to this process it’ll block until it gets a response. That means, you’ll never have a queue length, because this handle_call is consuming the only message that has been sent.
In short, what you’re doing is correct, but the way you’re testing it isn’t.
stefanchrobot
Sounds like you want to control the ingestion of data in your system. If that’s the case, rather than fiddling with the queue length, I’d suggest taking a look at GenStage.
peerreynders
That is irrelevant given that the process can only control removal of messages from the mailbox and nothing else - i.e. a process can’t stop accepting messages; it can only stop accepting work which would be tracked inside the process state.
something like
{:error, :queue_full}
a response like that happens when a received message is requesting more work but your work backlog indicates that you already have enough - i.e. the response should have nothing to do with messages remaining in the process mailbox but everything with information you currently have in the process state.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









