Merge pull request #14089 from annando/ping-lock

Prevent concurring ping requests
This commit is contained in:
Hypolite Petovan 2024-04-10 19:19:24 -04:00 committed by GitHub
commit ce580241e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 3 deletions

View file

@ -28,17 +28,14 @@ use Friendica\Content\GroupManager;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Model\Circle;
use Friendica\Model\Post;
use Friendica\Model\User;
use Friendica\Model\Verb;
use Friendica\Module\Conversation\Network;
use Friendica\Module\Register;
use Friendica\Module\Response;
use Friendica\Navigation\Notifications\Entity;

View file

@ -21,11 +21,43 @@
namespace Friendica\Module\Ping;
use Friendica\App;
use Friendica\App\Mode;
use Friendica\Content\Conversation;
use Friendica\Content\Conversation\Factory\Timeline as TimelineFactory;
use Friendica\Content\Conversation\Repository\UserDefinedChannel;
use Friendica\Content\Conversation\Factory\Channel as ChannelFactory;
use Friendica\Content\Conversation\Factory\UserDefinedChannel as UserDefinedChannelFactory;
use Friendica\Content\Conversation\Factory\Community as CommunityFactory;
use Friendica\Content\Conversation\Factory\Network as NetworkFactory;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
use Friendica\Core\Lock\Capability\ICanLock;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\Module\Conversation\Network as NetworkModule;
use Friendica\Module\Response;
use Friendica\Navigation\SystemMessages;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
class Network extends NetworkModule
{
/**
* @var ICanLock
*/
private $lock;
public function __construct(ICanLock $lock, UserDefinedChannelFactory $userDefinedChannel, NetworkFactory $network, CommunityFactory $community, ChannelFactory $channelFactory, UserDefinedChannel $channel, App $app, TimelineFactory $timeline, SystemMessages $systemMessages, Mode $mode, Conversation $conversation, App\Page $page, IHandleUserSessions $session, Database $database, IManagePersonalConfigValues $pConfig, IManageConfigValues $config, ICanCache $cache, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
{
parent::__construct($userDefinedChannel, $network, $community, $channelFactory, $channel, $app, $timeline, $systemMessages, $mode, $conversation, $page, $session, $database, $pConfig, $config, $cache, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->lock = $lock;
}
protected function rawContent(array $request = [])
{
if (!$this->session->getLocalUserId()) {
@ -46,6 +78,12 @@ class Network extends NetworkModule
System::httpExit('');
}
$lockkey = 'network-ping-' . $this->session->getLocalUserId();
if (!$this->lock->acquire($lockkey, 0)) {
$this->logger->debug('Ping-1-lock', ['uid' => $this->session->getLocalUserId()]);
System::httpExit('');
}
$this->setPing(true);
$this->itemsPerPage = 100;
@ -56,6 +94,7 @@ class Network extends NetworkModule
} else {
$items = $this->getItems();
}
$this->lock->release($lockkey);
$count = count($items);
System::httpExit(($count < 100) ? $count : '99+');
}