Skip to content
This repository
Browse code

add recv_timeout option

  • Loading branch information...
commit 5aff16529ef351b9696d711d889aae4b7f35bd95 1 parent efd877f
Benoit Chesneau authored November 12, 2012
3  doc/hackney.md
Source Rendered
@@ -197,6 +197,9 @@ redirection even on POST
197 197
 * insecure: to perform "insecure" SSL connections and
198 198
 transfers without checking the certificate
199 199
 
  200
+* {recv_timeout, infinity | integer()}: timeout used when
  201
+receiving a connection. Default is infinity
  202
+
200 203
 
201 204
 
202 205
 
2  doc/hackney_request.md
Source Rendered
@@ -57,5 +57,5 @@
57 57
 ###stream_body/2##
58 58
 
59 59
 
60  
-`stream_body(Body, Client) -> any()`
  60
+`stream_body(Func, Client) -> any()`
61 61
 
2  include/hackney.hrl
@@ -42,7 +42,7 @@
42 42
         netloc = netloc,
43 43
         options = [],
44 44
         socket = nil,
45  
-        timeout = infinity,
  45
+        recv_timeout = infinity,
46 46
         follow_redirect = false,
47 47
         max_redirect = 5,
48 48
         force_redirect = false,
12  src/hackney.erl
@@ -80,7 +80,9 @@ connect(Transport, Host, Port, #client{socket=Skt, options=Opts}=Client)
80 80
             socket_from_pool(Pool, {Transport, Host, Port}, Client)
81 81
     end;
82 82
 connect(Transport, Host, Port, Options) when is_list(Options) ->
83  
-    connect(Transport, Host, Port, #client{options=Options}).
  83
+    Timeout = proplists:get_value(recv_timeout, Options, infinity),
  84
+    connect(Transport, Host, Port, #client{recv_timeout=Timeout,
  85
+                                           options=Options}).
84 86
 
85 87
 %% @doc close the client
86 88
 close(Client) ->
@@ -159,6 +161,8 @@ request(Method, URL, Headers, Body) ->
159 161
 %%          <li>{proxy, proxy_options()}: to connect via a proxy.</li>
160 162
 %%          <li>insecure: to perform "insecure" SSL connections and
161 163
 %%          transfers without checking the certificate</li>
  164
+%%          <li>{recv_timeout, infinity | integer()}: timeout used when
  165
+%%          receiving a connection. Default is infinity</li>
162 166
 %%      </ul>
163 167
 %%
164 168
 %%      </li>
@@ -332,13 +336,15 @@ maybe_proxy(Transport, Host, Port, Options)
332 336
             connect(Transport, Host, Port, Options)
333 337
     end.
334 338
 
335  
-connect_proxy(ProxyUrl, Host, Port, ProxyOpts, Options) ->
  339
+connect_proxy(ProxyUrl, Host, Port, ProxyOpts0, Options) ->
336 340
     Host = iolist_to_binary([Host, ":", integer_to_list(Port)]),
337 341
     Headers = [{<<"Host">>, Host}],
  342
+    Timeout = proplists:get_value(recv_timeout, Options, infinity),
  343
+    ProxyOpts = [{recv_timeout, Timeout} | ProxyOpts0],
338 344
     case request(connect, ProxyUrl, Headers, <<>>, ProxyOpts) of
339 345
         {ok, 200, _, Client0} ->
340 346
             Client = skip_body(Client0),
341  
-            {ok, Client#client{options=Options}};
  347
+            {ok, Client#client{recv_timeout=Timeout, options=Options}};
342 348
         {ok, S, H, Client} ->
343 349
             Body = body(Client),
344 350
             {error, {proxy_connection, S, H, Body}};
9  src/hackney_response.erl
@@ -156,9 +156,8 @@ stream_body(Client=#client{body_state=done}) ->
156 156
 
157 157
 -spec stream_body_recv(#client{})
158 158
 	-> {ok, binary(), #client{}} | {error, atom()}.
159  
-stream_body_recv(Client=#client{
160  
-		transport=Transport, socket=Socket, buffer=Buffer}) ->
161  
-	case Transport:recv(Socket, 0, 5000) of
  159
+stream_body_recv(Client=#client{buffer=Buffer}) ->
  160
+	case recv(Client) of
162 161
 		{ok, Data} -> transfer_decode(<< Buffer/binary, Data/binary >>,
163 162
                                 Client);
164 163
 		{error, Reason} -> {error, Reason}
@@ -323,8 +322,8 @@ te_identity(Data, {Streamed, Total}) ->
323 322
 ce_identity(Data) ->
324 323
 	{ok, Data}.
325 324
 
326  
-recv(#client{transport=Transport, socket=Skt}) ->
327  
-    Transport:recv(Skt, 0).
  325
+recv(#client{transport=Transport, socket=Skt, recv_timeout=Timeout}) ->
  326
+    Transport:recv(Skt, 0, Timeout).
328 327
 
329 328
 
330 329
 close(#client{socket=nil}=Client) ->

0 notes on commit 5aff165

Please sign in to comment.
Something went wrong with that request. Please try again.