Skip to content

Commit 83d32b3

Browse files
committed
Allow setting recv_timeout when sending request
1 parent 0901d6d commit 83d32b3

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/gleam/hackney.gleam

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,36 @@ fn ffi_send(
2121
b: String,
2222
c: List(http.Header),
2323
d: BytesTree,
24+
e: Int,
2425
) -> Result(Response(BitArray), Error)
2526

2627
// TODO: test
2728
pub fn send_bits(
2829
request: Request(BytesTree),
30+
timeout: Int,
2931
) -> Result(Response(BitArray), Error) {
3032
use response <- result.then(
3133
request
3234
|> request.to_uri
3335
|> uri.to_string
34-
|> ffi_send(request.method, _, request.headers, request.body),
36+
|> ffi_send(request.method, _, request.headers, request.body, timeout),
3537
)
3638
let headers = list.map(response.headers, normalise_header)
3739
Ok(Response(..response, headers: headers))
3840
}
3941

4042
pub fn send(req: Request(String)) -> Result(Response(String), Error) {
43+
send_with_timeout(req, 8000)
44+
}
45+
46+
pub fn send_with_timeout(
47+
req: Request(String),
48+
timeout: Int,
49+
) -> Result(Response(String), Error) {
4150
use resp <- result.then(
4251
req
4352
|> request.map(bytes_tree.from_string)
44-
|> send_bits,
53+
|> send_bits(timeout),
4554
)
4655

4756
case bit_array.to_string(resp.body) {

src/gleam_hackney_ffi.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
-module(gleam_hackney_ffi).
22

3-
-export([send/4]).
3+
-export([send/5]).
44

5-
send(Method, Url, Headers, Body) ->
6-
Options = [{with_body, true}],
5+
send(Method, Url, Headers, Body, Timeout) ->
6+
Options = [{with_body, true}, {recv_timeout, Timeout}],
77
case hackney:request(Method, Url, Headers, Body, Options) of
88
{ok, Status, ResponseHeaders, ResponseBody} ->
99
{ok, {response, Status, ResponseHeaders, ResponseBody}};

0 commit comments

Comments
 (0)