huang-yi/shadowfax 问题修复 & 功能扩展

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

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

huang-yi/shadowfax

Composer 安装命令:

composer require huang-yi/shadowfax

包简介

Run Laravel on Swoole.

README 文档

README

English | 中文

Shadowfax

The Shadowfax is a package that runs your Laravel application on Swoole.

Installation

You may use Composer to install Shadowfax to your project:

composer require huang-yi/shadowfax

If you are using Lumen, you need to register the service provider manually in bootstrap/app.php:

$app->register(HuangYi\Shadowfax\ShadowfaxServiceProvider::class);

After installing Shadowfax, publish its configuration files using the shadowfax:publish Artisan command:

php artisan shadowfax:publish

Configuration

The primary configuration file is shadowfax.yml. And this file name is added to the .gitignore file by shadowfax:publish Artisan command.

  1. Basic configuration:
  • name: The processes name.
  • type: The server type, support: http, websocket.
  • host: The server host.
  • port: The server port.
  • mode: The server mode, support: process, base.
  • access_log: Indicates whether to print the request information.
  • app_pool_capacity: Set the capacity of the apps pool. Only valid when coroutine is enabled.
  • framework_bootstrapper: Set Laravel bootstrap file. If you changed Laravel's directory structure, you should modify this value.
  1. server configuration:

This section defines the Swoole\Server configuration. Read the official docs for more information.

  1. abstracts configuration:

This option allows you to set a group of abstracts in the Laravel IoC container. These abstracts will be rebound after each request.

  1. controllers configuration:

This option allows you to clean the controller instances in route after each request.

controllers:
  - App\Http\Controllers\FooController
  - App\Http\Controllers\BarController
  1. cleaners configuration:

This option allows you to register custom cleaners. These cleaners will run after each request.

cleaners:
  - app/Cleaners/
  - CustomNamespace/FooCleaner
  1. db_pools configuration:

This option allows you to configure database connection pools. You can add multiple key-value pairs in here. The key name is a connection name in your database.connections, the key value is the connection pool capacity. e.g.:

db_pools:
  mysql: 3
  mysql2: 5
  1. redis_pools configuration:

This option allows you to configure redis connection pools. You can add multiple key-value pairs in here. The key name is a connection name in your database.redis, the key value is the connection pool capacity. e.g.:

redis_pools:
  default: 3
  1. controller server configuration:

This section defines the controller server configuration. The controller server allows you to stop or reload your Shadowfax.

Command

Shadowfax provides a shadowfax command to manage your server processes. This command is build on the Symfony console component, so you can run php shadowfax list for more information.

You may run the php shadowfax start command to start Shadowfax server. The --watch|-w option can run your Shadowfax in watch mode. In watch mode, the processes will be automatically reloaded when the files under your project change.

You must install the fswatch before using --watch|-w option.

The php shadowfax reload allows you to reload the Shadowfax processes.

The php shadowfax stop allows you to stop the Shadowfax server.

Database Connection Pool

Before using database connection pools, you must enable Swoole coroutine and configure the hook_flags:

server:
  enable_coroutine: true
  hook_flags: SWOOLE_HOOK_ALL

Then, add your connection to the db_pools option, and specify a pool capacity:

db_pools:
  mysql: 3
  mysql2: 5

Redis Connection Pool

The difference with database connection pool is that redis connection pools are configured under the redis_pools option.

redis_pools:
  default: 3

WebSocket Server

Shadowfax also allows you to build your WebSocket server.

First of all, you need to change the value of configuration item type to websocket. Then create a handler class that implemented the HuangYi\Shadowfax\Contracts\WebSocket\Handler interface:

namespace App\WebSocket;

use Illuminate\Http\Request;
use HuangYi\Shadowfax\Contracts\WebSocket\Connection;
use HuangYi\Shadowfax\Contracts\WebSocket\Handler;
use HuangYi\Shadowfax\Contracts\WebSocket\Message;

