Skip to content

Commit 7361331

Browse files
authored
Merge pull request #19 from yankewei/fix-the-reset-limit-if-based-on-response
Fix the bug when resetting the limit based on the response handler
2 parents 4487a32 + 7776a97 commit 7361331

File tree

5 files changed

+11
-5
lines changed

5 files changed

+11
-5
lines changed

src/Exceptions/RateLimitReachedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class RateLimitReachedException extends SaloonException
1212
/**
1313
* Constructor
1414
*/
15-
public function __construct(readonly protected Limit $limit)
15+
public function __construct(protected readonly Limit $limit)
1616
{
1717
parent::__construct(sprintf('Request Rate Limit Reached (Name: %s)', $this->limit->getName()));
1818
}

src/Limit.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,13 @@ public function save(RateLimitStore $store, int $resetHits = 1): static
349349
// reset the limit completely and hit once.
350350

351351
if ($this->getRemainingSeconds() < 1) {
352-
$this->resetLimit()->hit($resetHits);
352+
// When using the fromResponse limiter, we don't need to update the hit
353+
// as we only update the hit when the detect the too many attempts
354+
if ($this->usesResponse()) {
355+
$this->resetLimit();
356+
} else {
357+
$this->resetLimit()->hit($resetHits);
358+
}
353359
}
354360

355361
$data = [

src/Stores/FileStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class FileStore implements RateLimitStore
2121
* @throws \Saloon\Exceptions\DirectoryNotFoundException
2222
* @throws \Saloon\Exceptions\UnableToCreateDirectoryException
2323
*/
24-
public function __construct(readonly protected string $directory)
24+
public function __construct(protected readonly string $directory)
2525
{
2626
$this->storage = new Storage($this->directory, false);
2727
}

src/Stores/PsrStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PsrStore implements RateLimitStore
1212
/**
1313
* Constructor
1414
*/
15-
public function __construct(readonly protected CacheInterface $cache)
15+
public function __construct(protected readonly CacheInterface $cache)
1616
{
1717
//
1818
}

src/Stores/RedisStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class RedisStore implements RateLimitStore
1212
/**
1313
* Constructor
1414
*/
15-
public function __construct(readonly protected Redis $redis)
15+
public function __construct(protected readonly Redis $redis)
1616
{
1717
//
1818
}

0 commit comments

Comments
 (0)