stanley-king 3 年之前
父節點
當前提交
31bfb4bb3a

+ 1 - 1
core/framework/cache/cache.redis.php

@@ -28,7 +28,7 @@ class Cacheredis extends Cache
 
     public function __destruct()
     {
-        if(defined('USE_COROUTINE') && USE_COROUTINE)
+        if(defined('USE_COROUTINE') && USE_COROUTINE && defined(COROUTINE_HOOK_TCP) && COROUTINE_HOOK_TCP)
         {
             if(!CoRedisPool::instance()->stoped())
             {

+ 0 - 1
core/framework/libraries/CoPool.php

@@ -37,7 +37,6 @@ abstract class CoPool
         $this->mUsingCount++;
 
         Log::record("get mUsingCount={$this->mUsingCount}",Log::DEBUG);
-
         return $client;
     }
 

+ 82 - 0
core/framework/libraries/CoRefPool.php

@@ -0,0 +1,82 @@
+<?php
+
+abstract class CoRefPool
+{
+    private $mMaxClient;
+    private $mFreeClients;
+    private $mUsingClients;
+    private $mSuspendCIDS;
+    private $mStop;
+
+    public function __construct($max_clients)
+    {
+        $this->mMaxClient = $max_clients;
+        $this->mUsingClients = [];
+        $this->mFreeClients = [];
+        $this->mSuspendCIDS = [];
+        $this->mStop = false;
+    }
+
+    public function get($cid)
+    {
+        if(array_key_exists($cid,$this->mUsingClients)) {
+            $this->mUsingClients[$cid]['ref_count'] += 1;
+            return $this->mUsingClients[$cid]['object'];
+        }
+
+        if(!empty($this->mFreeClients)) {
+            $client = $this->mFreeClients[0];
+            unset($this->mFreeClients[0]);
+            $this->mFreeClients = array_values($this->mFreeClients);
+            $this->mUsingClients[$cid] = ['object' =>$client,'ref_count' => 1];
+        }
+        elseif(count($this->mUsingClients) < $this->mMaxClient) {
+            $client = $this->create_client($args);
+        }
+        else {
+            $this->mSuspendCIDS[] = Co::getCid();
+            Co::suspend();
+            $client = $this->mFreeClients[0];
+            unset($this->mFreeClients[0]);
+            $this->mFreeClients = array_values($this->mFreeClients);
+        }
+        $this->mUsingCount++;
+
+        Log::record("get mUsingCount={$this->mUsingCount}",Log::DEBUG);
+        return $client;
+    }
+
+    public function put($client) {
+
+        $this->mFreeClients[] = $client;
+        $this->mUsingCount--;
+        $cid = $this->getSuspend();
+        if($cid > 0) {
+            Co::resume($cid);
+        }
+        $count = count($this->mFreeClients);
+        $waitings = count($this->mSuspendCIDS);
+        Log::record("put cid = {$cid} mUsingCount={$this->mUsingCount} frees={$count} waitings={$waitings}",Log::DEBUG);
+    }
+
+    private function getSuspend()
+    {
+        if(empty($this->mSuspendCIDS)) {
+            return 0;
+        }
+        else {
+            $cid = $this->mSuspendCIDS[0];
+            unset($this->mSuspendCIDS[0]);
+            $this->mSuspendCIDS = array_values($this->mSuspendCIDS);
+            return $cid;
+        }
+    }
+    public function stoped() {
+        return $this->mStop;
+    }
+    public function stop() {
+        $this->mStop = true;
+    }
+
+    abstract public function create_client($args);
+}

+ 3 - 5
core/framework/libraries/cache.php

@@ -36,14 +36,12 @@ class Cache
 	 */
 	public static function getInstance()
     {
-        if(defined('USE_COROUTINE') && USE_COROUTINE)
-        {
+        if (defined('USE_COROUTINE') && USE_COROUTINE && defined(COROUTINE_HOOK_TCP) && COROUTINE_HOOK_TCP) {
             $args = func_get_args();
             return CoRedisPool::instance()->get($args);
-        }
-        else {
+        } else {
             $args = func_get_args();
-            return get_obj_instance(__CLASS__,'connect',$args);
+            return get_obj_instance(__CLASS__, 'connect', $args);
         }
 	}
 }

+ 1 - 1
core/framework/libraries/model.php

@@ -86,7 +86,7 @@ class Model
 	}
 	public function __destruct()
     {
-        //Log::record("{$this->table_name} __destruct",Log::DEBUG);
+        Log::record("{$this->table_name} __destruct",Log::DEBUG);
     }
 
     public function init($table)

+ 2 - 0
server/coredis_test.php

@@ -8,6 +8,7 @@ define('SUPPORT_PTHREAD', false);
 define('APP_ID', 'coredis');
 define('BASE_ROOT_PATH', str_replace('/server', '', dirname(__FILE__)));
 define('BASE_PATH', BASE_ROOT_PATH . '/server');
+define('COROUTINE_HOOK_TCP',true);
 
 require_once(BASE_ROOT_PATH . '/global.php');
 require_once(BASE_ROOT_PATH . '/fooder.php');
@@ -17,6 +18,7 @@ require_once(BASE_HELPER_PATH . '/refill_proxy.php');
 require_once(BASE_CORE_PATH . '/framework/libraries/CoPool.php');
 require_once(BASE_CORE_PATH . '/framework/libraries/CoRedisPool.php');
 
+
 Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL | SWOOLE_HOOK_SLEEP | SWOOLE_HOOK_TCP]);
 
 class RedisPoolTest