SaloonPHP now has a Leaky Bucket

SaloonPHP now has a Leaky Bucket
Photo by Lucas van Oort / Unsplash

One of the many things I've been working on lately is adding Leaky Bucket to the SaloonPHP Rate Limit Plugin. This request actually came in via a Github issue, but (maybe selfishly) I actually saw a use case for one of my own applications, so I set to work implementing it!

Firstly, if you haven't read the docs for it, here's a link to that:

Rate Limit Handler | Saloon

So what is a leaky bucket? It's another way to handle rate limits set by APIs. Essentially using a leaky bucket means that while you are making requests, within a given time frame (usually every second) you "gain" additional requests you can make. For example if an API integration allowed 60 requests per minute but had a leak rate of 1 per second, you could in theory be constantly communicating with the API as long as you stay within the 60 request limit.

So how do you use it? Well, I want to keep it as easy to use as the existing rate limit behaviour, if you're specifying rate limits already, within your resolveLimits method, you can do:

protected function resolveLimits(): array
{
    return [
        Bucket::capacity(60)
            ->leak(1)
            ->everySeconds(1)
            ->sleep()
    ];
}

Bucket extends the existing Limit class, so all the existing time intervals you may already be using can be used with a bucket too.

This was fun to work on! I had to do a bit of research as I wasn't actually too familiar with leaky buckets but once I understood, I realised that there's at least one application I maintain where this could definitely come in use!

If you want to take a look at the implementation PR, I'll drop a link below!

Add Leaky Bucket Algorithm by JonPurvis · Pull Request #26 · saloonphp/rate-limit-plugin
Fixes: #12 (partially) This PR adds Leaky Bucket as another way of Rate Limiting your API Requests. I won't go too much into detail about exactly what the Leaky Bucket algorithm is here, howeve…