phphleb/webrotor 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phphleb/webrotor

Composer 安装命令:

composer create-project phphleb/webrotor

包简介

Asynchronous PHP web server for shared hosting

README 文档

README

License: MIT version Total Downloads PHP PHP build

WebRotor 

WebRotor Logo

A web server designed to enable asynchronous request processing for shared hosting.

Supports PHP versions 7.2 and higher.

Overview

Shared hosting environments often come with significant limitations compared to dedicated servers.
However, those limitations shouldn't stop you from experimenting with multi-threaded and asynchronous servers!

WebRotor is a specialized web server designed for asynchronous request handling on shared hosting. It is easy to install and configure.
Important: Before using, make sure to review the requirements for your code to execute asynchronously. Most frameworks include guidelines and recommendations for enabling asynchronous functionality.

With this library, applications built on frameworks like Laravel, Symfony, Yii3 and others that support asynchronous processing, along with any other asynchronously written code, can operate in this mode on various shared hosting platforms.

It makes sense to move a project to asynchrony if it has performance problems or you want to experiment with asynchrony.

Installation

Use Composer:

composer require phphleb/webrotor --no-dev

Next, you need to install one of the PHP HTTP client implementations for PSR-7.

Nyholm:

composer require nyholm/psr7
composer require nyholm/psr7-server

or Guzzle:

 composer require guzzlehttp/guzzle

Shared Hosting

Modifying the Index File

Typically, shared hosting environments have a public folder containing an index file, usually named index.php.
It is necessary to modify this file so that your application's code is enclosed within an asynchronous loop. You can find examples of how to connect to an asynchronous server in the documentation for the framework you are using.
In a simplified manner, this looks like:

<?php
// Contents of your index.php file.
// Basic example for displaying the greeting line.

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Phphleb\Webrotor\WebRotor;
use Phphleb\Webrotor\Src\Handler\NyholmPsr7Creator;

require __DIR__ . '/../vendor/autoload.php';

$psr7Creator = new NyholmPsr7Creator(); // or GuzzlePsr7Creator()
$server = new WebRotor();
$server->init($psr7Creator);

$server->run(function(ServerRequestInterface $request, ResponseInterface $response) {
    // Here's your application code. //
    $response->getBody()->write('Hello World!');

    return $response;
});

Launching Workers

Now for the fun part! :) Shared hosting is unlikely to allow you to daemonize processes, but we will use the built-in cron (or its equivalents). First, find the section in your hosting admin panel named something like "Task Scheduler" or "crontab." A simple example would be launching two workers, each with a lifespan of two minutes. We'll use the same index file (index.php) as our worker, specifying the worker number.

# Runs the first command every two minutes
*/2 * * * * /usr/local/bin/php7.2 /my-project/public_html/index.php --id=1

# Runs the second command every two minutes
*/2 * * * * /usr/local/bin/php7.2 /my-project/public_html/index.php --id=2

The following example shows two workers that launch every two minutes with a one-minute offset. The implementation can vary depending on your hosting settings, but if you encounter any issues, try using just the first example, or contact your hosting support and show them this example.

# Runs the first command every two minutes
*/2 * * * * /usr/local/bin/php7.2 /my-project/public_html/index.php --id=1

# Runs the second command with a one minute delay after the first
1-59/2 * * * * /usr/local/bin/php7.2 /my-project/public_html/index.php --id=2

Workers can number more than two, and the restart time may vary based on specific load and available server resources.

Configuration

In the previous example, we indicated that two workers are running with a two-minute interval between them. Now, it’s essential to modify this in the web server settings, as it defaults to one worker restarting every minute. Here's how to do that:

// ... //
use Phphleb\Webrotor\Config;

$config = new Config();
$config->logLevel = 'warning'; // Logging max level according to PSR-3.
$config->workerNum = 2; // Number of workers.
$config->workerLifetimeSec = 120; // Worker lifetime is two minutes.

$server = new WebRotor($config);
 // ... //

By default, web server logs are stored above the public directory in the wr-logs folder.

Working with Sessions, Cookies, and Files in Asynchronous Mode

Asynchronous mode has its own specific characteristics since the request is handled by a worker within a single thread inside the standard loop.
As a result, additional attributes are passed through a created object that implements the ServerRequestInterface.

// ... //
use Psr\Http\Message\ServerRequestInterface;

$server->run(function(ServerRequestInterface $request, ResponseInterface $response) {
    // Example of assigning a session.
    $request->getAttribute('session')->set('session_name', 'value');
    // or
    $_SESSION['session_name'] = 'value';
    // An example of getting a value from a session.
    $sessionParam = $request->getAttribute('session')->get('session_name');
    // or
    $sessionParam = $_SESSION['session_name'];
    
    // An example of assigning and getting a Cookie value.
    $request->getAttribute('cookie')->set('cookie_name', 'value', []);
    $cookieParam = $request->getAttribute('cookie')->get('cookie_name');
    // or
    $cookieParam = $_COOKIE['cookie_name'];
    
    $files = $request->getUploadedFiles(); // An array of special objects.
    // or
    $files = $request->getAttribute('files') // Standard array $_FILES
    // or
    $files = $_FILES;
    
    return $response;
});

Example for Laravel 11.x

An example of basic initialization for Laravel 11.x can be found in this repository in the examples/frameworks folder.

Virtual Server or Dedicated Server

With broader access to the server environment, there's the potential to replace the current worker data storage with a Redis-based storage to maximize performance. An example of such a setup can be found in the examples folder.

Simulating Serverless for Asynchronous Processing

If you need to distribute the load and want your asynchronous worker to remain active only when there are incoming requests, use the idleTimeoutSec configuration parameter to shut down the worker early when it becomes idle. If all workers are shut down ahead of time, the application will run without asynchronous processing until the workers are started again according to their schedule.

For example, if the idleTimeoutSec is set to 10 seconds, after handling a request, the worker will wait for 10 seconds for new requests; if none arrive during this time, it will shut down.

Local Development

For local development, there's no need to modify the previous setup. Simply run the following command from the project's public directory:

php index.php

This will initiate a single worker for the duration specified in the configuration.
If the worker is not running or has been disabled, your project will still function, but requests will be processed in the standard, non-asynchronous mode. As a result, using workers is optional for local development.


Telegram


phphleb/webrotor 适用场景与选型建议

phphleb/webrotor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.52k 次下载、GitHub Stars 达 64, 最近一次更新时间为 2025 年 01 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「symfony」 「php」 「laravel」 「slim」 「web-server」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phphleb/webrotor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phphleb/webrotor 我们能提供哪些服务?
定制开发 / 二次开发

基于 phphleb/webrotor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.52k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 64
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 64
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-07