Skip to content

Commit 37bce35

Browse files
committed
避免使用闭包写法,会导致内存泄漏
1 parent 68d9b29 commit 37bce35

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/TimerHeap.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function getInstance(): self
3939

4040
return self::$instance;
4141
}
42-
42+
4343
public static function stop(): void
4444
{
4545
if (isset(self::$instance) && isset(self::$instance->receiver)) {
@@ -148,6 +148,18 @@ public function delete(int $id): bool
148148
return true;
149149
}
150150

151+
public function tick(bool $has_registered = true): void
152+
{
153+
if ($has_registered) {
154+
$this->tick_heap->extract();
155+
}
156+
157+
// 如果队列正在执行状态,是无需再注册Tick的。因为当队列遇到一个时间未到的Task后会自动注册Tick
158+
if ($this->receive_flag) {
159+
$this->receiver->push(true);
160+
}
161+
}
162+
151163
protected function compare($task_id_1, $task_id_2)
152164
{
153165
$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
172184
}
173185
}
174186
$this->tick_heap->insert($tick_timestamp_ms);
175-
$func = function () use ($after_ms) {
187+
$self = $this;
188+
\Swoole\Coroutine::create(function () use ($after_ms, $self) {
176189
\Swoole\Coroutine::sleep(round($after_ms / 1000 + 0.0005, 3));
177-
$this->tick();
178-
};
179-
\Swoole\Coroutine::create($func->bindTo($this));
190+
$self->tick();
191+
});
180192

181193
return true;
182194
}
183-
184-
private function tick(bool $has_registered = true): void
185-
{
186-
if ($has_registered) {
187-
$this->tick_heap->extract();
188-
}
189-
190-
// 如果队列正在执行状态,是无需再注册Tick的。因为当队列遇到一个时间未到的Task后会自动注册Tick
191-
if ($this->receive_flag) {
192-
$this->receiver->push(true);
193-
}
194-
}
195195
}

0 commit comments

Comments
 (0)