From d157816e07e47dfdc8583d5fcee1925031aa6496 Mon Sep 17 00:00:00 2001 From: Dag Date: Wed, 13 Dec 2023 21:56:14 +0100 Subject: [PATCH] fix(reddit): cache tweak for 403 forbidden (#3830) --- actions/DisplayAction.php | 9 +++++++-- bridges/RedditBridge.php | 5 +++++ lib/contents.php | 1 + lib/http.php | 1 + 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/actions/DisplayAction.php b/actions/DisplayAction.php index e3b25fef..43563996 100644 --- a/actions/DisplayAction.php +++ b/actions/DisplayAction.php @@ -19,6 +19,7 @@ class DisplayAction implements ActionInterface 'message' => 'RSS-Bridge is down for maintenance.', ]), 503); } + $cacheKey = 'http_' . json_encode($request); /** @var Response $cachedResponse */ $cachedResponse = $this->cache->get($cacheKey); @@ -80,16 +81,19 @@ class DisplayAction implements ActionInterface $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; } @@ -187,6 +191,7 @@ class DisplayAction implements ActionInterface 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) { diff --git a/bridges/RedditBridge.php b/bridges/RedditBridge.php index f761afaa..c393c146 100644 --- a/bridges/RedditBridge.php +++ b/bridges/RedditBridge.php @@ -85,6 +85,11 @@ class RedditBridge extends BridgeAbstract 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; } } diff --git a/lib/contents.php b/lib/contents.php index 055d6bf3..a4def21a 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -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); diff --git a/lib/http.php b/lib/http.php index c5c57d05..eb70705f 100644 --- a/lib/http.php +++ b/lib/http.php @@ -2,6 +2,7 @@ class HttpException extends \Exception { + // todo: should include the failing http response (if present) } final class CloudFlareException extends HttpException