Browse Source

Updated Swoole with DB Pool support (#3809)

* Updated Swoole with DB Pool support

* Swoole Coroutine Namespace needed

* Fixed Seg Dump

* Reintroduced change without segdump

* Swoole change to pool ip

* Reverse changes to pool pop
Wulfklaue 7 years ago
parent
commit
5cfa94bd8b
1 changed files with 73 additions and 25 deletions
  1. 73 25
      frameworks/PHP/swoole/swoole-server.php

+ 73 - 25
frameworks/PHP/swoole/swoole-server.php

@@ -3,7 +3,14 @@ $server = new swoole_http_server('0.0.0.0', 8080, SWOOLE_BASE);
 $server->set(array(
     'worker_num' => NUMCORES
 ));
-$server->on('request', function ($req, $res) {
+
+$pool = new MySQLPool();
+
+$server->on('workerStart', function () use (&$pool) {
+    $pool->set_host_ip();
+});
+
+$server->on('request', function ($req, $res) use ($pool) {
 
     switch ($req->server['request_uri'])
     {
@@ -18,14 +25,7 @@ $server->on('request', function ($req, $res) {
             break;
 
         case "/db":
-            $db = new Swoole\Coroutine\Mysql;
-            $server = [
-                'host' => co::gethostbyname('tfb-database'),
-                'user' => 'benchmarkdbuser',
-                'password' => 'benchmarkdbpass',
-                'database' => 'hello_world'
-            ];
-            $db->connect($server);
+            $db = $pool->get();
 
             // Read number of queries to run from URL parameter
             $query_count = 1;
@@ -52,19 +52,14 @@ $server->on('request', function ($req, $res) {
             if (count($arr) === 1)
                 $arr = $arr[0];
 
+            $pool->put($db);
+
             $res->header('Content-Type', 'application/json');
             $res->end(json_encode($arr));
             break;
 
         case "/fortunes":
-            $db = new Swoole\Coroutine\Mysql;
-            $server = [
-                'host' => co::gethostbyname('tfb-database'),
-                'user' => 'benchmarkdbuser',
-                'password' => 'benchmarkdbpass',
-                'database' => 'hello_world' //;charset=utf8
-            ];
-            $db->connect($server);
+            $db = $pool->get();
 
             $fortune = [];
             // Define query
@@ -80,19 +75,14 @@ $server->on('request', function ($req, $res) {
 
             $html .= "</table></body></html>";
 
+            $pool->put($db);
+
             $res->header('Content-Type', 'text/html; charset=utf-8');
             $res->end($html);
             break;
 
         case "/updates":
-            $db = new Swoole\Coroutine\Mysql;
-            $server = [
-                'host' => co::gethostbyname('tfb-database'),
-                'user' => 'benchmarkdbuser',
-                'password' => 'benchmarkdbpass',
-                'database' => 'hello_world'
-            ];
-            $db->connect($server);
+            $db = $pool->get();
 
             $query_count = 1;
             if (isset($req->get['queries']) && $req->get['queries'] > 0)
@@ -116,6 +106,8 @@ $server->on('request', function ($req, $res) {
                 $arr[] = $world;
             }
 
+            $pool->put($db);
+
             $res->header('Content-Type', 'application/json');
             $res->end(json_encode($arr));
             break;
@@ -124,3 +116,59 @@ $server->on('request', function ($req, $res) {
 });
 
 $server->start();
+
+/**
+ * Class MySQLPool
+ *
+ * Deal with the fact that Swoole 2.1.3 has no build in database pooling
+ */
+class MySQLPool
+{
+    protected $pool;
+    var $pool_count = 0;
+
+    var $server = [
+        'host' => '',
+        'user' => 'benchmarkdbuser',
+        'password' => 'benchmarkdbpass',
+        'database' => 'hello_world'
+    ];
+
+    function __construct()
+    {
+        $this->pool = new \SplQueue;
+    }
+
+    function set_host_ip()
+    {
+        if( empty( $this->server['host'] ) )
+        {
+            $tfb_database_ip = Swoole\Coroutine::gethostbyname('tfb-database');
+            $this->server['host'] = $tfb_database_ip;
+        }
+    }
+
+    function put($db)
+    {
+        $this->pool->push($db);
+        $this->pool_count++;
+    }
+
+    function get()
+    {
+        if($this->pool_count > 0)
+        {
+            $this->pool_count--;
+            return $this->pool->pop();
+        }
+
+        // No idle connection to create a new connection
+        $db = new Swoole\Coroutine\Mysql;
+        $db->connect($this->server);
+
+        if ($db == false)
+            return false;
+        else
+            return $db;
+    }
+}