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

fix: ensure ratelimit expiry is set every time #556

Merged
merged 6 commits into from
May 18, 2024
Merged
Changes from 3 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
15 changes: 10 additions & 5 deletions src/CachedKeySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,20 @@ private function rateLimitExceeded(): bool
}

$cacheItem = $this->cache->getItem($this->rateLimitCacheKey);
if (!$cacheItem->isHit()) {
$cacheItem->expiresAfter(60); // # of calls are cached each minute

if (!$cacheItem->isHit() || !\is_array($cacheItemData = $cacheItem->get())) {
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
$cacheItemData = [
'callsPerMinute' => 0,
'expiry' => new \DateTime('+60 seconds', new \DateTimeZone('UTC')),
];
}

$callsPerMinute = (int) $cacheItem->get();
if (++$callsPerMinute > $this->maxCallsPerMinute) {
if (++$cacheItemData['callsPerMinute'] > $this->maxCallsPerMinute) {
return true;
}
$cacheItem->set($callsPerMinute);

$cacheItem->set($cacheItemData);
$cacheItem->expiresAt($cacheItemData['expiry']);
$this->cache->save($cacheItem);
return false;
}
Expand Down
Loading