From e80d67037c072b87adc4fd2d1c45e42a1910476e Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 3 Jun 2019 22:33:53 +0200 Subject: [PATCH] Improvements --- CHANGELOG.md | 8 ++++++-- docs | 2 +- src/danog/MadelineProto/APIFactory.php | 2 ++ src/danog/MadelineProto/Loop/Generic/GenericLoop.php | 7 ++++++- src/danog/MadelineProto/Loop/Impl/Loop.php | 6 ++---- src/danog/MadelineProto/MTProto.php | 12 +++++++++++- src/danog/MadelineProto/Wrappers/Loop.php | 7 ++++++- 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dd25318..ebfceb8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,15 @@ # Changelog - ## 4.0.0 Full async -**Fully asynchronous MadelineProto, thanks to [amphp](https://github.com/danog/amphp)!** +**Fully asynchronous MadelineProto!** + +MadelineProto now features async, for **incredible speed improvements**, and **parallel processing**. +Powered by [amphp](https://amphp.org), MadelineProto wraps the AMPHP APIs to provide a simpler generator-based async API. * Fully rewritten connection stack, with support for websockets, stuff * updates +* simultaneous method calls * new TL callback system * added support for wallpapers * Improved message splitting algorithm: performance improvements, and it will now notify you via the logs if there are too many entities in the logs, or if the entities are too long. @@ -41,6 +44,7 @@ * async HTTP requests internally * custom HTTP client with DoH * no more php 5 +* reset PTS to 0 Things to expect in the next releases: docs for update_2fa diff --git a/docs b/docs index da911f8e..f76840d0 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit da911f8e77afaae1ea9999a977868b87138d0b30 +Subproject commit f76840d014585d3ffbe8a272cd967c3d6321c096 diff --git a/src/danog/MadelineProto/APIFactory.php b/src/danog/MadelineProto/APIFactory.php index 56d3a8da..db4c2847 100644 --- a/src/danog/MadelineProto/APIFactory.php +++ b/src/danog/MadelineProto/APIFactory.php @@ -156,6 +156,7 @@ class APIFactory extends AsyncConstruct { if ($this->asyncInitPromise) { yield $this->initAsync(); + $this->API->logger->logger("Finished init asynchronously"); } if (Magic::is_fork() && !Magic::$processed_fork) { throw new Exception("Forking not supported, use async logic, instead: https://docs.madelineproto.xyz/docs/ASYNC.html"); @@ -171,6 +172,7 @@ class APIFactory extends AsyncConstruct } if ($this->API->asyncInitPromise) { yield $this->API->initAsync(); + $this->API->logger->logger("Finished init asynchronously"); } $lower_name = strtolower($name); diff --git a/src/danog/MadelineProto/Loop/Generic/GenericLoop.php b/src/danog/MadelineProto/Loop/Generic/GenericLoop.php index 76bc6bad..97417255 100644 --- a/src/danog/MadelineProto/Loop/Generic/GenericLoop.php +++ b/src/danog/MadelineProto/Loop/Generic/GenericLoop.php @@ -60,6 +60,11 @@ class GenericLoop extends ResumableSignalLoop while (true) { $timeout = yield $callback(); + if ($timeout === self::PAUSE) { + $this->API->logger->logger("Pausing $this", \danog\MadelineProto\Logger::VERBOSE); + } else if ($timeout > 0) { + $this->API->logger->logger("Pausing $this for $timeout", \danog\MadelineProto\Logger::VERBOSE); + } if ($timeout === self::STOP || yield $this->waitSignal($this->pause($timeout))) { return; } @@ -68,6 +73,6 @@ class GenericLoop extends ResumableSignalLoop public function __toString(): string { - return "{$this->name} loop"; + return $this->name; } } diff --git a/src/danog/MadelineProto/Loop/Impl/Loop.php b/src/danog/MadelineProto/Loop/Impl/Loop.php index 6fe3da9d..1c5dec6a 100644 --- a/src/danog/MadelineProto/Loop/Impl/Loop.php +++ b/src/danog/MadelineProto/Loop/Impl/Loop.php @@ -48,9 +48,7 @@ abstract class Loop implements LoopInterface return false; } - $this->callFork($this->loopImpl()); - - return true; + return $this->callFork($this->loopImpl()); } private function loopImpl() @@ -63,7 +61,7 @@ abstract class Loop implements LoopInterface } finally { $this->exitedLoop(); $this->API->logger->logger("Exited $this", Logger::ULTRA_VERBOSE); - return null; + //return null; } } diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 7e0f3571..3f876cad 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -883,9 +883,19 @@ class MTProto extends AsyncConstruct implements TLCallback yield $this->get_phone_config_async(); } + public function resetUpdateSystem() + { + foreach ($this->channels_state->get() as $state) { + $channelId = $state->getChannel(); + $this->channels_state->__construct([$channelId => new UpdatesState()]); + } + $this->startUpdateSystem(); + } public function startUpdateSystem() { - if ($this->asyncInitPromise) return; + if ($this->asyncInitPromise) { + return; + } if (!isset($this->seqUpdater)) { $this->seqUpdater = new SeqLoop($this); diff --git a/src/danog/MadelineProto/Wrappers/Loop.php b/src/danog/MadelineProto/Wrappers/Loop.php index 6f87c27a..3bf6738a 100644 --- a/src/danog/MadelineProto/Wrappers/Loop.php +++ b/src/danog/MadelineProto/Wrappers/Loop.php @@ -20,6 +20,7 @@ namespace danog\MadelineProto\Wrappers; use Amp\Deferred; +use Amp\Promise; /** * Manages logging in and out. @@ -36,9 +37,13 @@ trait Loop public function loop_async($max_forks = 0) { if (is_callable($max_forks)) { - $this->logger->logger('Running async callable and exiting from loop'); + $this->logger->logger('Running async callable'); return yield $max_forks(); } + if ($max_forks instanceof Promise) { + $this->logger->logger('Resolving async promise'); + return yield $max_forks; + } if (in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'get_updates_update_handler'], 'get_updates_update_handler'])) { $this->logger->logger('Getupdates event handler is enabled, exiting from loop', \danog\MadelineProto\Logger::FATAL_ERROR); return false;