Last active
June 30, 2018 05:39
-
-
Save sasa1977/8184874 to your computer and use it in GitHub Desktop.
poolboy demo
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
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