mirror of
https://github.com/danog/TelegramApiServer.git
synced 2024-11-26 11:54:42 +01:00
Proper bulking
This commit is contained in:
parent
55775897d6
commit
1101f65388
@ -14,8 +14,6 @@ use ReflectionProperty;
|
||||
use Revolt\EventLoop;
|
||||
use RuntimeException;
|
||||
use TelegramApiServer\EventObservers\EventObserver;
|
||||
use function Amp\async;
|
||||
use function Amp\delay;
|
||||
|
||||
final class Client
|
||||
{
|
||||
@ -211,10 +209,11 @@ final class Client
|
||||
$resume = Config::getInstance()->get('error.resume_on_error');
|
||||
|
||||
$currentHandler = EventLoop::getErrorHandler();
|
||||
EventLoop::setErrorHandler(static fn(\Throwable $e) => self::errorHandler($e, $currentHandler, $token, $peers, $resume));
|
||||
EventLoop::setErrorHandler(static fn (\Throwable $e) => self::errorHandler($e, $currentHandler, $token, $peers, $resume));
|
||||
}
|
||||
|
||||
private static function errorHandler(\Throwable $e, ?callable $currentHandler, string $token, array $peers, bool $resume): void {
|
||||
private static function errorHandler(\Throwable $e, ?callable $currentHandler, string $token, array $peers, bool $resume): void
|
||||
{
|
||||
if ($currentHandler) {
|
||||
$currentHandler($e);
|
||||
}
|
||||
@ -223,33 +222,32 @@ final class Client
|
||||
}
|
||||
if ($peers && $token) {
|
||||
try {
|
||||
$ch = curl_init("https://api.telegram.org/bot$token/sendMessage");
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$ch = \curl_init("https://api.telegram.org/bot$token/sendMessage");
|
||||
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
||||
\curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
\curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
|
||||
\curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
|
||||
foreach ($peers as $peer) {
|
||||
$exceptionArray = Logger::getExceptionAsArray($e);
|
||||
unset($exceptionArray['previous_exception']);
|
||||
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
|
||||
\curl_setopt($ch, CURLOPT_POSTFIELDS, \json_encode([
|
||||
'chat_id' => $peer,
|
||||
'text' => "```json\n" .
|
||||
json_encode($exceptionArray, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT) .
|
||||
"\n```"
|
||||
,
|
||||
\json_encode($exceptionArray, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT) .
|
||||
"\n```",
|
||||
'parse_mode' => 'MarkdownV2',
|
||||
]));
|
||||
|
||||
$response = curl_exec($ch);
|
||||
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
|
||||
$response = \curl_exec($ch);
|
||||
if (\curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
|
||||
Logger::getInstance()->error('Error notification bot response', [
|
||||
'response' => $response,
|
||||
'error_code' => curl_errno($ch),
|
||||
'error' => curl_error($ch),
|
||||
'error_code' => \curl_errno($ch),
|
||||
'error' => \curl_error($ch),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -3,14 +3,11 @@
|
||||
namespace TelegramApiServer\Controllers;
|
||||
|
||||
use Amp\DeferredFuture;
|
||||
use Amp\Future;
|
||||
use Exception;
|
||||
use Revolt\EventLoop;
|
||||
use TelegramApiServer\Client;
|
||||
use TelegramApiServer\Config;
|
||||
use TelegramApiServer\Logger;
|
||||
use function Amp\async;
|
||||
use function Amp\delay;
|
||||
use function Amp\Future\await;
|
||||
use function Amp\Future\awaitAll;
|
||||
|
||||
final class ApiController extends AbstractApiController
|
||||
{
|
||||
@ -28,6 +25,7 @@ final class ApiController extends AbstractApiController
|
||||
$this->api = \explode('.', $path['method'] ?? '');
|
||||
}
|
||||
|
||||
private static ?Future $w = null;
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
@ -40,40 +38,16 @@ final class ApiController extends AbstractApiController
|
||||
return $this->callApiCommon($madelineProto);
|
||||
}
|
||||
|
||||
//GROUP REQUESTS IN BULKS
|
||||
/** @var ?DeferredFuture $lock */
|
||||
static $lock = null;
|
||||
|
||||
if (!$lock) {
|
||||
try {
|
||||
$lock = new DeferredFuture();
|
||||
delay($this->waitNextTick());
|
||||
$lock->complete();
|
||||
} finally {
|
||||
$lock = null;
|
||||
}
|
||||
} else {
|
||||
$lock->getFuture()->await();
|
||||
if (!self::$w) {
|
||||
$f = new DeferredFuture;
|
||||
self::$w = $f->getFuture();
|
||||
EventLoop::delay(0.001, static function () use ($f): void {
|
||||
self::$w = null;
|
||||
$f->complete();
|
||||
});
|
||||
}
|
||||
self::$w->await();
|
||||
|
||||
return $this->callApiCommon($madelineProto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync threads execution via time ticks
|
||||
* Need to enable madelineProto futures bulk execution
|
||||
* @param float $tick interval of execution in seconds.
|
||||
*/
|
||||
protected function waitNextTick(float $tick = 0.5): float {
|
||||
$tickMs = (int)($tick * 1000);
|
||||
$now = (int)(microtime(true) * 1000);
|
||||
$currentTick = intdiv((int)(microtime(true) * 1000), $tickMs);
|
||||
$nextTick = ($currentTick + 1);
|
||||
$nextTickTime = $nextTick * $tickMs;
|
||||
$wait = round(($nextTickTime - $now)/1000, 3);
|
||||
|
||||
Logger::getInstance()->notice("Waiting $wait seconds before tick");
|
||||
|
||||
return $wait;
|
||||
}
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ final class Logger extends AbstractLogger
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine(),
|
||||
'code' => $exception->getCode(),
|
||||
'backtrace' => array_slice($exception->getTrace(), 0, 3),
|
||||
'backtrace' => \array_slice($exception->getTrace(), 0, 3),
|
||||
'previous_exception' => $exception->getPrevious(),
|
||||
];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user