Skip to content

Commit

Permalink
避免使用闭包写法,会导致内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
fdreamsu committed Oct 24, 2019
1 parent 68d9b29 commit 37bce35
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/TimerHeap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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];
Expand All @@ -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);
}
}
}

0 comments on commit 37bce35

Please sign in to comment.