Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed timeout when using option stream_to

  • Loading branch information...
commit a28355f1d7e729f826db4b1cf645c7dc631efe31 1 parent dcb3448
@lpgauth lpgauth authored
View
5 src/lhttpc.erl
@@ -325,7 +325,8 @@ request(Host, Port, Ssl, Path, Method, Hdrs, Body, Timeout, Options) ->
true ->
StreamTo = proplists:get_value(stream_to, Options),
Args = [ReqId, StreamTo, Host, Port, Ssl, Path, Method, Hdrs, Body, Options],
- Pid = spawn_link(lhttpc_client, request, Args),
+ Pid = spawn(lhttpc_client, request_with_timeout, [Timeout, Args]),
+ _TimerRef = erlang:send_after(Timeout, lhttpc_manager, {kill_client_after_timeout, ReqId, Pid, StreamTo}),
{ReqId, Pid};
false ->
Args = [ReqId, self(), Host, Port, Ssl, Path, Method, Hdrs, Body, Options],
@@ -557,8 +558,6 @@ verify_options([{max_connections, MS} | Options], Errors)
verify_options([{stream_to, Pid} | Options], Errors)
when is_pid(Pid) ->
verify_options(Options, Errors);
-verify_options([{stream_to, undefined} | Options], Errors) ->
- verify_options(Options, Errors);
verify_options([{partial_upload, WindowSize} | Options], Errors)
when is_integer(WindowSize), WindowSize >= 0 ->
verify_options(Options, Errors);
View
7 src/lhttpc_client.erl
@@ -32,7 +32,7 @@
%%% @end
-module(lhttpc_client).
--export([request/10]).
+-export([request/10, request_with_timeout/2]).
-include("lhttpc_types.hrl").
@@ -63,6 +63,11 @@
-define(CONNECTION_HDR(HDRS, DEFAULT),
string:to_lower(lhttpc_lib:header_value("connection", HDRS, DEFAULT))).
+request_with_timeout(Timeout, [ReqId, StreamTo, _Host, _Port, _Ssl, _Path, _Method, _Hdrs, _Body, _Options] = Args) ->
+ TimerRef = erlang:send_after(Timeout, lhttpc_manager, {kill_client_after_timeout, ReqId, self(), StreamTo}),
+ ok = apply(?MODULE, request, Args),
+ erlang:cancel_timer(TimerRef).
+
-spec request(tuple(), pid(), string(), 1..65535, true | false, string(),
string() | atom(), headers(), iolist(), [option()]) -> no_return().
%% @spec (ReqId, From, Host, Port, Ssl, Path, Method, Hdrs, RequestBody, Options) -> ok
View
8 src/lhttpc_manager.erl
@@ -82,6 +82,14 @@ handle_cast(_, State) ->
%% @hidden
-spec handle_info(any(), #httpc_man{}) -> {noreply, #httpc_man{}}.
+handle_info({kill_client_after_timeout, ReqId, Pid, StreamTo}, State) ->
+ case erlang:is_process_alive(Pid) of
+ true ->
+ exit(Pid, kill),
+ StreamTo ! {response, ReqId, Pid, {error, timeout}};
+ false -> ok
+ end,
+ {noreply, State};
handle_info(_, State) ->
{noreply, State}.
Please sign in to comment.
Something went wrong with that request. Please try again.