ifcastle/amphp-pool

使用纯PHP和AMPHP创建中间层库,以构建有状态的异步服务器端应用程序

v1.0.0 2024-07-13 08:55 UTC

This package is auto-updated.

Last update: 2024-09-22 08:46:35 UTC


README

使用纯PHP和AMPHP库构建中间层库,以创建有状态异步服务器端应用程序AMPHP

  • 不使用额外扩展(例如Swoole
  • 不使用来自其他编程语言的辅助工具(例如Go + Roadrunner

为什么需要这个?

  • 您只想使用纯PHP,不使用额外扩展。
  • 您想控制Worker的运行方式,并希望能够编程它们的运行行为,以确保更好的性能和稳定性平衡。

功能

  • 处理连接和后台任务(工作)的Worker,这些任务可以按需重启和扩展。
  • 支持不同类型的Worker,具有不同的行为。
  • 用于负载分配的重启扩展收集Worker的策略。
  • 根据优先级权重权重是资源消耗的估计)执行工作
  • 用于在长时间运行的后台作业之间分配负载的协程调度器
  • 支持使用Prometheus + Grafana进行遥测和统计。
  • 支持Windows

Architecture

但是PHP不是很快吗?

请查阅性能考虑

安装

composer require ifcastle/amphp-pool

示例

<?php
declare(strict_types=1);

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

use Amp\ByteStream;
use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use IfCastle\AmpPool\WorkerGroup;
use IfCastle\AmpPool\WorkerPool;
use IfCastle\AmpPool\WorkerTypeEnum;
use Examples\HttpServer\HttpReactor;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;

$logHandler = new StreamHandler(ByteStream\getStdout());
$logHandler->pushProcessor(new PsrLogMessageProcessor());
$logHandler->setFormatter(new ConsoleFormatter());

$logger = new Logger('server');
$logger->pushHandler($logHandler);
$logger->useLoggingLoopDetection(false);

// 1. Create a worker pool with a logger
$workerPool = new WorkerPool(logger: $logger);

// 2. Fill the worker pool with workers.
// We create a group of workers with the Reactor type, which are intended to handle incoming connections.
// The HttpReactor class is the entry point for the workers in this group.
// Please see the HttpReactor class for more details.
$workerPool->describeGroup(new WorkerGroup(
    entryPointClass: HttpReactor::class,
    workerType: WorkerTypeEnum::REACTOR,
    minWorkers: 1
));

// 3. Run the worker pool
// Start the main loop of the worker pool
// Now the server is ready to accept incoming connections.
// Try http://127.0.0.1:9095/ in your browser.
$workerPool->run();

请参阅入门指南。

Prometheus + Grafana

Prometheus

请参阅:如何设置Grafana仪表板