Skip to content

Commit

Permalink
Improve logic a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jun 4, 2023
1 parent 0c547b7 commit 2865ab4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 1 files
+6 −3 docs/docs/BROADCAST.md
4 changes: 2 additions & 2 deletions src/Loop/Connection/CheckLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ protected function loop(): ?float
if ($chr & 32) {
if ($message->getSent() + $this->resendTimeout < \time()) {
if ($message->isCancellationRequested()) {
unset($this->connection->new_outgoing[$message_id]);
unset($this->connection->outgoing_messages[$message_id]);
unset($this->connection->new_outgoing[$message_id], $this->connection->outgoing_messages[$message_id]);

$this->logger->logger("Cancelling $message...", Logger::ERROR);
} else {
$this->logger->logger("Message $message received by server and is being processed for way too long, resending request...", Logger::ERROR);
Expand Down
3 changes: 2 additions & 1 deletion src/MTProto/OutgoingMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public function __construct(array|callable $body, string $constructor, string $t
/**
* Whether cancellation is requested.
*/
public function isCancellationRequested(): bool {
public function isCancellationRequested(): bool
{
return $this->cancellation?->isRequested() ?? false;
}

Expand Down
26 changes: 9 additions & 17 deletions src/Wrappers/DialogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@

namespace danog\MadelineProto\Wrappers;

use Amp\CancelledException;
use Amp\Sync\LocalMutex;
use Amp\TimeoutCancellation;
use Amp\TimeoutException;
use danog\MadelineProto\Exception;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\Settings;
use Throwable;

use function Amp\async;
use function Amp\delay;

/**
* Dialog handler.
*
Expand Down Expand Up @@ -75,24 +70,21 @@ private function cacheAllBotUsers(): void
$this->botDialogsUpdatesState = $result['intermediate_state'];
break;
case 'updates.differenceTooLong':
// Binary search for working PTS
$bottom = $this->botDialogsUpdatesState['pts'];
$top = $result['pts'];
$state = $this->botDialogsUpdatesState;
$state['pts_total_limit'] = 2147483647;
while ($bottom <= $top) {
$state['pts'] = ($bottom+$top)>>1;
while (1) {
try {
$result = $this->methodCallAsyncRead(
'updates.getDifference',
$state
)['_'];
break;
} catch (Throwable $e) {
$this->logger->logger("Got {$e->getMessage()} while getting difference, retrying...");
delay(1.0);
}
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);
Expand Down

0 comments on commit 2865ab4

Please sign in to comment.