From 14fd2f18f3462a4a9b6e41d74214d00afff59c8f Mon Sep 17 00:00:00 2001 From: qieangel2013 <904208360@qq.com> Date: Thu, 11 Aug 2016 15:42:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=B1=A0=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改数据库连接池服务 --- application/library/swoole/socket.php | 10 +-- conf/application.ini | 2 +- server/mysql/DbServer.php | 101 +++++++++++++++----------- 3 files changed, 62 insertions(+), 51 deletions(-) diff --git a/application/library/swoole/socket.php b/application/library/swoole/socket.php index d92ef1b..bda0a03 100644 --- a/application/library/swoole/socket.php +++ b/application/library/swoole/socket.php @@ -4,30 +4,22 @@ class swoole_socket extends Model { public $user; public function __construct() { parent::__construct($this->table); - /*if (!isset($this->user)){ - $this->user = new swoole_socket(); - } */ } public function getname($userid){ - //$where=array('id' =>37936); $where=array('id' =>$userid); $result =$this->user->where($where)->select(); } public static function savefd($fd,$kname='fd'){ - //$where=array('id' =>37936); - $redis_con=new phpredis(); + $redis_con=new phpredis(); if($redis_con->listSize($kname)){ $redis_con->listPush($kname,$fd,0,1); }else{ $redis_con->listPush($kname,$fd); } - //file_put_contents( __DIR__ .'/log.txt' , $fd); } public static function getfd($kname='fd'){ $redis_con=new phpredis(); $result=$redis_con->listGet($kname,0,-1); - //$m = file_get_contents( __DIR__ .'/log.txt'); - //echo $m; echo json_encode($result); } public static function removefd($fd,$kname='fd'){ diff --git a/conf/application.ini b/conf/application.ini index 494d78d..6dfd304 100644 --- a/conf/application.ini +++ b/conf/application.ini @@ -46,7 +46,7 @@ ftp.config.host="192.168.1.26" ftp.config.username="root" ftp.config.password="root" ;redis配置 -redis.config.server="127.0.0.1" +redis.config.server="192.168.162.163" redis.config.port="6379" ;数据库连接池配置 DbServer.async=true diff --git a/server/mysql/DbServer.php b/server/mysql/DbServer.php index 734143d..78611df 100644 --- a/server/mysql/DbServer.php +++ b/server/mysql/DbServer.php @@ -96,15 +96,13 @@ public function __construct() { public function onStart($serv) { for ($i = 0; $i < $this->pool_size; $i++) { - $db = new mysqli; - $db->connect($this->config['host'],$this->config['user'],$this->config['pwd'],$this->config['name']); + $db = new swoole_mysql; + $db->connect(array('host' => $this->config['host'],'user' => $this->config['user'],'password' => $this->config['pwd'],'database' => $this->config['name']),array(&$this, 'onSQLReady')); //设置数据库编码 - $db->query("SET NAMES '".$this->config['charset']."'"); - $db_sock = swoole_get_mysqli_sock($db); - swoole_event_add($db_sock, array(&$this, 'onSQLReady')); - $this->idle_pool[] = array( - 'mysqli' => $db, - 'db_sock' => $db_sock, + $db->query("SET NAMES '".$this->config['charset']."'",array(&$this, 'doQuery')); + $this->idle_pool[$i] = array( + 'db' => $db, + 'sock'=>$i, 'fd' => 0, ); } @@ -117,9 +115,21 @@ public function onpipeMessage($serv, $src_worker_id, $data) //$this->idle_pool=json_decode($data,true); } - public function onSQLReady($db_sock) + public function onSQLReady($db,$result) { - $db_res = $this->busy_pool[$db_sock]; + if($result){ + foreach ($this->idle_pool as $k => $v) { + if($v['db']==$db){ + array_unshift($this->wait_queue,$db); + $this->busy_pool[$k]=array( + 'db' => $db, + 'sock'=>$k, + 'fd' => 0, + ); + } + } + } + /*$db_res = $this->busy_pool[$db_sock]; $mysqli = $db_res['mysqli']; $fd = $db_res['fd']; $data_select=array('status' =>'ok','error'=>0,'errormsg'=>'','result'=>''); @@ -151,25 +161,25 @@ public function onSQLReady($db_sock) $req = array_shift($this->wait_queue); $this->doQuery($req['fd'], $req['sql']); } - } + }*/ } public function onReceive($serv, $fd, $from_id, $data) { if($this->isasync){ - if (count($this->idle_pool) == 0) { + //if (count($this->idle_pool) == 0) { //等待队列未满 - if (count($this->wait_queue) < $this->wait_queue_max) { - $this->wait_queue[] = array( - 'fd' => $fd, - 'sql' => $data, - ); - } else { - $this->http->send($fd, "request too many, Please try again later."); - } - } else { - $this->doQuery($fd, $data); - } + //if (count($this->wait_queue) < $this->wait_queue_max) { + // $this->wait_queue[] = array( + // 'fd' => $fd, + // 'sql' => $data, + // ); + // } else { + // $this->http->send($fd, "request too many, Please try again later."); + //} + // } else { + $this->dosql($fd,$data); + //} }else{ if($this->multiprocess){ $result = $this->http->task($data); @@ -193,6 +203,26 @@ public function onReceive($serv, $fd, $from_id, $data) } } + //连接池策略 + public function dosql($fd,$data){ + if(count($this->wait_queue) > $this->wait_queue_max) { + $this->http->send($fd, "request too many, Please try again later."); + }else{ + if (count($this->wait_queue) > 0) { + $db=array_shift($this->wait_queue); + $httpser=$this->http; + $db->query($data,function($link,$result) use($httpser,$fd){ + if($result){ + $httpser->send($fd,json_encode($result)); + array_unshift($this->wait_queue,$db); + } + }); + + } + } + + } + public function onTask($serv, $task_id, $from_id, $sql) { if (!self::$link) { @@ -225,28 +255,17 @@ public function onTask($serv, $task_id, $from_id, $sql) return $data; } - public function doQuery($fd, $sql) + public function doQuery($link,$result) { + if($result){ //从空闲池中移除 - $db = array_pop($this->idle_pool); - /** - * @var mysqli - */ - $mysqli = $db['mysqli']; - for ($i = 0; $i < 2; $i++) { - $result = $mysqli->query($sql, MYSQLI_ASYNC); - if ($result === false) { - if ($mysqli->errno == 2013 or $mysqli->errno == 2006) { - $mysqli->close(); - $r = $mysqli->connect(); - if ($r === true) continue; - } + foreach ($this->idle_pool as $k => $v) { + if($link==$v['db']){ + unset($this->busy_pool[$k]); } - break; } - $db['fd'] = $fd; - //加入工作池中 - $this->busy_pool[$db['db_sock']] = $db; + return $result; + } } public function onFinish($serv, $data) {