Skip to content

Commit

Permalink
Update EventLoop.php
Browse files Browse the repository at this point in the history
  • Loading branch information
VennDev authored Aug 12, 2024
1 parent 169caee commit 1ea61f6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/vennv/vapm/simultaneous/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class EventLoop implements EventLoopInterface
* @var array<int, Promise>
*/
protected static array $returns = [];
protected static bool $isCleaningGarbage = false;

public static function init(): void
{
Expand Down Expand Up @@ -175,10 +174,15 @@ protected static function run(): void
{
if (count(GreenThread::getFibers()) > 0) GreenThread::run();

$i = 0;
$limit = min((int)((count(self::$queues) / 2) + 1), 100); // Limit 100 promises per loop

/**
* @var Promise $promise
*/
foreach (self::getQueues() as $promise) {
if ($i >= $limit) break;

$id = $promise->getId();
$fiber = $promise->getFiber();

Expand All @@ -196,7 +200,12 @@ protected static function run(): void
}
MicroTask::addTask($id, $promise);
self::$queues->offsetUnset($promise); // Remove from queue
} else {
self::$queues->detach($promise); // Remove from queue
self::$queues->attach($promise, $id); // Add to queue again
}

$i++;
}

if (count(MicroTask::getTasks()) > 0) MicroTask::run();
Expand Down

0 comments on commit 1ea61f6

Please sign in to comment.