Skip to content

Instantly share code, notes, and snippets.

@Sammyjo20
Created September 4, 2023 20:27
Show Gist options
  • Save Sammyjo20/2c41434cf3409685ac792ca253725128 to your computer and use it in GitHub Desktop.
Save Sammyjo20/2c41434cf3409685ac792ca253725128 to your computer and use it in GitHub Desktop.
Saloon Custom Rate Limit Example
<?php
use Saloon\RateLimitPlugin\Limit;
use Saloon\Http\Response;
Limit::custom(function (Response $response, Limit $limit) {
$hasExceededLimit = (int)$response->header('x-ratelimit-remaining-requests') === 0;
if ($hasExceededLimit === true) {
$limit->exceeded($releaseInSeconds);
}
})->name('some-name-unique-by-' . $userId);
// Tokens
Limit::custom(function (Response $response, Limit $limit) {
$hasExceededLimit = (int)$response->header('x-ratelimit-remaining-tokens') === 0;
if ($hasExceededLimit === true) {
$limit->exceeded($releaseInSeconds);
}
})->name('some-name-unique-by-' . $userId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment