mix-php / mix

☄️ PHP CLI mode development framework, supports Swoole, WorkerMan, FPM, CLI-Server / PHP 命令行模式开发框架,支持 Swoole、Swow、WorkerMan、FPM、CLI-Server
https://openmix.org/mix-php
Apache License 2.0
1.94k stars 213 forks source link

数据量较多时,json_decode可能会失败 #75

Closed peinhu closed 4 years ago

peinhu commented 4 years ago
Server         Name:      mix-httpd
System         Name:      linux
PHP            Version:   7.2.17
Swoole         Version:   4.4.16
Framework      Version:   2.1.9
Listen         Addr:      0.0.0.0
Listen         Port:      7777

需求:json_decode能正确处理数据 实际:json_decode处理json中大量数据(大概20多条)时有可能会失败,导致报non-object错误,具体表现为前两次请求中运行成功,第三次开始失败,一旦失败过一次后续都无法成功

[notice] 2020-03-31 13:39:12.613 <3255> Trying to get property 'health_data' of non-object
[code] 8 [type] Mix\Console\Exception\ErrorException
[file] in /vagrant/mixtest/app/Http/Controllers/BandController.php on line 83
 #0 Mix\Console\Error->appError(8, 'Trying to get p...', '/vagrant/mixtes...', 83, Array) in /vagrant/mixtest/app/Http/Controllers/BandController.php on line 83
 #1 [internal function]: App\Http\Controllers\BandController->uploadData(Object(Mix\Http\Message\ServerRequest), Object(Mix\Http\Message\Response))
 #2 call_user_func(Array, Object(Mix\Http\Message\ServerRequest), Object(Mix\Http\Message\Response)) in /vagrant/mixtest/app/Http/Commands/StartCommand.php on line 131
 #3 App\Http\Commands\StartCommand->handle(Object(Mix\Http\Message\ServerRequest), Object(Mix\Http\Message\Response)) in /vagrant/mixtest/app/Http/Commands/StartCommand.php on line 85
 #4 [internal function]: App\Http\Commands\StartCommand->App\Http\Commands\{closure}(Object(Mix\Http\Message\ServerRequest), Object(Mix\Http\Message\Response))
 #5 call_user_func(Object(Closure), Object(Mix\Http\Message\ServerRequest), Object(Mix\Http\Message\Response)) in /vagrant/mixtest/vendor/mix/http-server/src/Server.php on line 107
 #6 [internal function]: Mix\Http\Server\Server->Mix\Http\Server\{closure}(Object(Swoole\Http\Request), Object(Swoole\Http\Response))
 #7 {main}

无法确定是swoole还是框架或者是我使用上的问题,同样的请求、同样的代码换用slim框架后一切正常。

onanying commented 4 years ago

@peinhu 我这里 ab 压测没有复现出来,但是我觉得应该可以通过以下方式看能不能解决,把这两个 swoole 的默认值调更大一些

onanying commented 4 years ago

@peinhu 已经确认是 swoole 的 bug https://github.com/swoole/swoole-src/issues/3079 , swoole v4.4.16 已经修复该问题。

peinhu commented 4 years ago

我用的就是swoole v4.4.16,还是有这个问题

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.4.16
Built => Feb 19 2020 08:39:09
coroutine => enabled
trace_log => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 1.0.1e-fips 11 Feb 2013
http2 => enabled
pcre => enabled
zlib => 1.2.3
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

复现代码:

//mixphp 
//TestController.php
<?php

namespace App\Http\Controllers;

use App\Http\Helpers\ResponseHelper;
use Mix\Http\Message\ServerRequest;
use Mix\Http\Message\Response;

class TestController
{
    public function uploadData(ServerRequest $request, Response $response)
    { 
        $data = $request->getAttribute('data');
        $healthData = json_decode($data)->health_data;

        $content = ['code' => 0, 'message' => 'OK'];

        return ResponseHelper::json($response, $content);
    }

}
<?php
//mixphp 
//route.php
return [

    // 路由
    [
        // 名称
        'name'       => 'route',
        // 类路径
        'class'      => \Mix\Route\Router::class,
        // 初始方法
        'initMethod' => 'parse',
        // 属性注入
        'properties' => [
            // 默认变量规则
            'defaultPattern' => '[\w-]+',
            // 路由变量规则
            'patterns'       => [
                'id' => '\d+',
            ],
            // 全局中间件
            'middleware'     => [\App\Http\Middleware\GlobalMiddleware::class],
            // 路由规则
            'rules'          => [

                '/api'               => [
                    // 分组中间件
                    'middleware' => [\App\Http\Middleware\GroupMiddleware::class],
                    // 分组路由规则
                    'rules'      => [
                        'POST /test/upload_data' => [[\App\Http\Controllers\TestController::class, 'uploadData']],
                    ],
                ],
            ],
        ],
    ],

];

//data参数,post提交 {"health_data":[{"mac":"e0e4473f7311","aaaa":"36.7","bbbb":"0","cccccc":"8","dddddd":"0"},{"mac":"f52b02f12122","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121aa","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121bb","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121cc","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121dd","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121ee","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121ff","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121gg","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121hh","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121ii","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121jj","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121kk","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121ll","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121mm","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"},{"mac":"f52b02f121nn","aaaa":"36.7","bbbb":"0","cccccc":"0","dddddd":"0"}]}

peinhu commented 4 years ago

多提交几次就会出现,我是一般第三次提交会出现,也有可能第二次出现。

onanying commented 4 years ago

这个bug 和框架无关的,我现在这里压测 4.4.16 已经无法复现,你可以用原生代码试试,用 ab 压测:

<?php

function run($func)
{
    $scheduler = new \Swoole\Coroutine\Scheduler;
    $scheduler->set([
        'hook_flags' => SWOOLE_HOOK_ALL,
    ]);
    $scheduler->add(function () use ($func) {
        call_user_func($func);
    });
    $scheduler->start();
}

run(function(){

    $server = new \Swoole\Coroutine\Http\Server("0.0.0.0", 9502, false);
    $server->handle('/', function ($request, $response) {

        var_dump(strlen($request->rawContent()));

        $response->end("<h1>Index</h1>");
    });
    $server->start();

});
onanying commented 4 years ago

@peinhu 我在 linux 用原生和框架都 ab 压测没有复现,这个问题不在处理

System         Name:      linux
PHP            Version:   7.2.12
Swoole         Version:   4.4.16
Framework      Version:   2.1.15
onanying commented 4 years ago

@peinhu 新的版本中 json 会被自动解析,也就是说你的第二行那里会抛出异常,因为已经转换 https://github.com/mix-php/mix/blob/master/src/http-message/src/Factory/ServerRequestFactory.php#L109

$data = $request->getAttribute('data');
$healthData = json_decode($data)->health_data;