spiral/roadrunner-tcp

RoadRunner: TCP工作进程

v4.1.0 2024-04-11 18:06 UTC

This package is auto-updated.

Last update: 2024-09-12 09:54:28 UTC


README

RoadRunner TCP插件

PHP Version Require Latest Stable Version phpunit psalm Codecov Total Downloads type-coverage psalm-level

RoadRunner是一个开源的(MIT授权)高性能PHP应用程序服务器、负载均衡器和进程管理器。它支持作为服务运行,并且可以根据项目需求扩展其功能。

RoadRunner包括TCP服务器,并且可以用作用更高的性能和灵活性替换经典的TCP配置。

官方网站 | 文档

此存储库包含TCP PHP工作进程的代码库。要获取应用程序服务器,请查看spiral/roadrunner

安装

要安装应用程序服务器和TCP代码库

composer require spiral/roadrunner-tcp

您可以使用方便的安装程序下载RoadRunner的最新兼容版本

composer require spiral/roadrunner-cli --dev

要下载应用程序服务器的最新版本

vendor/bin/rr get

用法

例如,这样的配置可以很好地运行

tcp:
  servers:
    smtp:
      addr: tcp://127.0.0.1:1025
      delimiter: "\r\n" # by default
    server2:
      addr: tcp://127.0.0.1:8889

  pool:
    num_workers: 2
    max_jobs: 0
    allocate_timeout: 60s
    destroy_timeout: 60s

如果您的池中有超过1个工作进程,TCP服务器会将接收到的数据包发送到不同的工作进程,如果您需要收集数据,您必须使用所有工作进程都可以访问的存储,例如 RoadRunner Key Value

示例

初始化抽象的RoadRunner工作进程

<?php

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

use Spiral\RoadRunner\Worker;
use Spiral\RoadRunner\Tcp\TcpWorker;
use Spiral\RoadRunner\Tcp\TcpResponse;
use Spiral\RoadRunner\Tcp\TcpEvent;

// Create new RoadRunner worker from global environment
$worker = Worker::create();

$tcpWorker = new TcpWorker($worker);

while ($request = $tcpWorker->waitRequest()) {

    try {
        if ($request->getEvent() === TcpEvent::Connected) {
            // You can close connection according your restrictions
            if ($request->getRemoteAddress() !== '127.0.0.1') {
                $tcpWorker->close();
                continue;
            }
            
            // -----------------
            
            // Or continue read data from server
            // By default, server closes connection if a worker doesn't send CONTINUE response 
            $tcpWorker->read();
            
            // -----------------
            
            // Or send response to the TCP connection, for example, to the SMTP client
            $tcpWorker->respond("220 mailamie \r\n");
            
        } elseif ($request->getEvent() === TcpEvent::Data) {
                   
            $body = $request->getBody();
            
            // ... handle request from TCP server [tcp_access_point_1]
            if ($request->getServer() === 'tcp_access_point_1') {

                // Send response and close connection
                $tcpWorker->respond('Access denied', TcpResponse::RespondClose);
               
            // ... handle request from TCP server [server2] 
            } elseif ($request->getServer() === 'server2') {
                
                // Send response to the TCP connection and wait for the next request
                $tcpWorker->respond(\json_encode([
                    'remote_addr' => $request->getRemoteAddress(),
                    'server' => $request->getServer(),
                    'uuid' => $request->getConnectionUuid(),
                    'body' => $request->getBody(),
                    'event' => $request->getEvent()
                ]));
            }
           
        // Handle closed connection event 
        } elseif ($request->getEvent() === TcpEvent::Close) {
            // Do something ...
            
            // You don't need to send response on closed connection
        }
        
    } catch (\Throwable $e) {
        $tcpWorker->respond("Something went wrong\r\n", TcpResponse::RespondClose);
        $worker->error((string)$e);
    }
}
try Spiral Framework

测试

此代码库通过主存储库自动测试 - spiral/roadrunner

许可

MIT许可证(MIT)。请参阅LICENSE获取更多信息。由Spiral Scout维护。