Skip to content

Commit

Permalink
部分场景 JSON 操作出错抛出异常、不转义“/”和 Unicode (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft authored Oct 30, 2021
1 parent f0851e2 commit 9a9da51
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/Cron/CronManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function getTaskCallable(string $cronId, string $class, ?string &$cronTyp
$task = function (string $id, $data) use ($class) {
ProcessManager::run('CronWorkerProcess', [
'id' => $id,
'data' => json_encode($data),
'data' => json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE),
'class' => $class,
'cron-sock' => $this->getSocketFile(),
], null, null, $this->stdOutput);
Expand Down Expand Up @@ -234,7 +234,7 @@ public function getTaskCallable(string $cronId, string $class, ?string &$cronTyp
$task = function (string $id, $data) use ($process) {
ProcessManager::run($process->name, [
'id' => $id,
'data' => json_encode($data),
'data' => json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE),
'cron-sock' => $this->getSocketFile(),
], null, null, $this->stdOutput);
};
Expand Down
4 changes: 2 additions & 2 deletions src/Cron/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function run(ITaskParam $param)
'data' => $task->getData(),
'task' => \is_callable($taskClass) ? null : $taskClass,
'type' => $type,
]), mt_rand(0, $swooleServer->setting['worker_num'] - 1));
], \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE), mt_rand(0, $swooleServer->setting['worker_num'] - 1));
break;
case CronTaskType::ALL_WORKER:
/** @var ISwooleServer $server */
Expand All @@ -116,7 +116,7 @@ public function run(ITaskParam $param)
'data' => $task->getData(),
'task' => \is_callable($taskClass) ? null : $taskClass,
'type' => $type,
]);
], \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
for ($i = 0; $i < $swooleServer->setting['worker_num']; ++$i)
{
$swooleServer->sendMessage($message, $i);
Expand Down
8 changes: 1 addition & 7 deletions src/Process/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,7 @@ private function startWorker($workerId)

return;
}
$data = json_decode($content, true);
if (false === $data)
{
Log::warning('%s: Decode pipe message content failed');

return;
}
$data = json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
$this->trigger('Message', [
'pool' => $this,
'worker' => $worker,
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Process extends \Swoole\Process
public function sendMessage(string $action, array $data = [])
{
$data['a'] = $action;
$message = json_encode($data);
$message = json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);

return $this->write($message);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Util/AmqpServerUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function sendAmqpMessage(string $action, array $data = [], string $routin
{
$data['action'] = $action;
$data['workerId'] = Worker::getWorkerId();
$message = json_encode($data);
$message = json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
$amqpMessage = new Message();
$amqpMessage->setBody($message);
$amqpMessage->setRoutingKey($routingKey);
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Util/LocalServerUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class LocalServerUtil implements ISwooleServerUtil
public function sendMessage(string $action, array $data = [], $workerId = null): int
{
$data['action'] = $action;
$message = json_encode($data);
$message = json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);

return $this->sendMessageRaw($message, $workerId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Util/RedisServerUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function sendMessage(string $action, array $data = [], $workerId = null):
{
$data['action'] = $action;
$data['workerId'] = Worker::getWorkerId();
$message = json_encode($data);
$message = json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);

return $this->sendMessageRaw($message, $workerId);
}
Expand Down

0 comments on commit 9a9da51

Please sign in to comment.