自定义进程中数据查询数据库连接操时

gzahz1

问题描述

使用webman2.1后,http服务进程数据库操作正常,自定义进程中首次查询数据正常,等待3分钟左右后,数据库连接操时:SQLSTATE[HY000]: General error: 2006 MySQL server has gone away。

1.http服务进程因请求结束会调用 Context::destroy() 能正常将连接回收放入连接池,数据库连接心跳正常,所以没有导致连接超时。自定义进程中我是用来做异步执行定时任务的,经过排查发现自定义进程中,首次查询数据心跳是正常执行,定时几分钟再查询数据后,从此心跳不在执行,如果在等几分钟再次查询数据,没有心跳维持连接就会报数据库连接操时了。请帮忙看看。

2.缓存尚未测试,我看缓存连接池实现也是差不多,不知道在自定义进程中是否也会导致链接操时

程序代码

//进程
return [
    'webman' => [
        'handler' => Http::class,
        'listen' => 'http://0.0.0.0:8787',
        'count' => cpu_count() * 4,
        'user' => '',
        'group' => '',
        'reusePort' => false,
        'eventLoop' => '',
        'context' => [],
        'constructor' => [
            'requestClass' => Request::class,
            'logger' => Log::channel('default'),
            'appPath' => app_path(),
            'publicPath' => public_path()
        ]
    ],
    //自定义进程
     'http2' => [
        'handler' => Http2::class,
        'listen' => 'tcp://127.0.0.1:8856',
        'count' => 1,
        'reusePort' => false
    ],
    // File update detection and automatic reload
    'monitor' => [
        'handler' => app\process\Monitor::class,
        'reloadable' => false,
        'constructor' => [
            // Monitor these directories
            'monitorDir' => array_merge([
                app_path(),
                config_path(),
                base_path() . '/process',
                base_path() . '/support',
                base_path() . '/resource',
                base_path() . '/.env',
            ], glob(base_path() . '/plugin/*/app'), glob(base_path() . '/plugin/*/config'), glob(base_path() . '/plugin/*/api')),
            // Files with these suffixes will be monitored
            'monitorExtensions' => [
                'php', 'html', 'htm', 'env'
            ],
            'options' => [
                'enable_file_monitor' => !in_array('-d', $argv) && DIRECTORY_SEPARATOR === '/',
                'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/',
            ]
        ]
    ]
];

//Http2::class代码

class Http2
{
    public function onWorkerStart(Worker $worker): void
    {
        D('启动');

        //查询数据 - 查询各方面这里正常
        $mode = SystemCrontab::find(1);
        D('启动时首次查询数据', (array)$mode);

        //等待中一直观察数据库链接心跳正常触发

        //200秒执行查询一次测试
        Timer::add(200, function () {

            $mode = SystemCrontab::find(1);
            D('200秒查询数据', (array)$mode);

            //必须增加 Context::destroy(); 否则下一个200秒在查询导致数据库连接操时,从这里开始不会再执行心跳
            Context::destroy();

            //说明:在次查询会导致数据库连接操时,目前暂时解决是:定时50秒执行一次 Db::query('select 1'); 或者 在该进程中所有查询数据后执行 Context::destroy();

        });

    }
}

报错信息

截图报错信息里报错文件相关代码

操作系统及workerman/webman等框架组件具体版本

"require": {
    "php": ">=8.2",
    "workerman/webman-framework": "^2.1",
    "workerman/crontab": "^1.0",
    "monolog/monolog": "^2.0",
    "webman/event": "^1.0",
    "webman/console": "^2.1",
    "webman/log": "^2.1",
    "symfony/translation": "^6.4",
    "webman/redis": "^2.1",
    "illuminate/container": "^11.40",
    ....
},
151 1 0
1个回答

composer require workerman/coroutine:^1.1.3

升级下

  • gzahz1 4天前

    感谢大佬!测试一切正常

×
🔝