From 37bce353d9d5d6cff2c0b6390ddaf7dd65713665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E6=97=AD=20=E8=8B=8F?= Date: Thu, 24 Oct 2019 17:27:18 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=BF=E5=85=8D=E4=BD=BF=E7=94=A8=E9=97=AD?= =?UTF-8?q?=E5=8C=85=E5=86=99=E6=B3=95=EF=BC=8C=E4=BC=9A=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimerHeap.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/TimerHeap.php b/src/TimerHeap.php index abc99d0..7f9e7be 100644 --- a/src/TimerHeap.php +++ b/src/TimerHeap.php @@ -39,7 +39,7 @@ public static function getInstance(): self return self::$instance; } - + public static function stop(): void { if (isset(self::$instance) && isset(self::$instance->receiver)) { @@ -148,6 +148,18 @@ public function delete(int $id): bool return true; } + public function tick(bool $has_registered = true): void + { + if ($has_registered) { + $this->tick_heap->extract(); + } + + // 如果队列正在执行状态,是无需再注册Tick的。因为当队列遇到一个时间未到的Task后会自动注册Tick + if ($this->receive_flag) { + $this->receiver->push(true); + } + } + protected function compare($task_id_1, $task_id_2) { $time_offset = $this->time_map[$task_id_2] - $this->time_map[$task_id_1]; @@ -172,24 +184,12 @@ private function registerTick(int $after_ms): bool } } $this->tick_heap->insert($tick_timestamp_ms); - $func = function () use ($after_ms) { + $self = $this; + \Swoole\Coroutine::create(function () use ($after_ms, $self) { \Swoole\Coroutine::sleep(round($after_ms / 1000 + 0.0005, 3)); - $this->tick(); - }; - \Swoole\Coroutine::create($func->bindTo($this)); + $self->tick(); + }); return true; } - - private function tick(bool $has_registered = true): void - { - if ($has_registered) { - $this->tick_heap->extract(); - } - - // 如果队列正在执行状态,是无需再注册Tick的。因为当队列遇到一个时间未到的Task后会自动注册Tick - if ($this->receive_flag) { - $this->receiver->push(true); - } - } }