Limit number of concurrent requests to host, with telsa

Hello, I’m using the tesla library to send http requests to a host. However I’m supposed to keep the amount of concurrent requests somewhat low.

To achieve this I used poolboy. The problem with this approach however is that I have to use mock_global during testing, which is not a problem for now, but could become one later on.

Is there a better alternative to this approach? My current telsa adapter is hackney, but it doesn’t matter, if I have to switch.

Take a look at one of the following open source libraries which can be used to rate limit requests.

You could use the external host name as the rate limit identity. Writing your own Tesla middleware to apply the rate limit might be a good approach. Take a look at the existing fuse middleware for inspiration.

6 Likes

Just checking to see if there are any new opinions on this topic. :wave:

EDIT: Came across this other post, which recommends a couple resources:

Follow-up: It turns out the solution I needed is built into Hackney.

To limit the maximum number of connections for a given Tesla Client (using the Hackney adapter), you can just configure Hackney’s pool and max_connections options when configuring the adapter:

  adapter({
    Tesla.Adapter.Hackney,
    pool: :some_hackney_pool,
    max_connections: 10
  })

Seems to work as expected so far. Each pool appears to work independently of the others. :slight_smile:

To provide a bit of follow-up context from what I learned while doing this:

  • This works by (ab)using the connection pool feature built into Hackney. (TLDR: HTTP connection pools save time and resources by re-using HTTP connections, which saves time doing DNS resolution, negotiating SSL/TLS security, etc.)

  • Because this is not what connection pools are intended for, it is is definitely a “hack” (although I found it to be a pretty effective one).

  • If you (ab)use Hackney’s connection pool settings to do this, you should probably set timeout: 0 when configuring the Hackney adapter to ensure that no connections are actually kept alive.