UT框架基于Swoole的Mysql连接池

2022-03-21 22:37:03
黄豆 CSDN
开发实例与经验 3161
loading

直接上代码,我是将连接池做成了一个主类放在library目录下,方便所有模块调用:

libraryPool.php

namespace libraryPool;
use libraryUsualToolMysql;
/**
* 基于Swoole的数据库连接池
* 目前支持Mysql
* $mode 默认0,0CLI模式 1客户端模式
* $worker_num 默认5,进程数
* $task_num 默认10,维持连接数
* $dispatch_mode 默认2,1轮循 2固定 3抢占 4IP分配 5UID分配 7stream
* $daemonize 默认0,守护进程 1开启 0关闭
*/
class UTPool{
    protected $log_file;
    protected $max_request;
    public function __construct($host='127.0.0.1',$port='9510',$mode='0',$worker_num='5',$task_num='10',$dispatch_mode='2',$daemonize='0'){
        $this->host = $host;
        $this->port = $port;
        $this->mode = $mode;
        $this->worker_num = $worker_num;
        $this->task_worker_num = $task_num;   
        $this->dispatch_mode = $dispatch_mode;
        $this->daemonize = $daemonize;
        $this->max_request = 10000;
        if($this->mode==0):
            $this->pool = new SwooleServer($this->host, $this->port);
            $this->pool->set(array(
                'worker_num'=>$this->worker_num,
                'task_worker_num' => $this->task_worker_num,
                'max_request' => $this->max_request,
                'daemonize' => $this->daemonize,
                //'log_file' => "/www/wwwroot/test/log.txt",//日志地址
                'dispatch_mode' => $this->dispatch_mode
            ));
        endif;
    }
    public function Run(){
        $this->pool->on('Receive', array($this, 'OnReceive'));
        $this->pool->on('Task', array($this, 'OnTask'));
        $this->pool->on('Finish', array($this, 'OnFinish'));        
        $this->pool->start();
    }
    public function OnReceive($serv, $fd, $from_id, $data){
        $result = $this->pool->taskwait($data);
        if ($result !== false) {
            $result=json_decode($result,true);
                  if ($result['status'] == 'OK') {
                $this->pool->send($fd, json_encode($result['data']) . "
");
            } else {
                $this->pool->send($fd, $result);
            }
            return;
        } else {
            $this->pool->send($fd, "Error. Task timeout
");
        }
    }
    public function OnTask($serv, $task_id, $from_id, $sql){
        static $link = null;
        UTKILL:
            if ($link == null) {
                $link = UsualToolMysqlUTMysql::GetMysql();
                if (!$link) {
                    $link = null;
                    $this->pool->finish(mysqli_error($link));
                    return;
                }   
            }   
        $result = $link->query($sql);
        if (!$result) {
            if(in_array(mysqli_errno($link),[2013,2006])){
                    $link = null;
                    goto UTKILL;
            }else{
                $this->pool->finish(mysqli_error($link));
                return;
            }
        }
        if(preg_match("/^select/i", $sql)){
             $data = array();
                while ($fetchResult = mysqli_fetch_assoc($result) ){
                     $data['data'][]=$fetchResult;
                }               
        }else{
            $data['data'] = $result;
        }
        $data['status']="OK";
        $this->pool->finish(json_encode($data));
    }
    public function OnFinish($serv, $task_id, $data){
        echo "done";
    }
    public function Query($sql){
        $timeout=20;//请求超时时间
        $client=new SwooleClient(SWOOLE_SOCK_TCP);
        $client->connect($this->host,$this->port,$timeout) or die("connection failed");
        $client->send($sql);
        $data = $client->recv();
        $client->close();
        return json_decode($data,true);
    }
}

服务端 server.php

//服务器启动端 php server.php
require_once dirname(__FILE__).'/'.'autoload.php';
//默认配置简单写法
$pool=new libraryPoolUTPool("127.0.0.1",9639,0);
/**
* 配置详细写法
* 第1个参数:主机
* 第2个参数:端口
* 第3个参数:默认0,0CLI模式 1客户端模式
* 第4个参数:默认5,进程数
* 第5个参数:默认10,维持连接数
* 第6个参数:默认2,1轮循 2固定 3抢占 4IP分配 5UID分配 7stream
* 第7个参数:默认0,守护进程 1开启 0关闭
*/
//$pool=new libraryUsualToolPoolUTPool("127.0.0.1",9637,0,5,10,2,0);
$pool->Run();

客户端 index.php

//客户端
require_once dirname(__FILE__).'/'.'autoload.php';
$pool=new libraryPoolUTPool("127.0.0.1",9639,1);
$data=$pool->Query("select * from log limit 1");
print_r($data);
+分享给朋友+
郑重提醒:部分素材来源于互联网,如果侵犯了您的权利,请及时联络我们更正,谢谢合作,电邮:usualtool@qq.com