FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add coroutine stream hook support · yzmcoder/framework@3606778 · GitHub

Repository navigation

Commit 3606778

Browse files
committed
add coroutine stream hook support
1 parent 6a9a86c commit 3606778

8 files changed

Lines changed: 222 additions & 26 deletions

File tree

‎libs/Swoole/Component/Redis.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function connect()
7777
try
7878
{
7979
$this->_redis = new \Redis();
80-
if ($this->config['pconnect'])
80+
if (!empty($this->config['pconnect']))
8181
{
8282
$this->_redis->pconnect($this->config['host'], $this->config['port'], $this->config['timeout']);
8383
}

‎libs/Swoole/Coroutine/Component/Base.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function _createObject()
3737
{
3838
$object = $this->pool->pop();
3939
//必须要 Swoole 2.1.1 以上版本
40-
if ($object->connected === false)
40+
if (property_exists($object, "connected") and $object->connected === false)
4141
{
4242
continue;
4343
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Swoole\Coroutine\Component\Hook;
4+
5+
use Swoole\Coroutine\Component\Base;
6+
use Swoole\Coroutine\Context;
7+
use Swoole\Database\MySQLi as CoMysql;
8+
9+
10+
class MySQL extends Base
11+
{
12+
protected $type = 'mysql';
13+
14+
function __construct($config)
15+
{
16+
parent::__construct($config);
17+
\Swoole::getInstance()->beforeAction([$this, '_createObject'], \Swoole::coroModuleDb);
18+
\Swoole::getInstance()->afterAction([$this, '_freeObject'],\Swoole::coroModuleDb);
19+
}
20+
21+
function create()
22+
{
23+
$db = new CoMySQL($this->config);
24+
if ($db->connect() === false)
25+
{
26+
return false;
27+
}
28+
else
29+
{
30+
return $db;
31+
}
32+
}
33+
34+
function query($sql)
35+
{
36+
/**
37+
* @var $db CoMySQL
38+
*/
39+
$db = $this->_getObject();
40+
if (!$db)
41+
{
42+
return false;
43+
}
44+
45+
$result = false;
46+
for ($i = 0; $i < 2; $i++)
47+
{
48+
$result = $db->query($sql);
49+
if ($result === false)
50+
{
51+
$db->close();
52+
Context::delete($this->type);
53+
$db = $this->_createObject();
54+
continue;
55+
}
56+
break;
57+
}
58+
59+
return $result;
60+
}
61+
62+
/**
63+
* 调用$driver的自带方法
64+
* @param $method
65+
* @param array $args
66+
* @return mixed
67+
*/
68+
function __call($method, $args = array())
69+
{
70+
$redis = $this->_getObject();
71+
if (!$redis)
72+
{
73+
return false;
74+
}
75+
return $redis->{$method}(...$args);
76+
}
77+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Swoole\Coroutine\Component\Hook;
4+
5+
use Swoole\Coroutine\Component\Base;
6+
use Swoole\Component\Redis as CoRedis;
7+
8+
class Redis extends Base
9+
{
10+
protected $type = 'redis';
11+
12+
function __construct($config)
13+
{
14+
parent::__construct($config);
15+
\Swoole::getInstance()->beforeAction([$this, '_createObject'],\Swoole::coroModuleRedis);
16+
\Swoole::getInstance()->afterAction([$this, '_freeObject'],\Swoole::coroModuleRedis);
17+
}
18+
19+
20+
function create()
21+
{
22+
return new CoRedis($this->config);
23+
}
24+
25+
/**
26+
* 调用$driver的自带方法
27+
* @param $method
28+
* @param array $args
29+
* @return mixed
30+
*/
31+
function __call($method, $args = array())
32+
{
33+
$redis = $this->_getObject();
34+
if (!$redis)
35+
{
36+
return false;
37+
}
38+
return $redis->{$method}(...$args);
39+
}
40+
}

‎libs/Swoole/Database.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ function __construct($db_config)
8282
case self::TYPE_COMYSQL:
8383
$this->_db = new Coroutine\Component\MySQL($db_config);
8484
break;
85+
case self::TYPE_COHOOKMYSQL:
86+
$this->_db = new Coroutine\Component\HOOK\MySQL($db_config);
87+
break;
8588
default:
8689
$this->_db = new Database\PdoDB($db_config);
8790
break;

‎libs/Swoole/Swoole.php‎

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class Swoole
109109
* @var bool
110110
*/
111111
static $enableCoroutine = false;
112+
static $coroutineStreamHook = [];
113+
static $coroutineStreamHookSupported = ['redis','mysql'];
112114
protected static $coroutineInit = false;
113115
const coroModuleDb = 1;
114116
const coroModuleRedis = 2;
@@ -317,6 +319,22 @@ static function go($func)
317319
});
318320
}
319321

322+
static function setCoroutineStreamHook($type = null)
323+
{
324+
if ($type)
325+
{
326+
if (!in_array($type, self::$coroutineStreamHookSupported))
327+
{
328+
throw new RuntimeException("stream hook type [$type] not support");
329+
}
330+
self::$coroutineStreamHook[$type] = $type;
331+
}
332+
foreach (self::$coroutineStreamHookSupported as $_type)
333+
{
334+
self::$coroutineStreamHook[$_type] = $_type;
335+
}
336+
}
337+
320338
/**
321339
* 执行Hook函数列表
322340
* @param $type
@@ -490,16 +508,23 @@ protected function loadModule($module, $id = 'master')
490508
{
491509
$object = require $user_factory_file;
492510
}
493-
//Swoole 2.0 协程模式
511+
//Swoole协程模式
494512
elseif (self::$enableCoroutine)
495513
{
496-
$system_factory_2x_file = LIBPATH . '/factory_2x/' . $module . '.php';
514+
if (!empty(self::$coroutineStreamHook[$module]))
515+
{
516+
$system_factory_file = LIBPATH . '/factory_hook/' . $module . '.php';
517+
}
518+
else
519+
{
520+
$system_factory_file = LIBPATH . '/factory_2x/' . $module . '.php';
521+
}
497522
//不存在,继续使用 1.x 的工厂
498-
if (!is_file($system_factory_2x_file))
523+
if (!is_file($system_factory_file))
499524
{
500525
goto get_factory_file;
501526
}
502-
$object = require $system_factory_2x_file;
527+
$object = require $system_factory_file;
503528
}
504529
//系统默认
505530
else
@@ -763,33 +788,33 @@ function handlerServer(Swoole\Request $request)
763788
/**
764789
* 加载所有模块
765790
*/
766-
function loadAllModules()
791+
function loadAllModules($type = null)
767792
{
768-
//redis
769-
$redis_conf = $this->config['redis'];
770-
if (!empty($redis_conf))
793+
$support = ['db','redis','cache'];
794+
if (!empty($type) and in_array($type,$support))
771795
{
772-
foreach ($redis_conf as $k => $v)
796+
$conf = $this->config[$type];
797+
if (!empty($conf))
773798
{
774-
$this->loadModule('redis', $k);
775-
}
776-
}
777-
//cache
778-
$cache_conf = $this->config['cache'];
779-
if (!empty($cache_conf))
780-
{
781-
foreach ($cache_conf as $k => $v)
782-
{
783-
$this->loadModule('cache', $k);
799+
foreach ($conf as $k => $v)
800+
{
801+
$this->loadModule($type, $k);
802+
}
784803
}
785804
}
786-
//db
787-
$db_conf = $this->config['db'];
788-
if (!empty($db_conf))
805+
else
789806
{
790-
foreach ($db_conf as $k => $v)
807+
808+
foreach ($support as $_type)
791809
{
792-
$this->loadModule('db', $k);
810+
$conf = $this->config[$_type];
811+
if (!empty($conf))
812+
{
813+
foreach ($conf as $k => $v)
814+
{
815+
$this->loadModule($_type, $k);
816+
}
817+
}
793818
}
794819
}
795820
}

‎libs/factory_hook/db.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
global $php;
3+
$configs = $php->config['db'];
4+
if (empty($configs[$php->factory_key]))
5+
{
6+
throw new Swoole\Exception\Factory("db->{$php->factory_key} is not found.");
7+
}
8+
$config = $configs[$php->factory_key];
9+
10+
$config['type'] = \Swoole\Database::TYPE_COHOOKMYSQL;
11+
if (!empty($config['passwd']))
12+
{
13+
$config['password'] = $config['passwd'];
14+
unset($config['passwd']);
15+
}
16+
if (!empty($config['name']))
17+
{
18+
$config['database'] = $config['name'];
19+
unset($config['name']);
20+
}
21+
22+
//用于隔离多实例
23+
$config['object_id'] = $php->factory_key;
24+
25+
$db = new Swoole\Database($config);
26+
27+
return $db;

‎libs/factory_hook/redis.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
global $php;
3+
4+
$config = $php->config['redis'][$php->factory_key];
5+
if (empty($config) or empty($config['host']))
6+
{
7+
throw new Exception("require redis[$php->factory_key] config.");
8+
}
9+
10+
if (empty($config['port']))
11+
{
12+
$config['port'] = 6379;
13+
}
14+
15+
if (empty($config['timeout']))
16+
{
17+
$config['timeout'] = 0.5;
18+
}
19+
20+
//用于隔离多实例
21+
$config['object_id'] = $php->factory_key;
22+
23+
$redis = new Swoole\Coroutine\Component\Hook\Redis($config);
24+
return $redis;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL