Skip to content

Commit

Permalink
Ubiquity swoole update to Coroutine Mysql (#5039)
Browse files Browse the repository at this point in the history
* Add coroutine Mysql to swoole (no more pdo)

+ connection pooling

* Update SwooleDb.php
  • Loading branch information
jcheron authored and NateBrady23 committed Sep 5, 2019
1 parent 4c19910 commit a5aa5b3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
2 changes: 2 additions & 0 deletions frameworks/PHP/ubiquity/.ubiquity/_swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
$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']);
44 changes: 35 additions & 9 deletions frameworks/PHP/ubiquity/app/controllers/SwooleDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Ubiquity\orm\DAO;
use models\World;
use Swoole\Coroutine as co;

/**
* Bench controller.
Expand All @@ -14,28 +15,53 @@ public function initialize() {
}

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);
});
}
}
5 changes: 4 additions & 1 deletion frameworks/PHP/ubiquity/app/controllers/SwooleFortunes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion frameworks/PHP/ubiquity/ubiquity-swoole.dockerfile
Original file line number Diff line number Diff line change
@@ -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} \
Expand Down

0 comments on commit a5aa5b3

Please sign in to comment.