Boost Your Webman Apps with Coroutine Support Using webman-coroutine

This guide explains how to install the webman-coroutine plugin, set up the Swow extension, configure Webman for coroutine event loops, and use utility classes such as Pool, Channel, and WaitGroup to build a dynamic Redis connection pool with example code and test results.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Boost Your Webman Apps with Coroutine Support Using webman-coroutine

Introduction

webman-coroutine is a coroutine infrastructure plugin for the Webman framework ecosystem.

Origin

Workerman 4.x and the Webman framework built on it do not support coroutines.

Workerman 5.x also lacks complete coroutine capabilities.

Workerman/Webman have no unified coroutine usage pattern, making driver switching costly.

Implementing custom coroutine worker / server is expensive and error‑prone.

Purpose

Provide a set of coroutine event libraries for Workerman/Webman that work with both Workerman 4.x and 5.x, supporting drivers such as revolt/PHP‑fiber, Swow, Swoole, Ripple.

Offer a unified coroutine development tool compatible with non‑coroutine environments, including utilities: Utils/Channel, Utils/WaitGroup, Utils/Coroutine, Utils/Worker, Utils/Pool.

Vision

Supply a simple coroutine toolkit for Workerman/Webman to reduce cognitive load.

Enable a solution that works in both coroutine and non‑coroutine modes, easing migration.

Attempt non‑intrusive coroutine adaptation of official components.

Help PHP developers gain practical insights.

Installation

Install via Composer: composer require workbunny/webman-coroutine Typical Composer output (truncated) shows the package being added and dependencies resolved.

Install Swow Extension

Install Swow with Composer and then build the extension:

composer require swow/swow
vendor/bin/swow-builder --rebuild --install
# then run make install in the extension directory

Webman Configuration and Usage

1. Configuration

Add the event loop entry to config/server.php:

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

2. Placeholder Initialization

Prevent repeated Pool::create() calls by adding a placeholder in config/bootstrap.php:

return [
    // other config
    \app\common\CoroutinePool::class,
];

Define CoroutinePool.php as a bootstrap that initializes a Redis placeholder:

<?php
declare(strict_types=1);
namespace app\common;
use Webman\Bootstrap;
use Workbunny\WebmanCoroutine\Utils\Pool\Pool;
use Workerman\Worker;

class CoroutinePool implements Bootstrap
{
    public static function start(?Worker $worker)
    {
        // Initialize Redis placeholder
        Pool::init('redis', false);
    }
}

3. Dynamic Pooling Example

Controller CoroutineController demonstrates a Redis connection pool, lazy creation, and a simple command:

<?php
declare(strict_types=1);
namespace app\controller;
use Illuminate\Redis\Connections\Connection;
use support\Response;
use Workbunny\WebmanCoroutine\Utils\Pool\Pool;

class CoroutineController
{
    public function pool(): Response
    {
        $pools = Pool::get('redis', null);
        if (count($pools) < config('redis.pool_size')) {
            $config = config('redis');
            $redis = new \Illuminate\Redis\RedisManager('', 'phpredis', $config);
            Pool::append('redis', (int)array_key_last($pools) + 1, $redis, false);
        }

        $start = microtime(true);
        $redis = Pool::waitForIdle('redis', function (Pool $pool): \Redis {
            $connection = $pool->getElement();
            return $connection->client();
        });
        $res = $redis->lPush('webman:coroutine:key', 'OpenSourceTechStack' . date('Y-m-d H:i:s'));
        $time = (string)(microtime(true) - $start);
        return json(['time' => $time, 'res' => $res]);
    }
}

4. Start Webman

Run the server with the Swow extension enabled:

php -d extension=swow webman start

5. Test Request

Access http://127.0.0.1:8217/coroutine/pool and receive a JSON response showing execution time and Redis command result, e.g.:

{
  "time": "0.0697",
  "res": 1
}
Redis data storage result
Redis data storage result
Redis data storage result
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

pluginPHPInstallationcoroutineexampleSwowWebman
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.