From f0dc1071cd7f651c87a22dbf716c9e4f9fb7e3ad Mon Sep 17 00:00:00 2001 From: benoitc Date: Fri, 18 Apr 2014 09:44:51 +0200 Subject: [PATCH] add an example to test multiple connections over a proxy. --- examples/test_multiple_proxy.erl | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 examples/test_multiple_proxy.erl diff --git a/examples/test_multiple_proxy.erl b/examples/test_multiple_proxy.erl new file mode 100755 index 00000000..48794274 --- /dev/null +++ b/examples/test_multiple_proxy.erl @@ -0,0 +1,52 @@ +#!/usr/bin/env escript +%% -*- erlang -*- +%%! -pa ./ebin -pa ./deps/mimetypes/ebin +%% +%% +%% +%% +-define(MAX, 100). +-define(PROXY_OPTS, {"127.0.0.1", 8888}). + +wait_request() -> + receive + start -> ok + end. + +request(WaitPid) -> + wait_request(), + Method = get, + Url = <<"https://friendpaste.com">>, + Headers = [{<<"Connection">>, <<"keep-alive">>}], + Options = [{pool, default}, {proxy, ?PROXY_OPTS}], + {ok, _, _Headers, Ref} = hackney:request(Method, Url, Headers, <<>>, Options), + {ok, _} = hackney:body(Ref), + WaitPid ! ack. + +wait(100) -> + io:format("~n", []), + ok; +wait(N) -> + receive + ack -> + io:format(".", []), + wait(N+1) + after 15000 -> + PoolSize = hackney_pool:count(default), + io:format("~ntimeout ~p/~p done. ~p in the pool.~n", + [N, ?MAX, PoolSize]) + end. + + +main(_) -> + hackney:start(), + hackney_pool:start_pool(default, []), + io:format("processing ", []), + Self = self(), + Pids = lists:foldr(fun(_, Acc) -> + Pid = spawn(fun() -> timer:sleep(100), request(Self) end), + [Pid | Acc] + end, [], lists:seq(1, ?MAX)), + + [Pid ! start || Pid <- Pids], + wait(0).