• swoole 连接池



    proxy_pool.php

    
    <?php
    
    class ProxyServer
    {
        protected $frontends;
        protected $backends;
        /**
         * @var swoole_server
         */
        protected $serv;
        protected $index = 0;
        protected $mode = SWOOLE_BASE;
        protected $backendServer;
    
        function run($backendServer = array('host' => '127.0.0.1', 'port' => '80'), $port = 9501, $host = '0.0.0.0')
        {
            $this->backendServer = $backendServer;
            $serv = new swoole_server($host, $port, $this->mode);
            $serv->set(array(
                'worker_num' => 8, //worker process num
                //'backlog' => 128, //listen backlog
                //'open_tcp_keepalive' => 1,
                //'log_file' => '/tmp/swoole.log', //swoole error log
            ));
            $serv->on('WorkerStart', array($this, 'onStart'));
            $serv->on('Receive', array($this, 'onReceive'));
            $serv->on('Close', array($this, 'onClose'));
            $serv->on('WorkerStop', array($this, 'onShutdown'));
            $serv->start();
        }
    
        function onStart($serv)
        {
            $this->serv = $serv;
            //echo "Server: start.Swoole version is [" . SWOOLE_VERSION . "]
    ";
        }
    
        function onShutdown($serv)
        {
            echo "Server: onShutdown
    ";
        }
    
        function onClose($serv, $fd, $from_id)
        {
            //清理掉后端连接
            if (isset($this->frontends[$fd])) {
                $backend_socket = $this->frontends[$fd];
                $backend_socket->closing = true;
                $backend_socket->close();
                unset($this->backends[$backend_socket->sock]);
                unset($this->frontends[$fd]);
            }
            echo "onClose: frontend[$fd]
    ";
        }
    
        function onReceive($serv, $fd, $from_id, $data)
        {
            //尚未建立连接
            if (!isset($this->frontends[$fd])) {
                //连接到后台服务器
                $socket = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
                $socket->closing = false;
                $socket->on('connect', function (swoole_client $socket) use ($data) {
                    $socket->send($data);
                });
                $socket->on('error', function (swoole_client $socket) use ($fd) {
                    echo "ERROR: connect to backend server failed
    ";
                    $this->serv->send($fd, "backend server not connected. please try reconnect.");
                    $this->serv->close($fd);
                });
                $socket->on('close', function (swoole_client $socket) use ($fd) {
                    echo "onClose: backend[{$socket->sock}]
    ";
                    unset($this->backends[$socket->sock]);
                    unset($this->frontends[$fd]);
                    if (!$socket->closing) {
                        $this->serv->close($fd);
                    }
                });
                $socket->on('receive', function (swoole_client $socket, $_data) use ($fd) {
                    $this->serv->send($fd, $_data);
                });
                if ($socket->connect($this->backendServer['host'], $this->backendServer['port'])) {
                    $this->backends[$socket->sock] = $fd;
                    $this->frontends[$fd] = $socket;
                } else {
                    echo "ERROR: cannot connect to backend server.
    ";
                    $this->serv->send($fd, "backend server not connected. please try reconnect.");
                    $this->serv->close($fd);
                }
            } //已经有连接,可以直接发送数据
            else {
                /**
                 * @var $socket swoole_client
                 */
                $socket = $this->frontends[$fd];
                $socket->send($data);
            }
        }
    }
    // config
    $backendServerList = [
        [
            'host' => 'api.eelly.com', 'port' => 443, 'proxy_port' => 9501,
        ],
    ];
    
    $workerNum = count($backendServerList);
    $pool = new SwooleProcessPool($workerNum);
    $pool->on("WorkerStart", function ($pool, $workerId) use ($backendServerList) {
        echo "Worker#{$workerId} is started
    ";
        $backendServer = $backendServerList[$workerId];
        $serv = new ProxyServer($backendServer, $backendServer['proxy_port']);
        echo "Remote {$backendServer['host']}:{$backendServer['port']} -> Local {$backendServer['proxy_port']}
    ";
        $serv->run();
    });
    $pool->start();
    
    
  • 相关阅读:
    ASP.NET Web API是如何根据请求选择Action的?[上篇]
    Ruby的对象模型
    MongoDB学习3
    Linux目录树详细说明
    Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)
    [置顶] SQL注入安全分析
    3.9 聚集和联接
    Qt之QTemporaryFile(文件名唯一,且可以自动删除)
    调用Windows属性窗口(居然是通过注册表来调用的)
    QTextEdit中选中文本修改字体与颜色,全部文本修改字体与颜色(设置调色板的前景色、背景色、文字颜色以及基色)
  • 原文地址:https://www.cnblogs.com/lalalagq/p/10205872.html
Copyright © 2020-2023  润新知