QuickBEAM.Pool (QuickBEAM v0.10.5)

Copy Markdown View Source

A pool of JS runtimes for concurrent request handling.

Each runtime is initialized once with your setup code, then checked out for individual requests. After each use, the runtime is reset and re-initialized — giving you a clean slate with the setup already applied.

Example

{:ok, pool} = QuickBEAM.Pool.start_link(
  size: 10,
  init: fn rt ->
    QuickBEAM.eval(rt, File.read!("priv/js/app.js"))
  end
)

html = QuickBEAM.Pool.run(pool, fn rt ->
  {:ok, result} = QuickBEAM.call(rt, "renderPage", [assigns])
  result
end)

Options

  • :size — number of runtimes in the pool (default: 10)
  • :name — optional registered name for the pool
  • :init — function called on each runtime after creation and after each reset. Receives the runtime pid. Use it to load your JS code.
  • :lazy — if true, runtimes are created on first checkout (default: false)

All other options are forwarded to QuickBEAM.start/1 (:handlers, :memory_limit, :max_stack_size).

Summary

Functions

Check out a runtime, run the given function, and check it back in.

Functions

run(pool, fun, timeout \\ 5000)

@spec run(GenServer.server(), (QuickBEAM.runtime() -> result), timeout()) :: result
when result: var

Check out a runtime, run the given function, and check it back in.

The runtime is automatically reset and re-initialized after each use.

QuickBEAM.Pool.run(pool, fn rt ->
  {:ok, val} = QuickBEAM.eval(rt, "1 + 2")
  val
end)

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()