38 lines
1.2 KiB
Erlang
38 lines
1.2 KiB
Erlang
-module(requestWorker).
|
|
-export([request/1, loop_request/2]).
|
|
|
|
%% @spec request(Url::string()) -> void()
|
|
%% @doc Make a request to the given url and record the response time.
|
|
%% The result it's sent to the statistician process.
|
|
request(Url) ->
|
|
Begin = os:system_time(),
|
|
Response = httpc:request(Url),
|
|
End = os:system_time(),
|
|
case Response of
|
|
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, _Body}} ->
|
|
statistician ! {ok, Begin, End};
|
|
{ok, {{_Version, 404, _ReasonPhrase}, _Headers, _Body}} ->
|
|
io:format("Error 404: ~p~n", [Url]);
|
|
{ok, {{_Version, Result, _ReasonPhrase}, _Headers, _Body}} ->
|
|
io:format("Error: ~p~nUrl: ~p~n", [Result, Url]),
|
|
statistician ! {error, Begin, End};
|
|
{error, Reason} ->
|
|
io:format("Error: ~p~n", [Reason]),
|
|
statistician ! {connection_error, Begin, End}
|
|
end.
|
|
|
|
%% @spec loop_request(Urls::[string()], Rate::float()) -> void()
|
|
%% @doc Make a request, wait using the Rate and then proceed with the next
|
|
%% request.
|
|
loop_request(Urls, Rate) ->
|
|
case Urls of
|
|
[] -> ok;
|
|
[Url | RemainingUrls] ->
|
|
request(Url),
|
|
receive
|
|
{stop} -> ok
|
|
after requester:exponential_time(Rate) ->
|
|
loop_request(RemainingUrls, Rate)
|
|
end
|
|
end.
|