1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-23 05:51:14 +01:00

Misc fixes

This commit is contained in:
Daniil Gentili 2023-01-22 20:03:51 +01:00
parent 07d3929166
commit 581d00c80a
4 changed files with 30 additions and 7 deletions

View File

@ -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.
*

View File

@ -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;
}

View File

@ -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";

View File

@ -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();