Errata: Don't use cast in a poolboy transaction

So in episode 092 I introduced poolboy. Sadly, I suggested a bit of an anti-pattern as far as I can tell. In it, I introduced the following code:

  def inject(message) do
    :poolboy.transaction(:link_extractor_pool, fn(worker) ->
      LinkExtractor.Worker.handle_message(worker, message)
    end)
  end

This part is fine, but basically what it does is: check a worker out of the pool, evaluate the function, then check the worker back into the pool. But let's see what that function does exactly:

  def handle_message(server, message) do
    GenServer.cast(server, {:handle_message, message})
  end

So here we're casting to the GenServer. Of course, a cast will return immediately, so the worker gets checked back in immediately. However, he's busy doing some work. If another request is sent to him, he won't be able to respond yet as he's still busy with the previous request.

The general takeaway is to use a GenServer.call for functions that will be handled inside of a poolboy, so that the workers don't get prematurely checked back into the pool.

Sorry for the mistake, hope this helps. See you soon!