diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 05f1b225..f1f378be 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -305,7 +305,7 @@ class Connection extends Session */ public function connect(ConnectionContext $ctx): \Generator { - $this->API->logger->logger("Trying connection via $ctx", \danog\MadelineProto\Logger::WARNING); + $this->API->logger->logger("Trying connection ({$this->id}) via $ctx", \danog\MadelineProto\Logger::WARNING); $ctx->setReadCallback([$this, 'haveRead']); diff --git a/src/danog/MadelineProto/DataCenterConnection.php b/src/danog/MadelineProto/DataCenterConnection.php index a71894cb..878e59f0 100644 --- a/src/danog/MadelineProto/DataCenterConnection.php +++ b/src/danog/MadelineProto/DataCenterConnection.php @@ -330,6 +330,9 @@ class DataCenterConnection implements JsonSerializable $this->decWrite = self::WRITE_WEIGHT; if ($id === -1) { + if ($this->connections) { + $this->disconnect(); + } yield $this->connectMore($count); } else { $this->availableConnections[$id] = 0; @@ -423,7 +426,12 @@ class DataCenterConnection implements JsonSerializable */ public function even() { - if (\min($this->availableConnections) < 100) { + $min = \min($this->availableConnections); + if ($min < 50) { + foreach ($this->availableConnections as &$count) { + $count += 50; + } + } else if ($min < 100) { $max = $this->isMedia() || $this->isCDN() ? $this->API->settings['connection_settings']['media_socket_count']['max'] : 1; if (\count($this->availableConnections) < $max) { $this->connectMore(2); diff --git a/src/danog/MadelineProto/Loop/Impl/Loop.php b/src/danog/MadelineProto/Loop/Impl/Loop.php index da1a98bf..04884ea4 100644 --- a/src/danog/MadelineProto/Loop/Impl/Loop.php +++ b/src/danog/MadelineProto/Loop/Impl/Loop.php @@ -68,7 +68,7 @@ abstract class Loop implements LoopInterface yield $this->loop(); } finally { $this->exitedLoop(); - $this->API->logger->logger("Exited $this", Logger::ULTRA_VERBOSE); + $this->API->logger->logger("Physically exited $this", Logger::ULTRA_VERBOSE); //return null; } } @@ -76,6 +76,7 @@ abstract class Loop implements LoopInterface public function exitedLoop() { if ($this->count) { + $this->API->logger->logger("Exited $this", Logger::ULTRA_VERBOSE); $this->count--; } } diff --git a/src/danog/MadelineProto/Magic.php b/src/danog/MadelineProto/Magic.php index c3d1d6ce..bf5a01b0 100644 --- a/src/danog/MadelineProto/Magic.php +++ b/src/danog/MadelineProto/Magic.php @@ -23,6 +23,10 @@ use Amp\DoH\DoHConfig; use Amp\DoH\Nameserver; use Amp\DoH\Rfc8484StubResolver; use Amp\Loop; +use Amp\Loop\Driver; +use ReflectionClass; +use ReflectionObject; + use function Amp\ByteStream\getInputBufferStream; use function Amp\ByteStream\getStdin; use function Amp\Dns\resolver; @@ -234,6 +238,17 @@ class Magic self::$signaled = true; getStdin()->unreference(); getInputBufferStream()->unreference(); + if ($code !== 0) { + $driver = Loop::get(); + + $reflectionClass = new ReflectionClass(Driver::class); + $reflectionProperty = $reflectionClass->getProperty('watchers'); + $reflectionProperty->setAccessible(true); + foreach (array_keys($reflectionProperty->getValue($driver)) as $key) { + $driver->unreference($key); + } + } + Loop::stop(); die($code); } }