Skip to content

Instantly share code, notes, and snippets.

@sasa1977
Last active June 30, 2018 05:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sasa1977/8184874 to your computer and use it in GitHub Desktop.
Save sasa1977/8184874 to your computer and use it in GitHub Desktop.
poolboy demo
defmodule HttpRequester do
use GenServer.Behaviour
def start_link(_) do
:gen_server.start_link(__MODULE__, nil, [])
end
def fetch(server, url) do
:gen_server.cast(server, {:fetch, url})
end
def init(_) do
{:ok, nil}
end
def handle_cast({:fetch, url}, state) do
IO.puts "fetching #{url}"
:timer.sleep 5000
IO.puts "fetched #{url}"
{:noreply, state}
end
end
defmodule PoolboyDemo do
def run do
{:ok, pool} = :poolboy.start_link(
[worker_module: HttpRequester, size: 5, max_overflow: 0]
)
Enum.each(1..20, fn(n) ->
:poolboy.transaction(pool, fn(http_requester_pid) ->
HttpRequester.fetch(http_requester_pid, "url #{n}")
end)
end)
end
end
@henrik
Copy link

henrik commented Sep 18, 2015

Forked this to make it runnable in Elixir 1.0, and to make it run the poolboy workers concurrently instead of serially: https://gist.github.com/henrik/ceede9c4d9bf3fcb4dd5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment