Skip to content

Commit

Permalink
Persist more data across restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jun 4, 2023
1 parent 2865ab4 commit 7c03515
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/MTProto.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ final class MTProto implements TLCallback, LoggerGetter
*
* @var string
*/
const RELEASE = '8.0.0-beta88';
const RELEASE = '8.0.0-beta89';
/**
* We're not logged in.
*
Expand Down Expand Up @@ -692,6 +692,9 @@ public function __sleep(): array
'tmpDbPrefix',

// Misc caching
'searchingRightPts',
'bottomPts',
'topPts',
'botDialogsUpdatesState',
'cachedAllBotUsers',
'dialog_params',
Expand Down
4 changes: 2 additions & 2 deletions src/MTProtoTools/ReferenceDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public function addOrigin(array $data = []): void
}
$originType = \array_pop($this->cacheContexts);
if (!isset($this->cache[$key])) {
$this->API->logger->logger("Removing origin context {$originType} for {$data['_']}, nothing in the reference cache!", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
//$this->API->logger->logger("Removing origin context {$originType} for {$data['_']}, nothing in the reference cache!", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
return;
}
$cache = $this->cache[$key];
Expand Down Expand Up @@ -326,7 +326,7 @@ public function addOriginMethod(OutgoingMessage $data, array $res): void
}
$originType = \array_pop($this->cacheContexts);
if (!isset($this->cache[$key])) {
$this->API->logger->logger("Removing origin context {$originType} for {$constructor}, nothing in the reference cache!", Logger::ULTRA_VERBOSE);
//$this->API->logger->logger("Removing origin context {$originType} for {$constructor}, nothing in the reference cache!", Logger::ULTRA_VERBOSE);
return;
}
$cache = $this->cache[$key];
Expand Down
70 changes: 44 additions & 26 deletions src/Wrappers/DialogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use danog\MadelineProto\MTProto;
use danog\MadelineProto\Settings;
use Throwable;
use Webmozart\Assert\Assert;

/**
* Dialog handler.
Expand All @@ -43,6 +44,9 @@ trait DialogHandler
];
private bool $cachedAllBotUsers = false;
private ?LocalMutex $cachingAllBotUsers = null;
private bool $searchingRightPts = false;
private int $bottomPts = 0;
private int $topPts = 0;

private function cacheAllBotUsers(): void
{
Expand All @@ -52,6 +56,9 @@ private function cacheAllBotUsers(): void
$this->cachingAllBotUsers ??= new LocalMutex;
$lock = $this->cachingAllBotUsers->acquire();
try {
if ($this->searchingRightPts) {
$this->searchRightPts();
}
while (true) {
$result = $this->methodCallAsyncRead(
'updates.getDifference',
Expand All @@ -70,32 +77,10 @@ private function cacheAllBotUsers(): void
$this->botDialogsUpdatesState = $result['intermediate_state'];
break;
case 'updates.differenceTooLong':
$bottom = $this->botDialogsUpdatesState['pts'];
$top = $result['pts'];
$state = $this->botDialogsUpdatesState;
$state['pts_total_limit'] = 2147483647;
while ($bottom <= $top) {
$state['pts'] = ($bottom+$top)>>1;
try {
$result = $this->methodCallAsyncRead(
'updates.getDifference',
$state,
['cancellation' => new TimeoutCancellation(60.0)]
)['_'];
} catch (Throwable $e) {
$this->logger->logger("Got {$e->getMessage()} while getting difference, trying another PTS...");
$result = 'updates.differenceTooLong';
}
$this->logger("$bottom, {$state['pts']}, $top");
$this->logger($result);
if ($result === 'updates.differenceTooLong') {
$bottom = $state['pts']+1;
} else {
$top = $state['pts']-1;
}
}
$this->botDialogsUpdatesState['pts'] = $bottom;
$this->logger("Found PTS $bottom");
$this->bottomPts = $this->botDialogsUpdatesState['pts'];
$this->topPts = $result['pts'];
$this->searchingRightPts = true;
$this->searchRightPts();
break;
default:
throw new Exception('Unrecognized update difference received: '.\var_export($result, true));
Expand All @@ -106,6 +91,39 @@ private function cacheAllBotUsers(): void
$lock->release();
}
}
private function searchRightPts(): void
{
Assert::true($this->searchingRightPts);
$this->logger->logger("Searching for the right PTS (will take a loooong time...)...");
try {
$state = $this->botDialogsUpdatesState;
$state['pts_total_limit'] = 2147483647;
while ($this->bottomPts <= $this->topPts) {
$state['pts'] = ($this->bottomPts+$this->topPts)>>1;
try {
$result = $this->methodCallAsyncRead(
'updates.getDifference',
$state,
['cancellation' => new TimeoutCancellation(15.0)]
)['_'];
} catch (Throwable $e) {
$this->logger->logger("Got {$e->getMessage()} while getting difference, trying another PTS...");
$result = 'updates.differenceTooLong';
}
$this->logger("{$this->bottomPts}, {$state['pts']}, {$this->topPts}");
$this->logger($result);
if ($result === 'updates.differenceTooLong') {
$this->bottomPts = $state['pts']+1;
} else {
$this->topPts = $state['pts']-1;
}
}
$this->botDialogsUpdatesState['pts'] = $this->bottomPts;
$this->logger("Found PTS {$this->bottomPts}");
} finally {
$this->searchingRightPts = false;
}
}
/**
* Get dialog IDs.
*
Expand Down

0 comments on commit 7c03515

Please sign in to comment.