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.
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.
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:
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.