Skip to content

Commit c7bae44

Browse files
committed
Allow setting recv_timeout when sending request
1 parent d8e96bb commit c7bae44

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/gleam/hackney.gleam

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

2627
// TODO: test
2728
pub fn send_bits(
2829
request: Request(BytesBuilder),
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(req: Request(String), timeout: Int) -> Result(Response(String), Error) {
4147
use resp <- result.then(
4248
req
4349
|> request.map(bytes_builder.from_string)
44-
|> send_bits,
50+
|> send_bits(timeout),
4551
)
4652

4753
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)