Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment