Fix shutdown loop, automatically inflate stripped photoSizes, improve composer reporter

This commit is contained in:
Daniil Gentili 2020-06-23 20:30:40 +02:00
parent 58e0985d33
commit bfd08f5744
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
10 changed files with 85 additions and 67 deletions

View File

@ -52,7 +52,8 @@
"danog/7to70": "^1", "danog/7to70": "^1",
"danog/7to5": "^1", "danog/7to5": "^1",
"vimeo/psalm": "dev-master", "vimeo/psalm": "dev-master",
"phpstan/phpstan": "^0.12.14" "phpstan/phpstan": "^0.12.14",
"friendsofphp/php-cs-fixer": "^2.16"
}, },
"suggest": { "suggest": {
"ext-libtgvoip": "Install the php-libtgvoip extension to make phone calls (https://github.com/danog/php-libtgvoip)" "ext-libtgvoip": "Install the php-libtgvoip extension to make phone calls (https://github.com/danog/php-libtgvoip)"

View File

@ -84,6 +84,7 @@ class MyEventHandler extends EventHandler
if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') { if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') {
yield $this->messages->sendMedia(['peer' => $update, 'message' => $update['message']['message'], 'media' => $update]); yield $this->messages->sendMedia(['peer' => $update, 'message' => $update['message']['message'], 'media' => $update]);
} }
yield $this->ping(['multiple' => true] + \array_map(fn ($v) => ['ping_id' => $v], \range(0, 1020)));
} }
} }
$settings = [ $settings = [

View File

@ -184,7 +184,7 @@ class API extends InternalDoc
$this->API->unreference(); $this->API->unreference();
} }
$this->destructing = true; $this->destructing = true;
Tools::wait($this->wrapper->serialize(), true); Tools::wait($this->wrapper->serialize());
} else { } else {
$this->logger->logger('Shutting down MadelineProto (old deserialized instance of API)'); $this->logger->logger('Shutting down MadelineProto (old deserialized instance of API)');
} }

View File

@ -137,7 +137,7 @@ class DocsBuilder
foreach (['string', 'bytes', 'int', 'int53', 'long', 'int128', 'int256', 'int512', 'double', 'Bool', 'DataJSON'] as $type) { foreach (['string', 'bytes', 'int', 'int53', 'long', 'int128', 'int256', 'int512', 'double', 'Bool', 'DataJSON'] as $type) {
\file_put_contents("types/$type.md", $this->templates[$type]); \file_put_contents("types/$type.md", $this->templates[$type]);
} }
foreach (['boolFalse', 'boolTrue', 'null'] as $constructor) { foreach (['boolFalse', 'boolTrue', 'null', 'photoStrippedSize'] as $constructor) {
\file_put_contents("constructors/$constructor.md", $this->templates[$constructor]); \file_put_contents("constructors/$constructor.md", $this->templates[$constructor]);
} }
\danog\MadelineProto\Logger::log('Done!', \danog\MadelineProto\Logger::NOTICE); \danog\MadelineProto\Logger::log('Done!', \danog\MadelineProto\Logger::NOTICE);

View File

@ -72,33 +72,37 @@ class CheckLoop extends ResumableSignalLoop
return; return;
} }
} }
if ($connection->hasPendingCalls()) { if (!$connection->hasPendingCalls()) {
$last_msgid = $connection->msgIdHandler->getMaxId(true); if (yield $this->waitSignal($this->pause($timeout))) {
$last_chunk = $connection->getLastChunk(); return;
if ($shared->hasTempAuthKey()) { }
$full_message_ids = $connection->getPendingCalls(); continue;
//array_values($connection->new_outgoing); }
foreach (\array_chunk($full_message_ids, 8192) as $message_ids) { $last_msgid = $connection->msgIdHandler->getMaxId(true);
$deferred = new Deferred(); $last_chunk = $connection->getLastChunk();
$deferred->promise()->onResolve(function ($e, $result) use ($message_ids, $API, $connection, $datacenter, $timeoutResend) { if ($shared->hasTempAuthKey()) {
if ($e) { $full_message_ids = $connection->getPendingCalls();
$API->logger("Got exception in check loop for DC {$datacenter}"); foreach (\array_chunk($full_message_ids, 8192) as $message_ids) {
$API->logger((string) $e); $deferred = new Deferred();
return; $deferred->promise()->onResolve(function ($e, $result) use ($message_ids, $API, $connection, $datacenter, $timeoutResend) {
if ($e) {
$API->logger("Got exception in check loop for DC {$datacenter}");
$API->logger((string) $e);
return;
}
$reply = [];
foreach (\str_split($result['info']) as $key => $chr) {
$message_id = $message_ids[$key];
if (!isset($connection->outgoing_messages[$message_id])) {
$API->logger->logger('Already got response for and forgot about message ID '.$message_id);
continue;
} }
$reply = []; if (!isset($connection->new_outgoing[$message_id])) {
foreach (\str_split($result['info']) as $key => $chr) { $API->logger->logger('Already got response for '.$connection->outgoing_messages[$message_id]['_'].' with message ID '.$message_id);
$message_id = $message_ids[$key]; continue;
if (!isset($connection->outgoing_messages[$message_id])) { }
$API->logger->logger('Already got response for and forgot about message ID '.$message_id); $chr = \ord($chr);
continue; switch ($chr & 7) {
}
if (!isset($connection->new_outgoing[$message_id])) {
$API->logger->logger('Already got response for '.$connection->outgoing_messages[$message_id]['_'].' with message ID '.$message_id);
continue;
}
$chr = \ord($chr);
switch ($chr & 7) {
case 0: case 0:
$API->logger->logger('Wrong message status 0 for '.$connection->outgoing_messages[$message_id]['_'], \danog\MadelineProto\Logger::FATAL_ERROR); $API->logger->logger('Wrong message status 0 for '.$connection->outgoing_messages[$message_id]['_'], \danog\MadelineProto\Logger::FATAL_ERROR);
break; break;
@ -131,42 +135,37 @@ class CheckLoop extends ResumableSignalLoop
$reply[] = $message_id; $reply[] = $message_id;
} }
} }
}
if ($reply) {
\danog\MadelineProto\Tools::callFork($connection->objectCall('msg_resend_ans_req', ['msg_ids' => $reply], ['postpone' => true]));
}
$connection->flush();
});
$list = '';
// Don't edit this here pls
foreach ($message_ids as $message_id) {
$list .= $connection->outgoing_messages[$message_id]['_'].', ';
} }
$API->logger->logger("Still missing {$list} on DC {$datacenter}, sending state request", \danog\MadelineProto\Logger::ERROR); if ($reply) {
yield from $connection->objectCall('msgs_state_req', ['msg_ids' => $message_ids], ['promise' => $deferred]); \danog\MadelineProto\Tools::callFork($connection->objectCall('msg_resend_ans_req', ['msg_ids' => $reply], ['postpone' => true]));
}
} else {
foreach ($connection->new_outgoing as $message_id) {
if (isset($connection->outgoing_messages[$message_id]['sent']) && $connection->outgoing_messages[$message_id]['sent'] + $timeout < \time() && $connection->outgoing_messages[$message_id]['unencrypted']) {
$API->logger->logger('Still missing '.$connection->outgoing_messages[$message_id]['_'].' with message id '.$message_id." on DC {$datacenter}, resending", \danog\MadelineProto\Logger::ERROR);
$connection->methodRecall('', ['message_id' => $message_id, 'postpone' => true]);
} }
$connection->flush();
});
$list = '';
// Don't edit this here pls
foreach ($message_ids as $message_id) {
$list .= $connection->outgoing_messages[$message_id]['_'].', ';
} }
$connection->flush(); $API->logger->logger("Still missing {$list} on DC {$datacenter}, sending state request", \danog\MadelineProto\Logger::ERROR);
} yield from $connection->objectCall('msgs_state_req', ['msg_ids' => $message_ids], ['promise' => $deferred]);
if (yield $this->waitSignal($this->pause($timeout))) {
return;
}
if ($connection->msgIdHandler->getMaxId(true) === $last_msgid && $connection->getLastChunk() === $last_chunk) {
$API->logger->logger("We did not receive a response for {$timeout} seconds: reconnecting and exiting check loop on DC {$datacenter}");
//$this->exitedLoop();
Tools::callForkDefer($connection->reconnect());
return;
} }
} else { } else {
if (yield $this->waitSignal($this->pause($timeout))) { foreach ($connection->new_outgoing as $message_id) {
return; if (isset($connection->outgoing_messages[$message_id]['sent']) && $connection->outgoing_messages[$message_id]['sent'] + $timeout < \time() && $connection->outgoing_messages[$message_id]['unencrypted']) {
$API->logger->logger('Still missing '.$connection->outgoing_messages[$message_id]['_'].' with message id '.$message_id." on DC {$datacenter}, resending", \danog\MadelineProto\Logger::ERROR);
$connection->methodRecall('', ['message_id' => $message_id, 'postpone' => true]);
}
} }
$connection->flush();
}
if (yield $this->waitSignal($this->pause($timeout))) {
return;
}
if ($connection->msgIdHandler->getMaxId(true) === $last_msgid && $connection->getLastChunk() === $last_chunk) {
$API->logger->logger("We did not receive a response for {$timeout} seconds: reconnecting and exiting check loop on DC {$datacenter}");
//$this->exitedLoop();
Tools::callForkDefer($connection->reconnect());
return;
} }
} }
} }

View File

@ -223,7 +223,6 @@ class ReadLoop extends SignalLoop
$connection->incoming_messages[$message_id]['content'] = $deserialized; $connection->incoming_messages[$message_id]['content'] = $deserialized;
$connection->incoming_messages[$message_id]['response'] = -1; $connection->incoming_messages[$message_id]['response'] = -1;
$connection->new_incoming[$message_id] = $message_id; $connection->new_incoming[$message_id] = $message_id;
//$connection->last_http_wait = 0;
$API->logger->logger('Received payload from DC '.$datacenter, Logger::ULTRA_VERBOSE); $API->logger->logger('Received payload from DC '.$datacenter, Logger::ULTRA_VERBOSE);
} finally { } finally {
$connection->reading(false); $connection->reading(false);

View File

@ -1870,7 +1870,11 @@ class MTProto extends AsyncConstruct implements TLCallback
*/ */
public function getConstructorCallbacks(): array public function getConstructorCallbacks(): array
{ {
return \array_merge(\array_fill_keys(['chat', 'chatEmpty', 'chatForbidden', 'channel', 'channelEmpty', 'channelForbidden'], [[$this, 'addChat']]), \array_fill_keys(['user', 'userEmpty'], [[$this, 'addUser']]), ['help.support' => [[$this, 'addSupport']]]); return \array_merge(
\array_fill_keys(['chat', 'chatEmpty', 'chatForbidden', 'channel', 'channelEmpty', 'channelForbidden'], [[$this, 'addChat']]),
\array_fill_keys(['user', 'userEmpty'], [[$this, 'addUser']]),
['help.support' => [[$this, 'addSupport']]]
);
} }
/** /**
* Called right before deserialization of object. * Called right before deserialization of object.

View File

@ -82,6 +82,18 @@ abstract class Session
* @var array * @var array
*/ */
public $ack_queue = []; public $ack_queue = [];
/**
* State request queue.
*
* @var array
*/
public $state_queue = [];
/**
* Resend request queue.
*
* @var array
*/
public $resend_queue = [];
/** /**
* Message ID handler. * Message ID handler.
* *

View File

@ -995,6 +995,8 @@ class TL
default: default:
return $x['value']; return $x['value'];
} }
} elseif ($x['_'] === 'photoStrippedSize') {
$x['inflated'] = Tools::inflateStripped($x['bytes']);
} }
if (isset($this->callbacks[TLCallback::CONSTRUCTOR_CALLBACK][$x['_']])) { if (isset($this->callbacks[TLCallback::CONSTRUCTOR_CALLBACK][$x['_']])) {
foreach ($this->callbacks[TLCallback::CONSTRUCTOR_CALLBACK][$x['_']] as $callback) { foreach ($this->callbacks[TLCallback::CONSTRUCTOR_CALLBACK][$x['_']] as $callback) {

View File

@ -116,7 +116,7 @@ function ___install_madeline()
]; ];
} }
if (defined('HHVM_VERSION')) { if (\defined('HHVM_VERSION')) {
$phpVersion = 'HHVM ' . HHVM_VERSION; $phpVersion = 'HHVM ' . HHVM_VERSION;
} else { } else {
$phpVersion = 'PHP ' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION; $phpVersion = 'PHP ' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;
@ -125,15 +125,15 @@ function ___install_madeline()
[ [
'method' => 'POST', 'method' => 'POST',
'header' => [ 'header' => [
'Content-Type: application/json', 'Content-Type: application/json',
sprintf( \sprintf(
'User-Agent: Composer/%s (%s; %s; %s; %s%s)', 'User-Agent: Composer/%s (%s; %s; %s; %s%s)',
'MadelineProto', 'MadelineProto',
function_exists('php_uname') ? @php_uname('s') : 'Unknown', \function_exists('php_uname') ? @\php_uname('s') : 'Unknown',
function_exists('php_uname') ? @php_uname('r') : 'Unknown', \function_exists('php_uname') ? @\php_uname('r') : 'Unknown',
$phpVersion, $phpVersion,
'streams', 'streams',
getenv('CI') ? '; CI' : '' \getenv('CI') ? '; CI' : ''
) )
], ],
'content' => \json_encode($postData), 'content' => \json_encode($postData),