Revert bulk aggreagation

This commit is contained in:
Alexander Pankratov 2024-07-14 21:00:58 +02:00
parent 6ef7a898ab
commit 0ad5d07d94
2 changed files with 40 additions and 8 deletions

12
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b9e9ba3184bce93fe3654528621101f4",
"content-hash": "dc5798d52e4ed25349fd1c90bf88660d",
"packages": [
{
"name": "amphp/amp",
@ -232,7 +232,7 @@
"version": "2.x-dev",
"source": {
"type": "git",
"url": "https://github.com/xtrime-ru/dns.git",
"url": "git@github.com:xtrime-ru/dns.git",
"reference": "037b762d95f7d9e855b9c6736c0ccc4adcb8921e"
},
"dist": {
@ -323,10 +323,7 @@
"dns",
"resolve"
],
"support": {
"source": "https://github.com/xtrime-ru/dns/tree/2.x"
},
"time": "2024-05-14T18:13:22+00:00"
"time": "2024-05-14T16:09:55+00:00"
},
{
"name": "amphp/file",
@ -6889,7 +6886,8 @@
"platform": {
"php": "^8.2",
"ext-json": "*",
"ext-mbstring": "*"
"ext-mbstring": "*",
"ext-curl": "*"
},
"platform-dev": [],
"plugin-api-version": "2.6.0"

View File

@ -5,6 +5,10 @@ namespace TelegramApiServer\Controllers;
use Exception;
use TelegramApiServer\Client;
use TelegramApiServer\Config;
use TelegramApiServer\Logger;
use function Amp\async;
use function Amp\delay;
use function Amp\Future\awaitAll;
final class ApiController extends AbstractApiController
{
@ -34,6 +38,36 @@ final class ApiController extends AbstractApiController
return $this->callApiCommon($madelineProto);
}
return $this->callApiCommon($madelineProto);
//GROUP REQUESTS IN BULKS
static $futures = [];
$futures[] = $future = async($this->callApiCommon(...), $madelineProto);
delay($this->waitNextTick());
if ($futures) {
awaitAll($futures);
Logger::getInstance()->notice("Executed bulk requests:" . count($futures));
$futures = [];
}
return $future->await();
}
/**
* 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;
}
}