<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="90763" data-post-id="90763">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="elixirnewbie" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  elixirnewbie
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/peerreynders" rel="nofollow">@peerreynders</a>  thanks once again</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="90763" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-90763" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="90763"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="91048" data-post-id="91048">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jeffweiss" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeffweiss/120/946_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jeffweiss
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="elixirnewbie" data-post="6" data-topic="15513">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/e/db5fbb/48.png" class="avatar"> elixirnewbie:</div>
<blockquote>
<p>they don’t accept new messages if the message count is over 50 in their respective queues and send a reply back to the caller …something like <code>{:error, :queue_full}</code></p>
</blockquote>
</aside>
<aside class="quote no-group" data-username="peerreynders" data-post="8" data-topic="15513">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/48/5826_2.png" class="avatar"> peerreynders:</div>
<blockquote>
<p>a process can’t stop accepting <em>messages</em> ; it can only stop accepting <em>work</em> which would be tracked inside the process state.</p>
</blockquote>
</aside>
<p>If you proceed down this path and wish to return an <code>{:error, :queue_full}</code> you probably don’t want to do it from the <code>handle_call</code>. 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 <code>handle_call</code> if will affect the response to the message <em>at the head</em> of the queue.</p>
<p>In sandwich shop terms (because I’m hungry right now), putting the check and error response in <code>handle_call</code> 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 <em>think</em> what you want is a bouncer at the door that says, “I’m sorry. The line can’t extend outside.”</p>
<p>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 <code>handle_call</code>.</p>
<p>Here’s a quick proof of concept</p>
<pre><code>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 &gt; 5 -&gt;
        {:error, :queue_full}
      _ -&gt;
        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 &lt;- 1..10 do
  Task.async(fn -&gt; OverloadedGenServer.add_num(pid, n) end)
end
|&gt; Enum.map(&amp;Task.await/1)
|&gt; IO.inspect

Process.exit(pid, :kill)
</code></pre>
<p>with output</p>
<pre><code>$ 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&lt;0.73.0&gt;) killed
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="91048" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-91048" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="91048"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="91053" data-post-id="91053">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="elixirnewbie" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  elixirnewbie
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks <a class="mention" href="/u/jeffweiss" rel="nofollow">@jeffweiss</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="91053" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-91053" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="91053"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="251628" data-post-id="251628">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="boilercoding" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  boilercoding
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/jeffweiss" rel="nofollow">@jeffweiss</a> <a class="mention" href="/u/peerreynders" rel="nofollow">@peerreynders</a> 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?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="251628" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-251628" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="251628"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="251636" data-post-id="251636">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="msimonborg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/msimonborg/120/26638_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  msimonborg
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi <a class="mention" href="/u/boilercoding" rel="nofollow">@boilercoding</a> , 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?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="251636" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-251636" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="251636"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="251733" data-post-id="251733">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="boilercoding" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  boilercoding
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hello <a class="mention" href="/u/msimonborg" rel="nofollow">@msimonborg</a> 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:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">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 -&gt;
  GenServer.call(pid, :hi)
  :ok
end

(for _ &lt;- 1..14, do: Task.async(f))
|&gt; Enum.map(&amp;Task.await(&amp;1))
</code></pre>
<p>with output</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">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
</code></pre>
<p>But if I block as following:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">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 -&gt;
  GenServer.call(pid, :hi)
  :ok
end

(for _ &lt;- 1..14, do: Task.async(f))
|&gt; Enum.map(&amp;Task.await(&amp;1))
</code></pre>
<p>Then it works as expected. So my question is why does blocking the process on initialization make it work as expected?</p>
<p>Thanks!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="251733" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-251733" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="251733"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="251737" data-post-id="251737">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Nicd" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Nicd/120/2357_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Nicd
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="boilercoding" data-post="17" data-topic="15513">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/b/838e76/48.png" class="avatar"> boilercoding:</div>
<blockquote>
<p>So my question is why does blocking the process on initialization make it work as expected?</p>
</blockquote>
</aside>
<p>What do you mean by “as expected”? How do you want it to work?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="251737" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-251737" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="251737"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="251779" data-post-id="251779">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="boilercoding" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  boilercoding
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks for replying <a class="mention" href="/u/nicd" rel="nofollow">@Nicd</a>, the desired output is the following:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">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
</code></pre>
<p>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.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="251779" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-251779" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="251779"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="251783" data-post-id="251783">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="msimonborg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/msimonborg/120/26638_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  msimonborg
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You have 16 processes at work, each one with a job to do and each one can do its job asynchronously.</p>
<p>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.</p>
<p>Once your gen_server is already doing its job waiting for messages, the caller starts up 14 new processes <em>one by one</em>, 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.</p>
<p>By the time your gen_server is processing its first message there are already 5 in the queue, but <em>not 14</em>, 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 <code>1</code> with each new message it processes.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="251783" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-251783" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="251783"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="251785" data-post-id="251785">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="boilercoding" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  boilercoding
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks a lot <a class="mention" href="/u/msimonborg" rel="nofollow">@msimonborg</a> it makes more sense now.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="251785" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/message-queue-length-of-a-genserver/15513/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-251785" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="251785"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>