class EchoServer implements Handler
{
    /**
     * Handler for open event.
     *
     * @param  \HuangYi\Shadowfax\Contracts\WebSocket\Connection  $connection
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    public function onOpen(Connection $connection, Request $request)
    {
        $connection->send('connected');
    }

    /**
     * Handler for message event.
     *
     * @param  \HuangYi\Shadowfax\Contracts\WebSocket\Connection  $connection
     * @param  \HuangYi\Shadowfax\Contracts\WebSocket\Message  $message
     * @return mixed
     */
    public function onMessage(Connection $connection, Message $message)
    {
        $connection->send($message->getData());
    }

    /**
     * Handler for close event.
     *
     * @param  \HuangYi\Shadowfax\Contracts\WebSocket\Connection  $connection
     * @return mixed
     */
    public function onClose(Connection $connection)
    {
        $connection->send('closed');
    }
}

And bind this handler to a uri in your route file:

use App\WebSocket\EchoServer;
use HuangYi\Shadowfax\Facades\WebSocket;

WebSocket::listen('/echo', new EchoServer);

Now, you can start the WebSocket server by command php shadowfax start.

Nginx Configuration

You may use Nginx as a reverse proxy in production environment:

# Uncomment this if you are running a websocket server.
# map $http_upgrade $connection_upgrade {
#     default upgrade;
#     '' close;
# }

server {
    listen 80;
    server_name example.com;
    root /example.com/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location = /index.php {
        try_files /nonexistent_file @shadowfax;
    }

    location / {
        try_files $uri $uri/ @shadowfax;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 @shadowfax;

    location @shadowfax {
        set $suffix "";

        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }

        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # Uncomment this if you are running a websocket server.
        # proxy_set_header Upgrade $http_upgrade;
        # proxy_set_header Connection $connection_upgrade;

        proxy_pass http://127.0.0.1:1215$suffix;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

You need to add the IP address of the Shadowfax to the App\Http\Middleware\TrustProxies Middleware.

Supervisor Configuration

If you want to use the Supervisor to manage your Shadowfax processes, the following configuration file should suffice:

[program:shadowfax]
process_name=%(program_name)s
directory=/path/to/project
command=php shadowfax start
autostart=true
autorestart=true
user=www
redirect_stderr=true
stdout_logfile=/path/to/project/storage/logs/supervisor.log

Benchmarks

Run tests using wrk.

Environment 1

  • Hardware: 1 CPU, 4 Cores, 16GB Memory
  • MacOS 10.15.3
  • PHP 7.3.12 (with opcache)
  • Swoole 4.4.13
  • Laravel 7 (without session middleware)
  • Shadowfax 2.0.0 (with 20 worker processes)
wrk -t4 -c200 http://127.0.0.1:1215/

Result:

Running 10s test @ http://127.0.0.1:1215/
  4 threads and 200 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    26.44ms   31.44ms 212.73ms   84.28%
    Req/Sec     3.13k   839.99     6.07k    65.75%
  124418 requests in 10.01s, 312.06MB read
  Socket errors: connect 0, read 54, write 0, timeout 0
Requests/sec:  12430.20
Transfer/sec:     31.18MB

Environment 2

  • Hardware: 2 CPUs, 2 Cores, 4GB Memory
  • CentOS 7.5.1804
  • PHP 7.3.16 (with opcache)
  • Swoole 4.4.17
  • Laravel 7 (without session middleware)
  • Shadowfax 2.0.0 (with 10 worker processes)
wrk -c100 http://127.0.0.1:1215/

Result:

Running 10s test @ http://127.0.0.1:1215/
  2 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    25.06ms   12.11ms  85.92ms   60.94%
    Req/Sec     4.02k    41.46     4.08k    79.79%
  40321 requests in 10.08s, 101.13MB read
Requests/sec:   4001.76
Transfer/sec:     10.04MB

Testing

composer test

License

Shadowfax is open-sourced software licensed under the MIT license.

huang-yi/shadowfax 适用场景与选型建议

huang-yi/shadowfax 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.65k 次下载、GitHub Stars 达 351, 最近一次更新时间为 2020 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 huang-yi/shadowfax 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.65k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 352
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 351
  • Watchers: 7
  • Forks: 35
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-19