mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-03-08 09:05:57 +03:00
feat: introduce RateLimitException (#4199)
This commit is contained in:
parent
7073bb2f46
commit
9973f731df
6 changed files with 24 additions and 7 deletions
|
@ -121,8 +121,12 @@ class DisplayAction implements ActionInterface
|
||||||
$items = $feedItems;
|
$items = $feedItems;
|
||||||
}
|
}
|
||||||
$feed = $bridge->getFeed();
|
$feed = $bridge->getFeed();
|
||||||
} catch (\Exception $e) {
|
} catch (\Throwable $e) {
|
||||||
// Probably an exception inside a bridge
|
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) {
|
if ($e instanceof HttpException) {
|
||||||
// Reproduce (and log) these responses regardless of error output and report limit
|
// Reproduce (and log) these responses regardless of error output and report limit
|
||||||
if ($e->getCode() === 429) {
|
if ($e->getCode() === 429) {
|
||||||
|
|
|
@ -93,12 +93,12 @@ class RedditBridge extends BridgeAbstract
|
||||||
{
|
{
|
||||||
$forbiddenKey = 'reddit_forbidden';
|
$forbiddenKey = 'reddit_forbidden';
|
||||||
if ($this->cache->get($forbiddenKey)) {
|
if ($this->cache->get($forbiddenKey)) {
|
||||||
throw new HttpException('403 Forbidden', 403);
|
throw new RateLimitException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$rateLimitKey = 'reddit_rate_limit';
|
$rateLimitKey = 'reddit_rate_limit';
|
||||||
if ($this->cache->get($rateLimitKey)) {
|
if ($this->cache->get($rateLimitKey)) {
|
||||||
throw new HttpException('429 Too Many Requests', 429);
|
throw new RateLimitException();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -108,8 +108,10 @@ class RedditBridge extends BridgeAbstract
|
||||||
// 403 Forbidden
|
// 403 Forbidden
|
||||||
// This can possibly mean that reddit has permanently blocked this server's ip address
|
// This can possibly mean that reddit has permanently blocked this server's ip address
|
||||||
$this->cache->set($forbiddenKey, true, 60 * 61);
|
$this->cache->set($forbiddenKey, true, 60 * 61);
|
||||||
|
throw new RateLimitException();
|
||||||
} elseif ($e->getCode() === 429) {
|
} elseif ($e->getCode() === 429) {
|
||||||
$this->cache->set($rateLimitKey, true, 60 * 61);
|
$this->cache->set($rateLimitKey, true, 60 * 61);
|
||||||
|
throw new RateLimitException();
|
||||||
}
|
}
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
|
@ -194,7 +194,7 @@ class Vk2Bridge extends BridgeAbstract
|
||||||
public function collectData()
|
public function collectData()
|
||||||
{
|
{
|
||||||
if ($this->cache->get($this->rateLimitCacheKey)) {
|
if ($this->cache->get($this->rateLimitCacheKey)) {
|
||||||
throw new HttpException('429 Too Many Requests', 429);
|
throw new RateLimitException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$u = $this->getInput('u');
|
$u = $this->getInput('u');
|
||||||
|
|
|
@ -519,7 +519,7 @@ class VkBridge extends BridgeAbstract
|
||||||
$uri = urljoin(self::URI, $headers['location'][0]);
|
$uri = urljoin(self::URI, $headers['location'][0]);
|
||||||
|
|
||||||
if (str_contains($uri, '/429.html')) {
|
if (str_contains($uri, '/429.html')) {
|
||||||
returnServerError('VK responded "Too many requests"');
|
throw new RateLimitException();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!preg_match('#^https?://vk.com/#', $uri)) {
|
if (!preg_match('#^https?://vk.com/#', $uri)) {
|
||||||
|
|
|
@ -82,13 +82,14 @@ class YoutubeBridge extends BridgeAbstract
|
||||||
{
|
{
|
||||||
$cacheKey = 'youtube_rate_limit';
|
$cacheKey = 'youtube_rate_limit';
|
||||||
if ($this->cache->get($cacheKey)) {
|
if ($this->cache->get($cacheKey)) {
|
||||||
throw new HttpException('429 Too Many Requests', 429);
|
throw new RateLimitException();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$this->collectDataInternal();
|
$this->collectDataInternal();
|
||||||
} catch (HttpException $e) {
|
} catch (HttpException $e) {
|
||||||
if ($e->getCode() === 429) {
|
if ($e->getCode() === 429) {
|
||||||
$this->cache->set($cacheKey, true, 60 * 16);
|
$this->cache->set($cacheKey, true, 60 * 16);
|
||||||
|
throw new RateLimitException();
|
||||||
}
|
}
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
10
lib/http.php
10
lib/http.php
|
@ -1,5 +1,15 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown by bridges
|
||||||
|
*/
|
||||||
|
final class RateLimitException extends \Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal Do not use this class in bridges
|
||||||
|
*/
|
||||||
class HttpException extends \Exception
|
class HttpException extends \Exception
|
||||||
{
|
{
|
||||||
public ?Response $response;
|
public ?Response $response;
|
||||||
|
|
Loading…
Add table
Reference in a new issue