Limit request total timeout to just 2000ms with Req

How do we limit a Req.post! or Req.get! to a total timeout of just 2000ms?

I have an integration that needs very timely responses.
Limiting external API requests to just 2000ms would help my integration scenario.

I have looked at the documentation, but I’m not getting it at all.
As usual, AI suggestions are no use either.

suggested code - Via WhatsApp Meta AI Llama 3.2

def test do
    Req.get!("https://api.github.com/repos/wojtekmach/req",
      finch: [
        options: [
          timeout: 2000,
          receive_timeout: 2000
        ]
      ]
    )
  end

this seems to limit just the connection part, but not the entire request duration

Req.get!("https://httpbin.org", connect_options: [timeout: 2000])

Could it work to just spawn a task that just times out after that amount of time?

1 Like

If I Cannot get the Req to behave as I expect it to, then your idea is indeed solid.

Thanks.

I think the Finch option you want is :request_timeout:

The amount of time to wait for a complete response before returning an error. This timeout only applies to HTTP/1, and its current implementation is a best effort timeout, it does not guarantee the call will return precisely when the time has elapsed. Default value is :infinity .

And if you’re using HTTP/2 then that won’t work.

And the syntax to set the various timeouts looks like:

Req.get("https://httpbin.org", connect_options: [timeout: 500], receive_timeout: 2000, pool_timeout: 500)

The main place to see those options is at: Req — req v0.5.6

But I’m pretty sure those various timeouts all stack so if you want to limit the total time I agree with @hilko that a Task is your best bet.

2 Likes

I added retry: false as well, so far the receive_timeout: 2000 seems okay

1 Like