From 581d00c80ae8a9b1a4851d768257722b85919d6f Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 22 Jan 2023 20:03:51 +0100 Subject: [PATCH] Misc fixes --- src/danog/MadelineProto/EventHandler.php | 14 ++++++++++++++ src/danog/MadelineProto/GarbageCollector.php | 5 +++++ .../MadelineProto/MTProto/OutgoingMessage.php | 15 +++++++++------ .../MadelineProto/MTProtoTools/UpdateHandler.php | 3 ++- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/danog/MadelineProto/EventHandler.php b/src/danog/MadelineProto/EventHandler.php index 830d262dc..00f8febbf 100644 --- a/src/danog/MadelineProto/EventHandler.php +++ b/src/danog/MadelineProto/EventHandler.php @@ -20,11 +20,14 @@ declare(strict_types=1); namespace danog\MadelineProto; +use Amp\DeferredFuture; use Amp\Future; use Amp\Sync\LocalMutex; use danog\MadelineProto\Db\DbPropertiesTrait; use Generator; +use function Amp\delay; + /** * Event handler. */ @@ -89,6 +92,7 @@ abstract class EventHandler extends InternalDoc $this->{$namespace} = $this->exportNamespace($namespace); } } + private ?Future $startFuture = null; /** * Start method handler. * @@ -97,6 +101,8 @@ abstract class EventHandler extends InternalDoc public function startInternal(): void { $this->startMutex ??= new LocalMutex; + $startDeferred = new DeferredFuture; + $this->startFuture = $startDeferred->getFuture(); $lock = $this->startMutex->acquire(); try { if ($this->startedInternal) { @@ -116,9 +122,17 @@ abstract class EventHandler extends InternalDoc } $this->startedInternal = true; } finally { + $this->startFuture = null; + $startDeferred->complete(); $lock->release(); } } + /** + * @internal + */ + public function waitForStartInternal(): void { + $this->startFuture?->await(); + } /** * Get peers where to send error reports. * diff --git a/src/danog/MadelineProto/GarbageCollector.php b/src/danog/MadelineProto/GarbageCollector.php index 868f2c728..9041885ac 100644 --- a/src/danog/MadelineProto/GarbageCollector.php +++ b/src/danog/MadelineProto/GarbageCollector.php @@ -123,6 +123,11 @@ final class GarbageCollector $memory = \round(\memory_get_usage()/1024/1024, 1); if (!Magic::$suspendPeriodicLogging) { Logger::log("Memory consumption: $memory Mb", Logger::ULTRA_VERBOSE); + /*try { + $maps = \substr_count(\file_get_contents('/proc/self/maps'), "\n"); + Logger::log("mmap'ed regions: $maps", Logger::ULTRA_VERBOSE); + } catch (\Throwable) { + }*/ } return (int) $memory; } diff --git a/src/danog/MadelineProto/MTProto/OutgoingMessage.php b/src/danog/MadelineProto/MTProto/OutgoingMessage.php index 0cad2abd6..2a8d624a8 100644 --- a/src/danog/MadelineProto/MTProto/OutgoingMessage.php +++ b/src/danog/MadelineProto/MTProto/OutgoingMessage.php @@ -424,12 +424,15 @@ class OutgoingMessage extends Message */ public function __toString(): string { - $state = match ($this->state) { - self::STATE_PENDING => 'pending', - self::STATE_SENT => "sent ".(\time() - $this->sent)." seconds ago", - self::STATE_ACKED => 'acked', - self::STATE_REPLIED => 'acked (by reply)', - }; + if ($this->state & self::STATE_REPLIED) { + $state = 'acked (by reply)'; + } elseif ($this->state & self::STATE_ACKED) { + $state = 'acked'; + } elseif ($this->state & self::STATE_SENT) { + $state = 'sent '.(\time() - $this->sent).' seconds ago'; + } else { + $state = 'pending'; + } if ($this->msgId) { $msgId = MsgIdHandler::toString($this->msgId); return "{$this->constructor} with message ID $msgId $state"; diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 5194ad50c..68954ef4c 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -105,6 +105,7 @@ trait UpdateHandler if (!isset($this->eventHandlerMethods[$update['_']])) { return; } + $this->event_handler_instance->waitForStartInternal(); $r = $this->eventHandlerMethods[$update['_']]($update); if ($r instanceof Generator) { Tools::consumeGenerator($r); @@ -121,7 +122,7 @@ trait UpdateHandler $payload = \json_encode($update); Assert::notEmpty($payload); Assert::notNull($this->webhookUrl); - $request = new Request($this->hook_url, 'POST'); + $request = new Request($this->webhookUrl, 'POST'); $request->setHeader('content-type', 'application/json'); $request->setBody($payload); $result = ($this->datacenter->getHTTPClient()->request($request))->getBody()->buffer();