高性能PHP框架webman协程Redis动态连接池

开发 前端
在webman框架的控制器使用的时候,存在会被多次调用Pool::create()方法多次创建重复区域会抛出异常,可以在config/bootstrap.php中占位初始化。

🐞 简介

 webman-coroutine 是一个 webman 开发框架生态下的协程基建支撑插件

🐇 起源

  • workerman 4.x 及基于其作为运行容器的 webman 框架不支持协程
  • workerman 5.x 及基于其作为运行容器的 webman 框架不具备完备的协程能力
  • workerman / webman 没有一个较为统一的协程使用方式,导致切换协程驱动的开发成本较高,试错成本较高
  • 自行实现协程版worker、server开发成本较高,试错成本较高

🕷️ 目的

  • 提供 workerman/webman 多样的基础协程事件库,兼容支持workerman 4.x和workerman 5.x的协程驱动

revolt/PHP-fiber

swow

swoole

ripple

  • 提供 workerman/webman 统一的协程开发工具,兼容非协程环境
  • 协程通道:Utils/Channel
  • 协程等待:Utils/WaitGroup
  • 协程:Utils/Coroutine
  • 协程化Worker:Utils/Worker
  • 对象池:Utils/Pool

🐌 愿景

  1. 在 workerman/webman 开发环境下,提供一套简单的协程工具包,降低认知负荷。
  2. 在 workerman/webman 开发环境下,尝试实现一套兼容协程与非协程开发的方案,让选择和摆脱方案更简单,避免更多的焦虑。
  3. 在 workerman/webman 开发环境下,尽可能实现对官方组件的无侵入式协程化改造(虽然很难,但也想试试)。
  4. 希望在代码的实现上能够给更多PHP开发带来一些帮助,甚至灵感。

🪰 安装

通过composer安装

composer require workbunny/webman-coroutine

安装日志

# composer require workbunny/webman-coroutine
./composer.json has been updated
Running composer update workbunny/webman-coroutine
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Upgrading workbunny/webman-coroutine (1.1.4 => 1.2.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 4 installs, 0 updates, 1 removal
  - Downloading tinywan/exception-handler (v1.5.4)
  - Downloading workbunny/webman-coroutine (1.2.1)
> support\Plugin::uninstall
  - Removing openspout/openspout (v4.26.0)
  - Installing composer/semver (3.4.3): Extracting archive
  - Installing swow/swow (v1.5.3): Extracting archive
  - Installing tinywan/exception-handler (v1.5.4): Extracting archive
  - Installing workbunny/webman-coroutine (1.2.1): Extracting archive
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
Create config/plugin/workbunny/webman-coroutine
Generating autoload files
55 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
Using version ^1.2 for workbunny/webman-coroutine

🦠安装 swow

通过composer安装

composer require swow/swow

使用./vendor/bin/swow-builder安装swow拓展

vendor/bin/swow-builder --rebuild --install
> cd /var/www/demo.webman.tinywan.com/vendor/swow/swow/ext && \
phpize --clean
Cleaning..
✅ Clean done

> cd /var/www/demo.webman.tinywan.com/vendor/swow/swow/ext && \
phpize && \
./configure
Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831
checking for grep that handles long lines and -e... /bin/grep

Directive => Local Value => Master Value
swow.enable => On => On
swow.async_threads => 0 => 0
swow.async_file => On => On
swow.async_tty => On => On

> cd /var/www/demo.webman.tinywan.com/vendor/swow/swow/ext && \
make install
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20230831/
✅ Install done

🚀🚀🚀 All tasks have been completed 🚀🚀🚀

🐜 webman 使用

1. 配置config/server.php

修改事件驱动为event_loop(),event_loop()用于自动判断当前环境适合的event loop和协程驱动

'event_loop' => \Workbunny\WebmanCoroutine\event_loop(),

2. 占位初始化

在webman框架的控制器使用的时候,存在会被多次调用Pool::create()方法多次创建重复区域会抛出异常,可以在config/bootstrap.php中占位初始化。新增如下配置

<?php
return [
    // 其他配置
    \app\common\CoroutinePool::class
];

CoroutinePool.php协程连接池

<?php
/**
 * @desc CoroutinePool.php 描述信息
 * @author Tinywan(ShaoBo Wan)
 */
declare(strict_types=1);

namespace app\common;

use Webman\Bootstrap;
use Workbunny\WebmanCoroutine\Utils\Pool\Pool;
use Workerman\Worker;

class CoroutinePool implements Bootstrap
{
    /***
     * @desc start
     * @param Worker|null $worker
     * @author Tinywan(ShaoBo Wan)
     */
    public static function start(?Worker $worker)
    {
        /** 为redis占位 */
        Pool::init('redis',false);
    }
}

3. 动态池化

CoroutineController控制器伪代码

<?php
/**
 * @author Tinywan(ShaoBo Wan)
 * @date 2024/10/17 23:37
 */
declare(strict_types=1);

namespace app\controller;

use Illuminate\Redis\Connections\Connection;
use support\Response;
use Workbunny\WebmanCoroutine\Utils\Pool\Pool;

class CoroutineController
{
    /***
     * @desc Redis 连接池
     * @author Tinywan(ShaoBo Wan)
     */
    public function pool(): Response
    {
        /** 获取redis占位 */
        $pools = Pool::get('redis', null);
        /** 根据配置池大小判断是否需要追加,假设你存在这样的配置 */
        if (count($pools) < config('redis.pool_size')) {
            /** 新建redis连接 */
            $config = config('redis');
            $redis = new \Illuminate\Redis\RedisManager('', 'phpredis', $config);
            /** 追加一个redis连接对象,资源类型不用clone */
            Pool::append('redis', (int)array_key_last($pools) + 1, $redis, false);
        }

        $start = microtime(true);
        /** 等待闲置redis */
        $redis = Pool::waitForIdle('redis', function (Pool $pool): \Redis {
            /** @var  Connection $connection */
            $connection = $pool->getElement();
            /** 获取redis连接  */
            return $connection->client();
        });
        /** 执行Redis命令  */
        $res = $redis->lPush('webman:coroutine:key', '开源技术小栈' . date('Y-m-d H:i:s'));
        $time = (string)(microtime(true) - $start);
        return json(['time' => $time, 'res' => $res]);
    }
}

4. 启动webman

通过以下命令启动webman

php -d extension=swow webman start

启动成功输出

# php -d extension=swow webman start
Workerman[webman] start in DEBUG mode
---------------------------------------------------- WORKERMAN -----------------------------------------------------
Workerman version:4.1.15          PHP version:8.3.9           Event-Loop:Workbunny\WebmanCoroutine\Events\SwowEvent
----------------------------------------------------- WORKERS ------------------------------------------------------
proto   user            worker                       listen                      processes    status
tcp     root            webman                       http://0.0.0.0:8217         8             [OK] 
tcp     root            monitor                      none                        1             [OK] 
tcp     root            plugin.saiadmin.task         none                        1             [OK] 
tcp     root            plugin.saiadmin.websocket    websocket://0.0.0.0:9527    1             [OK] 
--------------------------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.

5. 请求访问

访问地址 http://127.0.0.1:8217/coroutine/pool

{
  "time": "0.069719791412354",
  "res": 1
}


Redis数据存储结果

图片图片

责任编辑:武晓燕 来源: 开源技术小栈
相关推荐

2024-11-06 11:25:06

2018-10-10 14:27:34

数据库连接池MySQL

2023-11-01 11:27:10

Linux协程

2024-11-01 11:46:59

2024-01-10 08:17:50

HikariCP数据库Spring

2019-12-30 15:30:13

连接池请求PHP

2021-03-24 09:06:01

MySQL长连接短连接

2017-05-02 11:38:00

PHP协程实现过程

2016-10-28 17:39:47

phpgolangcoroutine

2018-12-04 14:00:41

协程编程模式PHP

2009-11-12 09:25:21

ADO.NET连接池

2024-09-05 09:41:57

2011-06-01 13:54:10

MySQL

2017-08-10 15:50:44

PHP协程阻塞

2012-08-08 10:10:31

PHP

2023-12-27 08:07:49

Golang协程池Ants

2018-02-07 16:23:58

连接池内存池AI

2009-09-22 14:52:55

Hibernate p

2009-09-22 16:04:50

Hibernate连接

2022-11-11 09:41:04

连接池微服务数据库
点赞
收藏

51CTO技术栈公众号