Skip to content

Instantly share code, notes, and snippets.

@benjamintanweihao
Forked from sasa1977/poolboy_demo.ex
Last active August 29, 2015 14:23
Show Gist options
  • Save benjamintanweihao/0603f8005a8242c3cc53 to your computer and use it in GitHub Desktop.
Save benjamintanweihao/0603f8005a8242c3cc53 to your computer and use it in GitHub Desktop.
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