请教webman2.1源码数据库连接池的实现原理和协程文档章节提到的,使用协程驱动时回调会自动创建一个协程来执行的疑问

彭彭

请教webman2.1源码,数据库连接池的实现原理

webman2.1支持了协程,文档里数据库这节,写到“每个进程有自己的连接池,进程间不共享连接池。”

webman 1版本时,数据库启动是在onWorkerStart里里加载LaravelDb.php,从而启动数据库,每个进程一个数据库连接。

1、请教社区大牛,2.1版本是怎么做到每个进程有自己的连接池,进程间不共享连接池的?源码看不懂,有没有大牛帮忙解析分析一下关键代码。

2、文档提到,当使用Swoole Swow Fiber驱动时,workerman每次运行onWorkerStart onMessage onConnect onClose等回调时会自动创建一个协程来执行。源码中Worker.php中

public function run(): void
    {
        $this->listen();

        if (!$this->onWorkerStart) {
            return;
        }

        // Try to emit onWorkerStart callback.
        $callback = function() {
            try {
                ($this->onWorkerStart)($this);
            } catch (Throwable $e) {
                // Avoid rapid infinite loop exit.
                sleep(1);
                static::stopAll(250, $e);
            }
        };

        switch (Worker::$eventLoopClass) {
            case Swoole::class:
            case Swow::class:
            case Fiber::class:
                Coroutine::create($callback);
                break;
            default:
                (new \Fiber($callback))->start();
        }
    }

只有run方法中的onWorkerStart回调中新建了协程,没有看到其他回调中新建协程,请大佬指教!!!
谢谢!!!

125 0 0
0个回答

🔝