amnu3387

amnu3387

Hi, I’ve written a pooler using gen_statem, similar to poolboy but with a few differences, more control on how it can be configured (default workers, max workers (similar to overflow in poolboy) and waiting queue lenght), some more control on checking out the worker (immediate overload response, only timeout, unbound, not swallowing timeouts).

I’ve ran some tests with it and poolboy and the results are promising. What I’m wondering is if there’s any other more efficient poolers out there? Perhaps nif based? I’m asking because if not I would like to rewrite it in erlang (it’s mostly compatible with only some keyword lists for initialisation and naming which can be swapped for something else) and publish it publicly (and because poolboy is used in some public elixir libs if it was a better option it would be, well, better?).

They have slightly different semantics on how they control, the things but running poolboy with no “block”, and an :infinity timeout, against the unbound waiting queue, and 5_000 timeout, both with 100 workers, not overflow, results in this:

Interactive Elixir (1.9.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Queuer.Pool.test_queuer()
:ok
Total ok: 40000 - Total failed: 0
time: 1.13
iex(2)> Queuer.Pool.test_poolboy()
:ok
Total ok: 40000 - Total failed: 0
time: 41.639

The results are consistent across all range of workloads, workers, etc.

The tests are basically:

@total_reqs 20000
  
  def test_queuer(max \\ @total_reqs)  do
    pid = Process.whereis(__MODULE__)
    time = :erlang.system_time(:millisecond)
    receiver = spawn(__MODULE__, :keep_track, [time, 0, 0])
    feed_test_queuer(pid, receiver, 0, max)
    feed_test_queuer(pid, receiver, 0, max)
  end

  def feed_test_queuer(pid, receiver, count, max) when count < max do
    spawn(fn() ->
      case __MODULE__.checkout(pid, 5000, false) do
        {:ok, w_pid} ->
          :ok = GenServer.call(w_pid, :test)
          send(receiver, :ok)
          __MODULE__.checkin(pid, w_pid)
        error ->
          send(receiver, :failed)
          :error
      end
    end)
    feed_test_queuer(pid, receiver, count + 1, max)
  end

  def feed_test_queuer(_, _, _, _), do: :ok

  def test_poolboy(max \\ @total_reqs) do
    pid = Process.whereis(:worker)
    time = :erlang.system_time(:millisecond)
    receiver = spawn(__MODULE__, :keep_track, [time, 0, 0])
    feed_test_poolboy(pid, receiver, 0, max)
    feed_test_poolboy(pid, receiver, 0, max)
  end

  def feed_test_poolboy(pid, receiver, count, max) when count < max do
    spawn(fn() ->
      case :poolboy.checkout(pid, true, :infinity) do
        :full ->
          send(receiver, :failed)
          :error
        w_pid ->
          :ok = GenServer.call(w_pid, :test)
          send(receiver, :ok)
          :poolboy.checkin(pid, w_pid)
      end
    end)
    feed_test_poolboy(pid, receiver, count + 1, max)
  end

  def feed_test_poolboy(_, _, _, _), do: :ok
 
   def keep_track(time, c, f) when (c + f) == @total_reqs do
    time_now = :erlang.system_time(:millisecond)
    IO.puts("Total ok: #{c} - Total failed: #{f}")
    IO.inspect(((time_now - time) / 1000), label: "time")
  end
  
  def keep_track(time, count, failed) do
    receive do
      :ok -> keep_track(time, count + 1, failed)
      :failed -> keep_track(time, count, failed + 1)
    after
      10_000 ->
        :ok
    end
  end

In short, checkout a worker, call it for :ok, send a message to the “tracker”.
The worker module is:

defmodule WorkerTest do
  use GenServer

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

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

  def handle_call(:test, _from, state) do
    {:reply, :ok, state}
  end
end

(it has two start_links, because my queuer is passing 0 args to it)
Can you see something wrong with the way I’m testing both implementations?

Showing Posts 1 to 2

hauleth

hauleth

Have you read @ferd’s article about handling overload?

amnu3387

amnu3387 OP

I think so, but the unbound queue here is just to simulate the behaviour of poolboy, the error/overflow control is more fine grained than in poolboy, where checking out the worker is always a max timeout no matter if it’s overloaded or not, and always makes the call (and can only know if it’s overloaded after the call returns or timeouts). In this queuer you can bail out immediately on overload, only bail out after X, bail out after X OR overload, and/or have a 0 max waiting queue, an unbound queue or a finite queue.

[edit] - the timeout is not hardcoded in poolboy duh

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews