Disable garbage collection logs when reading from console and polyfill from_id

This commit is contained in:
Daniil Gentili 2020-10-06 22:02:04 +02:00
parent 6eedd54ad3
commit 7e281c1589
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
7 changed files with 64 additions and 15 deletions

View File

@ -80,6 +80,7 @@ class MyEventHandler extends EventHandler
return;
}
$res = \json_encode($update, JSON_PRETTY_PRINT);
yield $this->messages->sendMessage(['peer' => $update, 'message' => "<code>$res</code>", 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'parse_mode' => 'HTML']);
if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') {

View File

@ -272,4 +272,9 @@ final class Coroutine implements Promise, \ArrayAccess
}
return [];
}
public function __debugInfo()
{
return ['_' => 'To obtain a result from a Coroutine object, yield the result or disable async (not recommended). See https://docs.madelineproto.xyz/docs/ASYNC.html for more information on async.'];
}
}

View File

@ -212,7 +212,22 @@ class FeedLoop extends ResumableSignalLoop
$from = false;
$via_bot = false;
$entities = false;
if ($update['message']['_'] !== 'messageEmpty' && (($from = isset($update['message']['from_id']) && !(yield from $this->API->peerIsset($update['message']['from_id']))) || ($to = !(yield from $this->API->peerIsset($update['message']['peer_id']))) || ($via_bot = isset($update['message']['via_bot_id']) && !(yield from $this->API->peerIsset($update['message']['via_bot_id']))) || ($entities = isset($update['message']['entities']) && !(yield from $this->API->entitiesPeerIsset($update['message']['entities']))))) {
if ($update['message']['_'] !== 'messageEmpty' && (
(
$from = isset($update['message']['from_id'])
&& !(yield from $this->API->peerIsset($update['message']['from_id']))
) || (
$to = !(yield from $this->API->peerIsset($update['message']['peer_id']))
)
|| (
$via_bot = isset($update['message']['via_bot_id'])
&& !(yield from $this->API->peerIsset($update['message']['via_bot_id']))
) || (
$entities = isset($update['message']['entities'])
&& !(yield from $this->API->entitiesPeerIsset($update['message']['entities']))
)
)
) {
$log = '';
if ($from) {
$from_id = $this->API->getId($update['message']['from_id']);

View File

@ -26,6 +26,11 @@ class GarbageCollector
*/
public static int $memoryDiffMb = 1;
/**
* Whether to enable logging.
*/
public static bool $log = false;
/**
* Memory consumption after last cleanup.
* @var int
@ -45,7 +50,9 @@ class GarbageCollector
\gc_collect_cycles();
static::$memoryConsumption = static::getMemoryConsumption();
$cleanedMemory = $currentMemory - static::$memoryConsumption;
Logger::log("gc_collect_cycles done. Cleaned memory: $cleanedMemory Mb", Logger::VERBOSE);
if (static::$log) {
Logger::log("gc_collect_cycles done. Cleaned memory: $cleanedMemory Mb", Logger::VERBOSE);
}
}
});
}
@ -53,7 +60,9 @@ class GarbageCollector
private static function getMemoryConsumption(): int
{
$memory = \round(\memory_get_usage()/1024/1024, 1);
Logger::log("Memory consumption: $memory Mb", Logger::ULTRA_VERBOSE);
if (static::$log) {
Logger::log("Memory consumption: $memory Mb", Logger::ULTRA_VERBOSE);
}
return (int) $memory;
}
}

View File

@ -386,8 +386,10 @@ trait PeerHandler
return $this->toSupergroup($id['channel_id']);
case 'message':
case 'messageService':
if (!isset($id['from_id'])
if (!isset($id['from_id']) // No other option
// It's a channel/chat, 100% what we need
|| $id['peer_id']['_'] !== 'peerUser'
// It is a user, and it's not ourselves
|| $id['peer_id']['user_id'] !== $this->authorization['user']['id']
) {
return $this->getId($id['peer_id']);

View File

@ -412,9 +412,20 @@ trait UpdateHandler
if (isset($update['message']['_']) && $update['message']['_'] === 'messageEmpty') {
return;
}
if (isset($update['message']['from_id']['user_id']) && $update['message']['from_id']['user_id'] === $this->authorization['user']['id']) {
if (isset($update['message']['from_id']['user_id'])) {
if ($update['message']['from_id']['user_id'] === $this->authorization['user']['id']) {
$update['message']['out'] = true;
}
} elseif (!isset($update['message']['from_id'])
&& isset($update['message']['peer_id']['user_id'])
&& $update['message']['peer_id']['user_id'] === $this->authorization['user']['id']) {
$update['message']['out'] = true;
}
if (!isset($update['message']['from_id']) && isset($update['message']['peer_id']['user_id'])) {
$update['message']['from_id'] = $update['message']['peer_id'];
}
if (isset($update['message']['peer_id'])) {
$update['message']['to_id'] = $update['message']['peer_id'];
}

View File

@ -26,6 +26,7 @@ use Amp\Loop;
use Amp\Promise;
use Amp\Success;
use Amp\TimeoutException;
use danog\MadelineProto\MTProtoTools\GarbageCollector;
use tgseclib\Math\BigInteger;
use function Amp\ByteStream\getOutputBufferStream;
use function Amp\ByteStream\getStdin;
@ -690,16 +691,21 @@ abstract class Tools extends StrTools
*/
public static function readLineGenerator(string $prompt = ''): \Generator
{
$stdin = getStdin();
$stdout = getStdout();
if ($prompt) {
yield $stdout->write($prompt);
}
static $lines = [''];
while (\count($lines) < 2 && ($chunk = yield $stdin->read()) !== null) {
$chunk = \explode("\n", \str_replace(["\r", "\n\n"], "\n", $chunk));
$lines[\count($lines) - 1] .= \array_shift($chunk);
$lines = \array_merge($lines, $chunk);
try {
GarbageCollector::$log = false;
$stdin = getStdin();
$stdout = getStdout();
if ($prompt) {
yield $stdout->write($prompt);
}
static $lines = [''];
while (\count($lines) < 2 && ($chunk = yield $stdin->read()) !== null) {
$chunk = \explode("\n", \str_replace(["\r", "\n\n"], "\n", $chunk));
$lines[\count($lines) - 1] .= \array_shift($chunk);
$lines = \array_merge($lines, $chunk);
}
} finally {
GarbageCollector::$log = true;
}
return \array_shift($lines);
}