Browse Source

Ubiquity swoole update to Coroutine Mysql (#5039)

* Add coroutine Mysql to swoole (no more pdo)

+ connection pooling

* Update SwooleDb.php
jcheron 6 years ago
parent
commit
a5aa5b3256

+ 2 - 0
frameworks/PHP/ubiquity/.ubiquity/_swoole.php

@@ -9,10 +9,12 @@ if (! defined ( 'DS' )) {
 $config=include ROOT.'config/config.php';
 $sConfig= include __DIR__.\DS.'swoole-config.php';
 $config["sessionName"]=null;
+$config["database"]["wrapper"]="\\Ubiquity\\db\\providers\\swoole\\SwooleWrapper";
 $address=$sConfig['host'].':'.$sConfig['port'];
 $config ["siteUrl"] = 'http://'.$address;
 require ROOT . './../vendor/autoload.php';
 require ROOT.'config/services.php';
 $reactServer=new \Ubiquity\servers\swoole\SwooleServer();
 $reactServer->init($config, __DIR__);
+\Ubiquity\orm\DAO::initPooling($config,'default');
 $reactServer->run($sConfig['host'],$sConfig['port']);

+ 35 - 9
frameworks/PHP/ubiquity/app/controllers/SwooleDb.php

@@ -3,6 +3,7 @@ namespace controllers;
 
 use Ubiquity\orm\DAO;
 use models\World;
+use Swoole\Coroutine as co;
 
 /**
  * Bench controller.
@@ -14,28 +15,53 @@ class SwooleDb extends \Ubiquity\controllers\Controller {
 	}
 	
 	public function index() {
+	    $dbInstance=DAO::pool();
 		$world = DAO::getById(World::class, \mt_rand(1, 10000), false);
+		DAO::freePool($dbInstance);
 		echo \json_encode($world->_rest);
 	}
 	
 	public function query($queries = 1) {
 		$worlds = [];
 		$queries = \min(\max($queries, 1), 500);
+		$dbInstance=DAO::pool();
 		for ($i = 0; $i < $queries; ++ $i) {
 			$worlds[] = (DAO::getById(World::class, \mt_rand(1, 10000), false))->_rest;
 		}
+		DAO::freePool($dbInstance);
 		echo \json_encode($worlds);
 	}
 	
 	public function update($queries = 1) {
-		$worlds = [];
-		$queries = \min(\max($queries, 1), 500);
-		for ($i = 0; $i < $queries; ++ $i) {
-			$world = DAO::getById(World::class, \mt_rand(1, 10000), false);
-			$world->randomNumber = \mt_rand(1, 10000);
-			DAO::update($world);
-			$worlds[] = $world->_rest;
-		}
-		echo \json_encode($worlds);
+	    \Swoole\Runtime::enableCoroutine();
+	    $queries = \min(\max($queries, 1), 500);
+	    $worlds = new co\Channel($queries);
+	    $count=\min(4,$queries);
+	    $rest=$queries%$count;
+	    $nb=($queries-$rest)/$count;
+	    for ($i = 0; $i < $nb; ++ $i) {
+	        $this->_update($count, $worlds);
+	    }
+	    if($rest>0){
+	        $this->_update($rest, $worlds);
+	    }
+	    $result=[];
+	    for($i=0;$i<$queries;++$i){
+	        $result[]=$worlds->pop();
+	    }
+	    echo \json_encode($result);
+	}
+	
+	private function _update($queries,$worlds) {
+        go(static function() use($queries,$worlds){
+	         $dbInstance=DAO::pool();
+	         for ($j = 0; $j < $queries; ++ $j) {
+	             $world = DAO::getById(World::class, \mt_rand(1, 10000), false);
+	             $world->randomNumber = \mt_rand(1, 10000);
+	             DAO::update($world);
+	             $worlds->push($world->_rest);
+	         }
+	         DAO::freePool($dbInstance);
+        });
 	}
 }

+ 4 - 1
frameworks/PHP/ubiquity/app/controllers/SwooleFortunes.php

@@ -2,11 +2,14 @@
 namespace controllers;
 
 use models\Fortune;
+use Ubiquity\orm\DAO;
 
 class SwooleFortunes extends \Ubiquity\controllers\Controller {
 
 	public function index() {
-		$fortunes = \Ubiquity\orm\DAO::getAll(Fortune::class, '', false);
+	    $dbInstance=DAO::pool();
+		$fortunes = DAO::getAll(Fortune::class, '', false);
+		DAO::freePool($dbInstance);
 		$fortunes[] = (new Fortune())->setId(0)->setMessage('Additional fortune added at request time.');
 		\usort($fortunes, function ($left, $right) {
 			return $left->message <=> $right->message;

+ 1 - 1
frameworks/PHP/ubiquity/ubiquity-swoole.dockerfile

@@ -1,6 +1,6 @@
 FROM php:7.3
 
-ENV SWOOLE_VERSION=4.3.4
+ENV SWOOLE_VERSION=4.4.5
 
 RUN cd /tmp && curl -sSL "https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz" | tar xzf - \
         && cd swoole-src-${SWOOLE_VERSION} \