Skip to content

Commit

Permalink
feat: introduce RateLimitException
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan committed Aug 8, 2024
1 parent 7073bb2 commit ad40212
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
8 changes: 6 additions & 2 deletions actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ private function createResponse(Request $request, BridgeAbstract $bridge, string
$items = $feedItems;
}
$feed = $bridge->getFeed();
} catch (\Exception $e) {
// Probably an exception inside a bridge
} catch (\Throwable $e) {
if ($e instanceof RateLimitException) {
// These are internally generated by bridges
$this->logger->info(sprintf('RateLimitException in DisplayAction(%s): %s', $bridge->getShortName(), create_sane_exception_message($e)));
return new Response(render(__DIR__ . '/../templates/exception.html.php', ['e' => $e]), 429);
}
if ($e instanceof HttpException) {
// Reproduce (and log) these responses regardless of error output and report limit
if ($e->getCode() === 429) {
Expand Down
6 changes: 4 additions & 2 deletions bridges/RedditBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public function collectData()
{
$forbiddenKey = 'reddit_forbidden';
if ($this->cache->get($forbiddenKey)) {
throw new HttpException('403 Forbidden', 403);
throw new RateLimitException();
}

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

try {
Expand All @@ -108,8 +108,10 @@ public function collectData()
// 403 Forbidden
// This can possibly mean that reddit has permanently blocked this server's ip address
$this->cache->set($forbiddenKey, true, 60 * 61);
throw new RateLimitException();
} elseif ($e->getCode() === 429) {
$this->cache->set($rateLimitKey, true, 60 * 61);
throw new RateLimitException();
}
throw $e;
}
Expand Down
2 changes: 1 addition & 1 deletion bridges/Vk2Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected function getImageURLWithLargestWidth($items)
public function collectData()
{
if ($this->cache->get($this->rateLimitCacheKey)) {
throw new HttpException('429 Too Many Requests', 429);
throw new RateLimitException();
}

$u = $this->getInput('u');
Expand Down
2 changes: 1 addition & 1 deletion bridges/VkBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ private function getContents()
$uri = urljoin(self::URI, $headers['location'][0]);

if (str_contains($uri, '/429.html')) {
returnServerError('VK responded "Too many requests"');
throw new RateLimitException();
}

if (!preg_match('#^https?://vk.com/#', $uri)) {
Expand Down
3 changes: 2 additions & 1 deletion bridges/YoutubeBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ public function collectData()
{
$cacheKey = 'youtube_rate_limit';
if ($this->cache->get($cacheKey)) {
throw new HttpException('429 Too Many Requests', 429);
throw new RateLimitException();
}
try {
$this->collectDataInternal();
} catch (HttpException $e) {
if ($e->getCode() === 429) {
$this->cache->set($cacheKey, true, 60 * 16);
throw new RateLimitException();
}
throw $e;
}
Expand Down
10 changes: 10 additions & 0 deletions lib/http.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/**
* Thrown by bridges
*/
final class RateLimitException extends \Exception
{
}

/**
* @internal Do not use this class in bridges
*/
class HttpException extends \Exception
{
public ?Response $response;
Expand Down

0 comments on commit ad40212

Please sign in to comment.