-
-
Save benjamintanweihao/0603f8005a8242c3cc53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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