Skip to content

Commit

Permalink
Update Select.php
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor authored Jul 31, 2022
1 parent 47f495f commit 270cfe8
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions Events/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/
namespace Workerman\Events;

use Throwable;
use Workerman\Worker;

/**
* select eventloop
*/
Expand Down Expand Up @@ -211,6 +214,7 @@ public function del($fd, $flag)
*/
protected function tick()
{
$tasks_to_insert = [];
while (!$this->_scheduler->isEmpty()) {
$scheduler_data = $this->_scheduler->top();
$timer_id = $scheduler_data['data'];
Expand All @@ -228,14 +232,28 @@ protected function tick()
$task_data = $this->_eventTimer[$timer_id];
if ($task_data[2] === self::EV_TIMER) {
$next_run_time = $time_now + $task_data[3];
$this->_scheduler->insert($timer_id, -$next_run_time);
$tasks_to_insert[] = [$timer_id, -$next_run_time];
}
try {
\call_user_func_array($task_data[0], $task_data[1]);
} catch (Throwable $e) {
Worker::stopAll(250, $e);
}
\call_user_func_array($task_data[0], $task_data[1]);
if (isset($this->_eventTimer[$timer_id]) && $task_data[2] === self::EV_TIMER_ONCE) {
$this->del($timer_id, self::EV_TIMER_ONCE);
}
continue;
} else {
break;
}
}
foreach ($tasks_to_insert as $item) {
$this->_scheduler->insert($item[0], $item[1]);
}
if (!$this->_scheduler->isEmpty()) {
$scheduler_data = $this->_scheduler->top();
$next_run_time = -$scheduler_data['priority'];
$time_now = \microtime(true);
$this->_selectTimeout = \max((int) (($next_run_time - $time_now) * 1000000), 0);
return;
}
$this->_selectTimeout = 100000000;
Expand Down Expand Up @@ -275,10 +293,8 @@ public function loop()

} else {
$this->_selectTimeout >= 1 && usleep($this->_selectTimeout);
$ret = false;
}


if (!$this->_scheduler->isEmpty()) {
$this->tick();
}
Expand Down

0 comments on commit 270cfe8

Please sign in to comment.