Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/FUNDING
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ko_fi: sammyjo20
github: sammyjo20
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.4'
coverage: none

- name: Install composer dependencies
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/redis-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.1, 8.2 ]
php: [ 8.1, 8.2, 8.3, 8.4 ]
stability: [ prefer-lowest, prefer-stable ]
services:
# Label used to access the service container
Expand All @@ -39,7 +39,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest, windows-latest ]
php: [ 8.1, 8.2 ]
php: [ 8.1, 8.2, 8.3, 8.4 ]
stability: [ prefer-lowest, prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# Caches, externally stored stuff, etc
.php-cs-fixer.cache
.phpunit.result.cache
.phpunit.cache
tests/Fixtures/Saloon

# environments/configs
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"orchestra/testbench": "^8.5"
},
"minimum-stability": "stable",
"prefer-stable": true,
"autoload": {
"psr-4": {
"Saloon\\RateLimitPlugin\\": "src/",
Expand Down
27 changes: 14 additions & 13 deletions src/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Saloon\RateLimitPlugin;

use Closure;
use DateInterval;
use DateTimeImmutable;
use Saloon\Http\Response;
Expand Down Expand Up @@ -40,7 +39,7 @@ class Limit
/**
* The threshold that should be used when determining if a limit has been reached
*
* Must be between 0 and 1. For example if you want the limiter to kick in at 85%
* Must be between 0 and 1. For example, if you want the limiter to kick in at 85%
* you must set the threshold to 0.85
*/
protected float $threshold = 1;
Expand All @@ -58,8 +57,10 @@ class Limit

/**
* Custom response handler
*
* @var null|callable(): mixed
*/
protected ?Closure $responseHandler = null;
protected $responseHandler = null;

/**
* Determines if we should sleep or not
Expand All @@ -69,15 +70,15 @@ class Limit
/**
* @param (callable(): mixed)|null $responseHandler
*/
final public function __construct(int $allow, float $threshold = 1, callable $responseHandler = null)
final public function __construct(int $allow, float $threshold = 1, ?callable $responseHandler = null)
{
$this->allow = $allow;
$this->threshold = $threshold;
$this->responseHandler = isset($responseHandler) ? $responseHandler(...) : null;
$this->responseHandler = $responseHandler;
}

/**
* Construct a limiter's allow and threshold
* Construct a limiter's allowing and threshold
*/
public static function allow(int $requests, float $threshold = 1): static
{
Expand All @@ -89,7 +90,7 @@ public static function allow(int $requests, float $threshold = 1): static
*/
public static function custom(callable $responseHandler): static
{
return (new static(1, 1, $responseHandler(...)))->everySeconds(60, 'custom');
return (new static(1, 1, $responseHandler))->everySeconds(60, 'custom');
}

/**
Expand Down Expand Up @@ -123,7 +124,7 @@ public function hit(int $amount = 1): static
/**
* Set the limit as exceeded
*/
public function exceeded(int $releaseInSeconds = null): void
public function exceeded(?int $releaseInSeconds = null): void
{
$this->exceeded = true;

Expand Down Expand Up @@ -281,7 +282,7 @@ public function handleResponse(Response $response): void
*
* @return $this
* @throws \JsonException
* @throws \Saloon\RateLimitPlugin\Exceptions\LimitException
* @throws LimitException
*/
public function update(RateLimitStore $store): static
{
Expand Down Expand Up @@ -316,14 +317,14 @@ public function update(RateLimitStore $store): static
return $this;
}

// If our expiry hasn't passed, yet then we'll set the expiry timestamp
// and, we'll also update the hits so the current instance has the
// If our expiry hasn't passed yet, then we'll set the expiry timestamp,
// and we'll also update the hits so the current instance has the
// number of previous hits.

$this->setExpiryTimestamp($expiry);
$this->hit($hits);

// If this is a fromResponse limiter then we should apply the "allow" which will
// If this is a fromResponse limiter, then we should apply the "allow" which will
// be useful to check if we have reached our rate limit

if ($this->usesResponse()) {
Expand All @@ -338,7 +339,7 @@ public function update(RateLimitStore $store): static
*
* @return $this
* @throws \JsonException
* @throws \Saloon\RateLimitPlugin\Exceptions\LimitException
* @throws LimitException
*/
public function save(RateLimitStore $store, int $resetHits = 1): static
{
Expand Down
Loading