Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rate limit queues up batches and sends them #6

Open
Joel-Jensen opened this issue Aug 15, 2023 · 0 comments
Open

Rate limit queues up batches and sends them #6

Joel-Jensen opened this issue Aug 15, 2023 · 0 comments

Comments

@Joel-Jensen
Copy link

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:

  • Prints price x times
  • Rate limit is hit
  • Delay next request of remaining 5 seconds limit
  • Delay another 100 request
  • Delay next request of remaining 4 seconds limit
  • Delay another 100 request
  • ...
  • 5 seconds later, send 100 requests
  • 6 seconds later, another batch of 100 requests

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:

$existingDelay = $pendingRequest->delay()->get() ?? 0;

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;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant