From 83d32b3532dc3d40a12de2e4407aa4455e48c958 Mon Sep 17 00:00:00 2001 From: Felix Date: Fri, 19 Jan 2024 16:23:07 +0100 Subject: [PATCH] Allow setting recv_timeout when sending request --- src/gleam/hackney.gleam | 13 +++++++++++-- src/gleam_hackney_ffi.erl | 6 +++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/gleam/hackney.gleam b/src/gleam/hackney.gleam index a0effb7..479b76d 100644 --- a/src/gleam/hackney.gleam +++ b/src/gleam/hackney.gleam @@ -21,27 +21,36 @@ fn ffi_send( b: String, c: List(http.Header), d: BytesTree, + e: Int, ) -> Result(Response(BitArray), Error) // TODO: test pub fn send_bits( request: Request(BytesTree), + timeout: Int, ) -> Result(Response(BitArray), Error) { use response <- result.then( request |> request.to_uri |> uri.to_string - |> ffi_send(request.method, _, request.headers, request.body), + |> ffi_send(request.method, _, request.headers, request.body, timeout), ) let headers = list.map(response.headers, normalise_header) Ok(Response(..response, headers: headers)) } pub fn send(req: Request(String)) -> Result(Response(String), Error) { + send_with_timeout(req, 8000) +} + +pub fn send_with_timeout( + req: Request(String), + timeout: Int, +) -> Result(Response(String), Error) { use resp <- result.then( req |> request.map(bytes_tree.from_string) - |> send_bits, + |> send_bits(timeout), ) case bit_array.to_string(resp.body) { diff --git a/src/gleam_hackney_ffi.erl b/src/gleam_hackney_ffi.erl index ca01f15..9388f3c 100644 --- a/src/gleam_hackney_ffi.erl +++ b/src/gleam_hackney_ffi.erl @@ -1,9 +1,9 @@ -module(gleam_hackney_ffi). --export([send/4]). +-export([send/5]). -send(Method, Url, Headers, Body) -> - Options = [{with_body, true}], +send(Method, Url, Headers, Body, Timeout) -> + Options = [{with_body, true}, {recv_timeout, Timeout}], case hackney:request(Method, Url, Headers, Body, Options) of {ok, Status, ResponseHeaders, ResponseBody} -> {ok, {response, Status, ResponseHeaders, ResponseBody}};