Skip to content

Commit

Permalink
fix(reddit): tweak internal cache logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan committed Dec 13, 2023
1 parent d157816 commit b904c0e
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions bridges/RedditBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,26 @@ class RedditBridge extends BridgeAbstract

public function collectData()
{
$cacheKey = 'reddit_rate_limit';
if ($this->cache->get($cacheKey)) {
$forbiddenKey = 'reddit_forbidden';
if ($this->cache->get($forbiddenKey)) {
throw new HttpException('403 Forbidden', 403);
}

$rateLimitKey = 'reddit_rate_limit';
if ($this->cache->get($rateLimitKey)) {
throw new HttpException('429 Too Many Requests', 429);
}

try {
$this->collectDataInternal();
} catch (HttpException $e) {
if ($e->getCode() === 429) {
$this->cache->set($cacheKey, true, 60 * 16);
}
if ($e->getCode() === 403) {
// 403 Forbidden
// This can possibly mean that reddit has permanently blocked this server's ip address
$this->cache->set($cacheKey, true, 60 * 61);
$this->cache->set($forbiddenKey, true, 60 * 61);
}
if ($e->getCode() === 429) {
$this->cache->set($rateLimitKey, true, 60 * 16);
}
throw $e;
}
Expand Down

0 comments on commit b904c0e

Please sign in to comment.