Skip to content

Commit 7a59eea

Browse files
committed
avoid using \Swoole\Serialize for mem leak issue
1 parent 68c9ce2 commit 7a59eea

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/HttpServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function () use ($cmds, $host, $port) {
177177
]);
178178
if (! $client->connect($host, $port ?? Environment::getServiceConfig()->terminal_server_port, - 1))
179179
exit("connect failed. Error: {$client->errCode}\n");
180-
$client->send(\Swoole\Serialize::pack($cmds, SWOOLE_FAST_PACK) . "\r\n");
180+
$client->send(implode("\x1E", $cmds) . "\r\n");
181181
for($response = $client->recv(); $response; $response = $client->recv())
182182
echo $response;
183183
$client->close();

src/TerminalServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function onReceive(\Swoole\Server $server, int $fd, int $reactor_id, stri
4242
'switch' => 0
4343
]);
4444
$data = substr($data, 0, - 2);
45-
$cmds = \Swoole\Serialize::unpack($data);
45+
$cmds = explode("\x1E", $data);
4646
switch ($cmds[0]) {
4747
case 'show' :
4848
if ($cmds[1] === 'request') {

src/session.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,13 @@ public static function get(string $key) {
246246
$value = $ob->hGet($ob->sid, $key);
247247

248248
if (isset($value))
249-
$value = \Swoole\serialize::unpack($value);
249+
try {
250+
$value = \Json::decodeAsObject($value);
251+
} catch(\JsonDecodeFailException $e) {
252+
// 旧版本兼容,并试图以新格式存储
253+
$value = \Swoole\serialize::unpack($value);
254+
$ob->changed[$key] = true;
255+
}
250256
$ob->data->{$key} = $value;
251257
return $value;
252258
}
@@ -260,7 +266,13 @@ public static function has(string $key): bool {
260266
$value = $ob->hGet($ob->sid, $key);
261267

262268
if (isset($value))
263-
$value = \Swoole\serialize::unpack($value);
269+
try {
270+
$value = \Json::decodeAsObject($value);
271+
} catch(\JsonDecodeFailException $e) {
272+
// 旧版本兼容,并试图以新格式存储
273+
$value = \Swoole\serialize::unpack($value);
274+
$ob->changed[$key] = true;
275+
}
264276
$ob->data->{$key} = $value;
265277
return isset($value);
266278
}
@@ -355,7 +367,7 @@ private function saveIfNeeded(): void {
355367
foreach ($this->changed as $key=>$tmp) {
356368
$value = $this->data->{$key};
357369
if (isset($value))
358-
$set[$key] = \Swoole\serialize::pack($value);
370+
$set[$key] = \Json::encode($value);
359371
else
360372
$del[] = $key;
361373
}

0 commit comments

Comments
 (0)