TimoMoss

TimoMoss

Hello, in our Phoenix API, we’re making numerous calls to external APIs like Opena Ai, with long responses that can take 2 to 15 seconds.
As an HTTP client, we use Req {:req, "~> 0.5.2"}, when the number of user requests to our API reaches approximately 20 per second, hence the same number of calls we’re doing to the external API we’re getting the following error: (RuntimeError) Finch was unable to provide a connection within the timeout due to excess queuing for connections. Consider adjusting the pool size, count, timeout, or reducing the rate of requests if it is possible that the downstream service is unable to keep up with the current rate.

I played with Finch opts, in the Application supervision children:

children = [
      {Finch,
             name: MyConfiguredFinch,
             pools: %{
               :default => [
                 size: 1000,
                 count: 200,
                 pool_max_idle_time: 120_000,
                 conn_max_idle_time: 120_000
               ]
             }},
               ....
   ]

but seems it doesn’t help
I would highly appreciate any help since this happening in the production

Showing Posts 1 to 7

oliveiragahenrique

oliveiragahenrique

Can you confirm that the upstream service is healthy and capable of handling your load?

Keep in mind that Finch connections are lazy—they only initiate new connections when needed. If the upstream service is degraded at that time, try to open a new connection may result in a timeout.

TimoMoss

TimoMoss OP

may I please clarify by the upstream service you mean the external API to which Req makes requests? If so, yes time to time they can respond with 429 but not that often. from 1200 requests per minute only 600 were handled, for all others I got the mentioned above error.
Is there a way to make connections eager?

oliveiragahenrique

oliveiragahenrique

Yes! I was referring to the external API that Req makes requests to.

Given the occasional 429 responses, it seems likely the issue is related to the service being unable to establish a new connection when Finch requires a new one (due to lazy initialization).

To confirm, you could try lowering the pool size and pool count (perhaps even setting it to 1) and see if the error persists. If this is indeed the issue, you’ll likely see timeouts, but the connection pool should remain functional and not raise this error.

TimoMoss

TimoMoss OP

Thank you very much Gabriel
Is the following setup correct in general? And also pull size 1, does it slow down the performance?

oliveiragahenrique

oliveiragahenrique

Seems correct! But it for sure slow down the performance and the way it is right now it would apply for all services called by this MyConfiguredFinch instance.

So, I would suggest 2 things:

  • Create a configuration for the specific service you are having trouble with.
  • Do not decrease it to 1 on the first try, but measure and see

Somehting like this:

children = [
      {Finch,
             name: MyConfiguredFinch,
             pools: %{
               :default => # Keep as it is
               "https://your-faulty-service.com" => [size: 10, count: 1]
             }},
TimoMoss

TimoMoss OP

It helped but not totally, less but still getting the same error, is there buy chance a way to catch this particular error? I can see only RuntimeError without any additional markers

oliveiragahenrique

oliveiragahenrique

You could probaly match on it’s message on a catch/2

My suggestion now would be increasing :pool_timeout option on Finch request. See Finch — Finch v0.23.0

Since some of your requests take a long time (eg. 15 seconds) you may end up starving the pool.

One way to measure it is to start pool metrics on finch with the option :start_pool_metrics? on Finch start_link. See Finch — Finch v0.23.0

And then run a periodically job that will execute Finch.pool_status/2 and gather this metrics so you can check if your pool is starving or not.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews