Reduce the number of HTTP requests in the media handling

This commit is contained in:
Michael 2024-12-09 13:40:47 +00:00
parent 69345432e1
commit e7d9c6c254
11 changed files with 207 additions and 89 deletions

View file

@ -38,9 +38,10 @@ use stdClass;
*/
class Jetstream
{
private $uids = [];
private $self = [];
private $capped = false;
private $uids = [];
private $self = [];
private $capped = false;
private $next_stat = 0;
/** @var LoggerInterface */
private $logger;
@ -108,6 +109,7 @@ class Jetstream
$timestamp = $data->time_us;
$this->route($data);
$this->keyValue->set('jetstream_timestamp', $timestamp);
$this->incrementMessages();
} else {
$this->logger->warning('Unexpected return value', ['data' => $data]);
break;
@ -135,6 +137,15 @@ class Jetstream
}
}
private function incrementMessages()
{
$packets = (int)($this->keyValue->get('jetstream_messages') ?? 0);
if ($packets >= PHP_INT_MAX) {
$packets = 0;
}
$this->keyValue->set('jetstream_messages', $packets + 1);
}
private function syncContacts()
{
$active_uids = $this->atprotocol->getUids();
@ -184,10 +195,14 @@ class Jetstream
}
if (!$this->capped && count($dids) < $did_limit) {
$contacts = Contact::selectToArray(['url'], ['uid' => 0, 'network' => Protocol::BLUESKY], ['order' => ['last-item' => true], 'limit' => $did_limit]);
$condition = ["`uid` = ? AND `network` = ? AND EXISTS(SELECT `author-id` FROM `post-user` WHERE `author-id` = `contact`.`id` AND `post-user`.`uid` != ?)", 0, Protocol::BLUESKY, 0];
$contacts = Contact::selectToArray(['url'], $condition, ['order' => ['last-item' => true], 'limit' => $did_limit]);
$dids = $this->addDids($contacts, $uids, $did_limit, $dids);
}
$this->keyValue->set('jetstream_did_count', count($dids));
$this->keyValue->set('jetstream_did_limit', $did_limit);
$this->logger->debug('Selected DIDs', ['uids' => $active_uids, 'count' => count($dids), 'capped' => $this->capped]);
$update = [
'type' => 'options_update',
@ -241,17 +256,7 @@ class Jetstream
private function routeCommits(stdClass $data)
{
$drift = max(0, round(time() - $data->time_us / 1000000));
if ($drift > 60 && !$this->capped) {
$this->capped = true;
$this->setOptions();
$this->logger->notice('Drift is too high, dids will be capped');
} elseif ($drift == 0 && $this->capped) {
$this->capped = false;
$this->setOptions();
$this->logger->notice('Drift is low enough, dids will be uncapped');
}
$drift = $this->getDrift($data);
$this->logger->notice('Received commit', ['time' => date(DateTimeFormat::ATOM, $data->time_us / 1000000), 'drift' => $drift, 'capped' => $this->capped, 'did' => $data->did, 'operation' => $data->commit->operation, 'collection' => $data->commit->collection, 'timestamp' => $data->time_us]);
$timestamp = microtime(true);
@ -299,6 +304,23 @@ class Jetstream
}
}
private function getDrift(stdClass $data): int
{
$drift = max(0, round(time() - $data->time_us / 1000000));
$this->keyValue->set('jetstream_drift', $drift);
if ($drift > 60 && !$this->capped) {
$this->capped = true;
$this->setOptions();
$this->logger->notice('Drift is too high, dids will be capped');
} elseif ($drift == 0 && $this->capped) {
$this->capped = false;
$this->setOptions();
$this->logger->notice('Drift is low enough, dids will be uncapped');
}
return $drift;
}
private function routePost(stdClass $data, int $drift)
{
switch ($data->commit->operation) {