Skip to content

Commit

Permalink
fix(reddit): cache tweak for 403 forbidden
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan committed Dec 13, 2023
1 parent 0b67544 commit b8be1de
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function execute(array $request)
'message' => 'RSS-Bridge is down for maintenance.',
]), 503);
}

$cacheKey = 'http_' . json_encode($request);
/** @var Response $cachedResponse */
$cachedResponse = $this->cache->get($cacheKey);
Expand Down Expand Up @@ -80,16 +81,19 @@ public function execute(array $request)
$this->cache->set($cacheKey, $response, $ttl);
}

if (in_array($response->getCode(), [429, 503])) {
$this->cache->set($cacheKey, $response, 60 * 15 + rand(1, 60 * 10)); // average 20m
if (in_array($response->getCode(), [403, 429, 503])) {
// Cache these responses for about ~20 mins on average
$this->cache->set($cacheKey, $response, 60 * 15 + rand(1, 60 * 10));
}

if ($response->getCode() === 500) {
$this->cache->set($cacheKey, $response, 60 * 15);
}

if (rand(1, 100) === 2) {
$this->cache->prune();
}

return $response;
}

Expand Down Expand Up @@ -187,6 +191,7 @@ private function createFeedItemFromException($e, BridgeAbstract $bridge): FeedIt

private function logBridgeError($bridgeName, $code)
{
// todo: it's not really necessary to json encode $report
$cacheKey = 'error_reporting_' . $bridgeName . '_' . $code;
$report = $this->cache->get($cacheKey);
if ($report) {
Expand Down
5 changes: 5 additions & 0 deletions bridges/RedditBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public function collectData()
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);
}
throw $e;
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function getContents(
// Ignore invalid 'Last-Modified' HTTP header value
}
}
// todo: to be nice nice citizen we should also check for Etag
}

$response = $httpClient->request($url, $config);
Expand Down
1 change: 1 addition & 0 deletions lib/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class HttpException extends \Exception
{
// todo: should include the failing http request (if present)
}

final class CloudFlareException extends HttpException
Expand Down

0 comments on commit b8be1de

Please sign in to comment.