fix(reddit): cache tweak for 403 forbidden (#3830)

This commit is contained in:
Dag 2023-12-13 21:56:14 +01:00 committed by GitHub
parent f01729c86f
commit d157816e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 2 deletions

View File

@ -19,6 +19,7 @@ class DisplayAction implements ActionInterface
'message' => 'RSS-Bridge is down for maintenance.', 'message' => 'RSS-Bridge is down for maintenance.',
]), 503); ]), 503);
} }
$cacheKey = 'http_' . json_encode($request); $cacheKey = 'http_' . json_encode($request);
/** @var Response $cachedResponse */ /** @var Response $cachedResponse */
$cachedResponse = $this->cache->get($cacheKey); $cachedResponse = $this->cache->get($cacheKey);
@ -80,16 +81,19 @@ class DisplayAction implements ActionInterface
$this->cache->set($cacheKey, $response, $ttl); $this->cache->set($cacheKey, $response, $ttl);
} }
if (in_array($response->getCode(), [429, 503])) { if (in_array($response->getCode(), [403, 429, 503])) {
$this->cache->set($cacheKey, $response, 60 * 15 + rand(1, 60 * 10)); // average 20m // Cache these responses for about ~20 mins on average
$this->cache->set($cacheKey, $response, 60 * 15 + rand(1, 60 * 10));
} }
if ($response->getCode() === 500) { if ($response->getCode() === 500) {
$this->cache->set($cacheKey, $response, 60 * 15); $this->cache->set($cacheKey, $response, 60 * 15);
} }
if (rand(1, 100) === 2) { if (rand(1, 100) === 2) {
$this->cache->prune(); $this->cache->prune();
} }
return $response; return $response;
} }
@ -187,6 +191,7 @@ class DisplayAction implements ActionInterface
private function logBridgeError($bridgeName, $code) private function logBridgeError($bridgeName, $code)
{ {
// todo: it's not really necessary to json encode $report
$cacheKey = 'error_reporting_' . $bridgeName . '_' . $code; $cacheKey = 'error_reporting_' . $bridgeName . '_' . $code;
$report = $this->cache->get($cacheKey); $report = $this->cache->get($cacheKey);
if ($report) { if ($report) {

View File

@ -85,6 +85,11 @@ class RedditBridge extends BridgeAbstract
if ($e->getCode() === 429) { if ($e->getCode() === 429) {
$this->cache->set($cacheKey, true, 60 * 16); $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; throw $e;
} }
} }

View File

@ -71,6 +71,7 @@ function getContents(
// Ignore invalid 'Last-Modified' HTTP header value // Ignore invalid 'Last-Modified' HTTP header value
} }
} }
// todo: to be nice nice citizen we should also check for Etag
} }
$response = $httpClient->request($url, $config); $response = $httpClient->request($url, $config);

View File

@ -2,6 +2,7 @@
class HttpException extends \Exception class HttpException extends \Exception
{ {
// todo: should include the failing http response (if present)
} }
final class CloudFlareException extends HttpException final class CloudFlareException extends HttpException