saloonphp / rate-limit-plugin

🤠 Handle rate limits beautifully in your Saloon API integrations or SDKs
https://docs.saloon.dev
MIT License
15 stars 0 forks source link

Rate limit queues up batches and sends them #6

Open Joel-Jensen opened 1 year ago

Joel-Jensen commented 1 year ago

Hi! Love the idea and syntax of the package, got into one problem tho. For a big batch import job instead of limiting the rates, as I understand it, it just queues up hundreds of jobs each second.

Command example:

while (true) {
    try {
        $price = $api->prices()->get();

        $this->info($price);
    } catch(\Exception $e) {
        dump($e->getMessage());
    }
} 

The execution:

And I get tons of 429.

You see the issue? I feel like it would be nice if this time calculation could take everything else queued into account too: https://github.com/saloonphp/rate-limit-plugin/blob/98f19b5434d7d4488a9db8ad85908c9e55500a3c/src/Traits/HasRateLimits.php#L177

Sadly I have know clue of how to do that and if it would be possible, so I'm not much of any more help here.

In the mean time it looks like this will be my very basic rate limiter:

public function send(Request $request, MockClient $mockClient = null): Response
{
    // 🚀 ... 🪐  ... 💫

    try {
        return $this->createPendingRequest($request, $mockClient)->send();
    } catch(\Exception $e) {
        if(str($e->getMessage())->contains('429')) {
            // This is our request limiter, let's wait a bit and retry
            sleep(3);
            return $this->createPendingRequest($request, $mockClient)->send();
        }
        throw $e;
    }
}