From 87b12e34cf00972e6bdc2cd22b65de03315d6218 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Tue, 30 Jul 2019 16:41:44 +0800 Subject: [PATCH] refactor(Coroutine): Remove Coroutine Model 1. Remove class Rid\Pool\ConnectionPool, Rid\{Database,Redis}\Coroutine, We now only keep permanent model. 2. Crontab: only print when function hit. 3. Remove class Rid\Http\Token Component. 4. Not remember empty session as invalid, just quick return false. --- CHANGELOG.md | 1 + apps/commands/TimerCommand.php | 39 ---- apps/components/Site.php | 7 +- apps/config/http_base.php | 31 +--- apps/config/http_coroutine.php | 101 ----------- apps/config/http_permanent.php | 21 --- apps/config/httpd.php | 2 +- apps/process/CronTabProcess.php | 6 +- framework/Base/Process.php | 8 +- .../Database/Coroutine/PDOConnection.php | 72 -------- framework/Http/Token.php | 167 ------------------ framework/Pool/ConnectionPool.php | 131 -------------- framework/Redis/Coroutine/RedisConnection.php | 71 -------- 13 files changed, 19 insertions(+), 638 deletions(-) delete mode 100644 apps/commands/TimerCommand.php delete mode 100644 apps/config/http_coroutine.php delete mode 100644 apps/config/http_permanent.php delete mode 100644 framework/Database/Coroutine/PDOConnection.php delete mode 100644 framework/Http/Token.php delete mode 100644 framework/Pool/ConnectionPool.php delete mode 100644 framework/Redis/Coroutine/RedisConnection.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 6edf05d..27b842d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - **Process:** Add custom Process Support - **Redis:** Add mutiDelete() function for Redis - **Tracker:** Add `retry in` field when failed +- **Tracker:** Move From Timer to Process - **UserInfo:** Add Cache Lock of user access_{time,ip} update - **ban:** Add table `ban_usernames` and `ban_emails` - **csrf:** Add Csrf Support diff --git a/apps/commands/TimerCommand.php b/apps/commands/TimerCommand.php deleted file mode 100644 index 6d4fcf6..0000000 --- a/apps/commands/TimerCommand.php +++ /dev/null @@ -1,39 +0,0 @@ - - */ -class TimerCommand extends Command -{ - /** - * 主函数 - */ - public function actionExec() - { - // 一次性定时 - Timer::new()->after(1000, function () { - var_dump(time()); - }); - // 持续定时 - $timer = new Timer(); - $timer->tick(1000, function () { - var_dump(time()); - }); - // 停止定时 - Timer::new()->after(10000, function () use ($timer) { - $timer->clear(); - }); - } -} diff --git a/apps/components/Site.php b/apps/components/Site.php index 2ebfd8c..378f67d 100644 --- a/apps/components/Site.php +++ b/apps/components/Site.php @@ -102,8 +102,9 @@ protected function loadCurUser($grant = 'cookies') protected function loadCurUserFromCookies() { $user_session_id = app()->request->cookie(Constant::cookie_name); - $user_id = app()->redis->zScore(Constant::mapUserSessionToId, $user_session_id); - if (false === $user_id) { + if (is_null($user_session_id)) return false; // quick return when cookies is not exist + + if (false === $user_id = app()->redis->zScore(Constant::mapUserSessionToId, $user_session_id)) { // First check cache if (false === app()->redis->zScore(Constant::invalidUserSessionZset, $user_session_id)) { // check session from database to avoid lost @@ -126,7 +127,7 @@ protected function loadCurUserFromPasskey() $passkey = app()->request->get('passkey'); $user_id = app()->redis->zScore(Constant::mapUserPasskeyToId, $passkey); if (false === $user_id) { - if (app()->redis->zScore(Constant::invalidUserPasskeyZset, $passkey) !== false) { + if (app()->redis->zScore(Constant::invalidUserPasskeyZset, $passkey) === false) { $user_id = app()->pdo->createCommand('SELECT `id` FROM `users` WHERE `passkey` = :passkey LIMIT 1;')->bindParams([ 'passkey' => $passkey ])->queryScalar(); diff --git a/apps/config/http_base.php b/apps/config/http_base.php index 6195580..b908969 100644 --- a/apps/config/http_base.php +++ b/apps/config/http_base.php @@ -1,5 +1,7 @@ 0, // 最大文件尺寸 ], - // Token - 'token' => [ - // 类路径 - 'class' => Rid\Http\Token::class, - // 保存处理者 - 'saveHandler' => [ - // 类路径 - 'class' => Rid\Redis\RedisConnection::class, - // 主机 - 'host' => env('REDIS_HOST'), - // 端口 - 'port' => env('REDIS_PORT'), - // 数据库 - 'database' => env('REDIS_DATABASE'), - // 密码 - 'password' => env('REDIS_PASSWORD'), - ], - // 保存的Key前缀 - 'saveKeyPrefix' => 'TOKEN:', - // 有效期 - 'expiresIn' => 604800, - // token键名 - 'name' => 'access_token', - ], - // Session 'session' => [ // 类路径 @@ -160,7 +137,7 @@ // 数据库 'pdo' => [ // 类路径 - 'class' => Rid\Database\PDOConnection::class, + 'class' => Rid\Database\Persistent\PDOConnection::class, // 数据源格式 'dsn' => env('DATABASE_DSN'), // 数据库用户名 @@ -177,7 +154,7 @@ // redis 'redis' => [ // 类路径 - 'class' => Rid\Redis\RedisConnection::class, + 'class' => Rid\Redis\Persistent\RedisConnection::class, // 主机 'host' => env('REDIS_HOST'), // 端口 diff --git a/apps/config/http_coroutine.php b/apps/config/http_coroutine.php deleted file mode 100644 index bbf7f33..0000000 --- a/apps/config/http_coroutine.php +++ /dev/null @@ -1,101 +0,0 @@ - [ - - 'token' => [ - 'saveHandler' => [ - // 类路径 - 'class' => Rid\Redis\Coroutine\RedisConnection::class, - // 连接池 - 'connectionPool' => [ - // 组件路径 - 'component' => 'token.connectionPool', - ], - ], - ], - - // 连接池 - 'token.connectionPool' => [ - // 类路径 - 'class' => Rid\Pool\ConnectionPool::class, - // 最小连接数 - 'min' => 5, - // 最大连接数 - 'max' => 50, - ], - - // Session - 'session' => [ - // 保存处理者 - 'saveHandler' => [ - // 类路径 - 'class' => Rid\Redis\Coroutine\RedisConnection::class, - // 连接池 - 'connectionPool' => [ - // 组件路径 - 'component' => 'session.connectionPool', - ], - ], - ], - - // 连接池 - 'session.connectionPool' => [ - // 类路径 - 'class' => Rid\Pool\ConnectionPool::class, - // 最小连接数 - 'min' => 5, - // 最大连接数 - 'max' => 50, - ], - - // 数据库 - 'pdo' => [ - // 类路径 - 'class' => Rid\Database\Coroutine\PDOConnection::class, - // 连接池 - 'connectionPool' => [ - // 组件路径 - 'component' => 'pdo.connectionPool', - ], - ], - - // 连接池 - 'pdo.connectionPool' => [ - // 类路径 - 'class' => Rid\Pool\ConnectionPool::class, - // 最小连接数 - 'min' => 5, - // 最大连接数 - 'max' => 50, - ], - - // redis - 'redis' => [ - // 类路径 - 'class' => Rid\Redis\Coroutine\RedisConnection::class, - // 连接池 - 'connectionPool' => [ - // 组件路径 - 'component' => 'redis.connectionPool', - ] - ], - - // 连接池 - 'redis.connectionPool' => [ - // 类路径 - 'class' => Rid\pool\ConnectionPool::class, - // 最小连接数 - 'min' => 5, - // 最大连接数 - 'max' => 50, - ], - - ], -]); diff --git a/apps/config/http_permanent.php b/apps/config/http_permanent.php deleted file mode 100644 index fb7fe83..0000000 --- a/apps/config/http_permanent.php +++ /dev/null @@ -1,21 +0,0 @@ - [ - - 'pdo' => [ - 'class' => Rid\Database\Persistent\PDOConnection::class, - ], - - 'redis' => [ - 'class' => Rid\Redis\Persistent\RedisConnection::class, - ], - - ], -]); diff --git a/apps/config/httpd.php b/apps/config/httpd.php index bd6efd5..6df9bfa 100644 --- a/apps/config/httpd.php +++ b/apps/config/httpd.php @@ -71,7 +71,7 @@ 'virtualHost' => [ 'host' => '127.0.0.1', 'port' => 9501, - 'configFile' => __DIR__ . '/http_permanent.php', + 'configFile' => __DIR__ . '/http_base.php', ], // 运行参数:https://wiki.swoole.com/wiki/page/274.html diff --git a/apps/process/CronTabProcess.php b/apps/process/CronTabProcess.php index 4a5197f..9e33595 100644 --- a/apps/process/CronTabProcess.php +++ b/apps/process/CronTabProcess.php @@ -12,7 +12,7 @@ use Rid\Base\Process; -class CronTabProcess extends Process +final class CronTabProcess extends Process { private $_print_flag = 1; // FIXME debug model on @@ -67,7 +67,7 @@ public function run() } } $end_time = time(); - $this->print_log('This Cron Work period Start At ' . $start_time . ', Cost Time: ' . number_format($start_time - $end_time, 10) . 's, With ' . $hit . ' Jobs hits.'); + if ($hit > 0) $this->print_log('This Cron Work period Start At ' . $start_time . ', Cost Time: ' . number_format($end_time - $start_time, 10) . 's, With ' . $hit . ' Jobs hits.'); } protected function clean_expired_zset_cache() { @@ -92,7 +92,7 @@ protected function clean_expired_zset_cache() { foreach ($clean_list as $item) { [$field, $msg] = $item; $clean_count = app()->redis->zRemRangeByScore($field, 0, $timenow); - if ($clean_list) $this->print_log(sprintf($msg, $clean_count)); + if ($clean_count > 0) $this->print_log(sprintf($msg, $clean_count)); } } diff --git a/framework/Base/Process.php b/framework/Base/Process.php index 13e070f..8de3568 100644 --- a/framework/Base/Process.php +++ b/framework/Base/Process.php @@ -17,7 +17,8 @@ class Process implements StaticInstanceInterface private $sleep_time; protected $_config; - public function run() { + public function run() + { } @@ -42,10 +43,13 @@ protected function resetSleepTime() $this->setSleepTime($this->_config['sleep']); } - final public function start($config) { + final public function start($config) + { $this->_config = $config; $this->resetSleepTime(); + println('New Custom process `' . static::class . '` added.'); + while (true) { $this->run(); sleep($this->getSleepTime()); diff --git a/framework/Database/Coroutine/PDOConnection.php b/framework/Database/Coroutine/PDOConnection.php deleted file mode 100644 index 088f460..0000000 --- a/framework/Database/Coroutine/PDOConnection.php +++ /dev/null @@ -1,72 +0,0 @@ -disconnect(); - } - - // 连接 - protected function connect() - { - if (isset($this->connectionPool)) { - $this->_pdo = $this->connectionPool->getConnection(function () { - return parent::createConnection(); - }); - } else { - $this->_pdo = parent::createConnection(); - } - } - - // 关闭连接 - public function disconnect() - { - if (isset($this->connectionPool) && isset($this->_pdo)) { - $this->connectionPool->releaseConnection($this->_pdo, function () { - parent::disconnect(); - }); - } else { - parent::disconnect(); - } - } - - // 重新连接 - protected function reconnect() - { - if (isset($this->connectionPool)) { - $this->connectionPool->destroyConnection(function () { - parent::disconnect(); - }); - } else { - parent::disconnect(); - } - $this->connect(); - } - -} diff --git a/framework/Http/Token.php b/framework/Http/Token.php deleted file mode 100644 index 7e47734..0000000 --- a/framework/Http/Token.php +++ /dev/null @@ -1,167 +0,0 @@ -loadTokenId(); - } - - // 请求后置事件 - public function onRequestAfter() - { - parent::onRequestAfter(); - // 关闭连接 - $this->saveHandler->disconnect(); - } - - // 载入TokenID - public function loadTokenId() - { - $this->_tokenId = \Rid::app()->request->get($this->name) or - $this->_tokenId = \Rid::app()->request->header($this->name) or - $this->_tokenId = \Rid::app()->request->post($this->name); - $this->_tokenKey = $this->saveKeyPrefix . $this->_tokenId; - } - - // 创建TokenID - public function createTokenId() - { - do { - $this->_tokenId = StringHelper::getRandomString($this->_tokenIdLength); - $this->_tokenKey = $this->saveKeyPrefix . $this->_tokenId; - } while ($this->saveHandler->exists($this->_tokenKey)); - } - - // 设置唯一索引 - public function setUniqueIndex($uniqueId, $uniqueIndexPrefix = 'client_credentials:') - { - $uniqueKey = $this->saveKeyPrefix . $uniqueIndexPrefix . $uniqueId; - // 删除旧token数据 - $oldTokenId = $this->saveHandler->get($uniqueKey); - if (!empty($oldTokenId)) { - $oldTokenkey = $this->saveKeyPrefix . $oldTokenId; - $this->saveHandler->del($oldTokenkey); - } - // 更新唯一索引 - $this->saveHandler->setex($uniqueKey, $this->expiresIn, $this->_tokenId); - // 在数据中加入索引信息 - $this->saveHandler->hmset($this->_tokenKey, ['__uidx__' => $uniqueId]); - } - - // 赋值 - public function set($name, $value) - { - $success = $this->saveHandler->hmset($this->_tokenKey, [$name => $value]); - $this->saveHandler->expire($this->_tokenKey, $this->expiresIn); - return $success ? true : false; - } - - // 取值 - public function get($name = null) - { - if (is_null($name)) { - $result = $this->saveHandler->hgetall($this->_tokenKey); - unset($result['__uidx__']); - return $result ?: []; - } - $value = $this->saveHandler->hget($this->_tokenKey, $name); - return $value === false ? null : $value; - } - - // 判断是否存在 - public function has($name) - { - $exist = $this->saveHandler->hexists($this->_tokenKey, $name); - return $exist ? true : false; - } - - // 删除 - public function delete($name) - { - $success = $this->saveHandler->hdel($this->_tokenKey, $name); - return $success ? true : false; - } - - // 清除token - public function clear() - { - $success = $this->saveHandler->del($this->_tokenKey); - return $success ? true : false; - } - - // 获取TokenId - public function getTokenId() - { - return $this->_tokenId; - } - - // 刷新token - public function refresh($uniqueIndexPrefix = 'client_credentials:') - { - // 判断 token 是否存在 - $tokenData = $this->saveHandler->hgetall($this->_tokenKey); - if (empty($tokenData)) { - return false; - } - // 定义变量 - $oldData = $tokenData; - $oldTokenKey = $this->_tokenKey; - $newTokenId = StringHelper::getRandomString($this->_tokenIdLength); - $newTokenKey = $this->saveKeyPrefix . $newTokenId; - $uniqueKey = $this->saveKeyPrefix . $uniqueIndexPrefix . $oldData['__uidx__']; - // 判断索引是否正确 - $exists = $this->saveHandler->exists($uniqueKey); - if (empty($exists)) { - return false; - } - // 删除旧数据 - $this->saveHandler->del($oldTokenKey); - // 生成新数据 - $this->saveHandler->hmset($newTokenKey, $oldData); - $this->saveHandler->expire($newTokenKey, $this->expiresIn); - // 更新索引信息 - $this->saveHandler->set($uniqueKey, $newTokenId); - $this->saveHandler->expire($uniqueKey, $this->expiresIn); - // 新 token 赋值到属性 - $this->_tokenId = $newTokenId; - $this->_tokenKey = $newTokenKey; - // 返回 - return true; - } - -} diff --git a/framework/Pool/ConnectionPool.php b/framework/Pool/ConnectionPool.php deleted file mode 100644 index 0263d55..0000000 --- a/framework/Pool/ConnectionPool.php +++ /dev/null @@ -1,131 +0,0 @@ -setCoroutineMode(Component::COROUTINE_MODE_REFERENCE); - // 创建协程队列 - $this->_queue = new \Swoole\Coroutine\Channel($this->min); - } - - // 获取连接池的统计信息 - public function getStats() - { - return [ - 'current_count' => $this->getCurrentCount(), - 'queue_count' => $this->getQueueCount(), - 'active_count' => $this->getActiveCount(), - ]; - } - - // 获取队列中的连接数 - public function getQueueCount() - { - $count = $this->_queue->stats()['queue_num']; - return $count < 0 ? 0 : $count; - } - - // 获取活跃的连接数 - public function getActiveCount() - { - return $this->_activeCount; - } - - // 获取当前总连接数 - public function getCurrentCount() - { - return $this->getQueueCount() + $this->getActiveCount(); - } - - // 活跃连接数自增 - protected function activeCountIncrement() - { - return ++$this->_activeCount; - } - - // 活跃连接数自减 - protected function activeCountDecrement() - { - return --$this->_activeCount; - } - - // 放入连接 - protected function push($connection) - { - $this->activeCountDecrement(); - if ($this->getQueueCount() < $this->min) { - return $this->_queue->push($connection); - } - return false; - } - - // 弹出连接 - protected function pop() - { - while (true) { - $connection = $this->_queue->pop(); - $this->activeCountIncrement(); - return $connection; - } - } - - // 获取连接 - public function getConnection($closure) - { - // 队列有连接,从队列取 - if ($this->getQueueCount() > 0) { - return $this->pop(); - } - // 达到最大连接数,从队列取 - if ($this->getCurrentCount() >= $this->max) { - return $this->pop(); - } - // 活跃连接数自增 - $this->activeCountIncrement(); - // 执行创建连接的匿名函数并返回 - return $closure(); - } - - // 释放连接 - public function releaseConnection($connection, $closure) - { - // 放入连接 - $this->push($connection); - // 执行销毁连接的匿名函数 - $closure(); - } - - // 销毁连接 - public function destroyConnection($closure) - { - // 执行销毁连接的匿名函数 - $closure(); - // 活跃连接数自减 - $this->activeCountDecrement(); - } - -} diff --git a/framework/Redis/Coroutine/RedisConnection.php b/framework/Redis/Coroutine/RedisConnection.php deleted file mode 100644 index 06fdbe2..0000000 --- a/framework/Redis/Coroutine/RedisConnection.php +++ /dev/null @@ -1,71 +0,0 @@ -disconnect(); - } - - // 连接 - protected function connect() - { - if (isset($this->connectionPool)) { - $this->_redis = $this->connectionPool->getConnection(function () { - return parent::createConnection(); - }); - } else { - $this->_redis = parent::createConnection(); - } - } - - // 关闭连接 - public function disconnect() - { - if (isset($this->connectionPool) && isset($this->_redis)) { - $this->connectionPool->releaseConnection($this->_redis, function () { - parent::disconnect(); - }); - } else { - parent::disconnect(); - } - } - - // 重新连接 - protected function reconnect() - { - if (isset($this->connectionPool)) { - $this->connectionPool->destroyConnection(function () { - parent::disconnect(); - }); - } else { - parent::disconnect(); - } - $this->connect(); - } - -}