mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 23:55:35 +08:00
initial commit
This commit is contained in:
46
src/ZM/Utils/DataProvider.php
Normal file
46
src/ZM/Utils/DataProvider.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Utils;
|
||||
|
||||
|
||||
use Co;
|
||||
use Framework\Console;
|
||||
use Framework\ZMBuf;
|
||||
|
||||
class DataProvider
|
||||
{
|
||||
public static $buffer_list = [];
|
||||
|
||||
public static function getResourceFolder() {
|
||||
return WORKING_DIR . '/resources/';
|
||||
}
|
||||
|
||||
public static function addSaveBuffer($buf_name, $sub_folder = null) {
|
||||
$name = ($sub_folder ?? "") . "/" . $buf_name . ".json";
|
||||
self::$buffer_list[$buf_name] = $name;
|
||||
ZMBuf::set($buf_name, self::getJsonData($name));
|
||||
}
|
||||
|
||||
public static function saveBuffer() {
|
||||
$head = Console::setColor(date("[H:i:s ") . "INFO] Saving buffer......", "lightblue");
|
||||
echo $head;
|
||||
foreach(self::$buffer_list as $k => $v) {
|
||||
self::setJsonData($v, ZMBuf::get($k));
|
||||
}
|
||||
echo Console::setColor("saved", "lightblue").PHP_EOL;
|
||||
}
|
||||
|
||||
private static function getJsonData(string $string) {
|
||||
if(!file_exists(self::getDataFolder().$string)) return [];
|
||||
return json_decode(Co::readFile(self::getDataFolder().$string), true);
|
||||
}
|
||||
|
||||
private static function setJsonData($filename, array $args) {
|
||||
Co::writeFile(self::getDataFolder() . $filename, json_encode($args, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING));
|
||||
}
|
||||
|
||||
private static function getDataFolder() {
|
||||
return CONFIG_DIR;
|
||||
}
|
||||
}
|
||||
101
src/ZM/Utils/SQLPool.php
Normal file
101
src/ZM/Utils/SQLPool.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: jerry
|
||||
* Date: 2019/1/5
|
||||
* Time: 4:48 PM
|
||||
*/
|
||||
|
||||
namespace ZM\Utils;
|
||||
|
||||
use framework\Console;
|
||||
use framework\ZMBuf;
|
||||
use SplQueue;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\Mysql;
|
||||
|
||||
class SQLPool
|
||||
{
|
||||
protected $available = true;
|
||||
protected $pool;
|
||||
private $info;
|
||||
|
||||
public $co_list = [];
|
||||
public $connect_cnt = 0;
|
||||
|
||||
public function __construct() {
|
||||
$this->pool = new SplQueue;
|
||||
$this->info = [
|
||||
"host" => ZMBuf::globals("sql_config")["sql_host"],
|
||||
"port" => ZMBuf::globals("sql_config")["sql_port"],
|
||||
"user" => ZMBuf::globals("sql_config")["sql_username"],
|
||||
"password" => ZMBuf::globals("sql_config")["sql_password"],
|
||||
"database" => ZMBuf::globals("sql_config")["sql_database"]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 将利用过的连接入队
|
||||
* @param $mysql
|
||||
*/
|
||||
public function put($mysql) {
|
||||
$this->pool->push($mysql);
|
||||
if (($a = array_shift($this->co_list)) !== null) {
|
||||
Coroutine::resume($a);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取队中的连接,如果不存在则创建新的
|
||||
* @param bool $no_new_conn
|
||||
* @return bool|mixed|Mysql
|
||||
*/
|
||||
public function get($no_new_conn = false) {
|
||||
if (count($this->pool) == 0 && $this->connect_cnt <= 70) {
|
||||
if($no_new_conn) return false;
|
||||
$this->connect_cnt += 1;
|
||||
$r = $this->newConnect();
|
||||
if ($r !== false) {
|
||||
return $r;
|
||||
} else {
|
||||
$this->connect_cnt -= 1;
|
||||
return false;
|
||||
}
|
||||
} elseif (count($this->pool) > 0) {
|
||||
$con = $this->pool->pop();
|
||||
if ($con->connected !== false) return $con;
|
||||
} elseif ($this->connect_cnt > 70) {
|
||||
$this->co_list[]=Coroutine::getuid();
|
||||
Console::warning("数据库连接过多,协程等待重复利用中...当前协程数 ".Coroutine::stats()["coroutine_num"]);
|
||||
Coroutine::suspend();
|
||||
return $this->get($no_new_conn);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getCount() {
|
||||
return $this->pool->count();
|
||||
}
|
||||
|
||||
public function destruct() {
|
||||
// 连接池销毁, 置不可用状态, 防止新的客户端进入常驻连接池, 导致服务器无法平滑退出
|
||||
$this->available = false;
|
||||
while (!$this->pool->isEmpty()) {
|
||||
$this->pool->pop();
|
||||
}
|
||||
}
|
||||
|
||||
private function newConnect() {
|
||||
//无空闲连接,创建新连接
|
||||
$mysql = new Mysql();
|
||||
|
||||
Console::info("创建SQL连接中,当前有" . $this->connect_cnt . "个连接");
|
||||
$res = $mysql->connect($this->info);
|
||||
if ($res == false) {
|
||||
echo $mysql->error . PHP_EOL;
|
||||
return false;
|
||||
} else {
|
||||
return $mysql;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/ZM/Utils/ScheduleManager.php
Normal file
10
src/ZM/Utils/ScheduleManager.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Utils;
|
||||
|
||||
|
||||
class ScheduleManager
|
||||
{
|
||||
//TODO: 写framework部分的schedule控制代码
|
||||
}
|
||||
71
src/ZM/Utils/ZMUtil.php
Normal file
71
src/ZM/Utils/ZMUtil.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Utils;
|
||||
|
||||
|
||||
use Co;
|
||||
use framework\Console;
|
||||
use Framework\ZMBuf;
|
||||
|
||||
class ZMUtil
|
||||
{
|
||||
/**
|
||||
* 检查workerStart是否运行结束
|
||||
*/
|
||||
public static function checkWait() {
|
||||
if(ZMBuf::isset("wait_start")) {
|
||||
ZMBuf::append("wait_start", Co::getCid());
|
||||
Co::suspend();
|
||||
}
|
||||
}
|
||||
|
||||
public static function stop() {
|
||||
Console::info(Console::setColor("Stopping server...", "red"));
|
||||
foreach (ZMBuf::$server->connections as $v) {
|
||||
ZMBuf::$server->close($v);
|
||||
}
|
||||
DataProvider::saveBuffer();
|
||||
ZMBuf::$server->shutdown();
|
||||
ZMBuf::$server->stop();
|
||||
}
|
||||
|
||||
public static function getHttpCodePage(int $http_code) {
|
||||
if(isset(ZMBuf::globals("http_default_code_page")[$http_code])) {
|
||||
return Co::readFile(DataProvider::getResourceFolder()."html/".ZMBuf::globals("http_default_code_page")[$http_code]);
|
||||
} else return null;
|
||||
}
|
||||
|
||||
public static function reload() {
|
||||
Console::info(Console::setColor("Reloading server...", "gold"));
|
||||
foreach (ZMBuf::$server->connections as $v) {
|
||||
ZMBuf::$server->close($v);
|
||||
}
|
||||
DataProvider::saveBuffer();
|
||||
ZMBuf::$server->reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析CQ码
|
||||
* @param $msg
|
||||
* @return array|null
|
||||
* 0123456
|
||||
* [CQ:at]
|
||||
*/
|
||||
static function getCQ($msg) {
|
||||
if (($start = mb_strpos($msg, '[')) === false) return null;
|
||||
if (($end = mb_strpos($msg, ']')) === false) return null;
|
||||
$msg = mb_substr($msg, $start + 1, $end - $start - 1);
|
||||
if (mb_substr($msg, 0, 3) != "CQ:") return null;
|
||||
$msg = mb_substr($msg, 3);
|
||||
$msg2 = explode(",", $msg);
|
||||
$type = array_shift($msg2);
|
||||
$array = [];
|
||||
foreach ($msg2 as $k => $v) {
|
||||
$ss = explode("=", $v);
|
||||
$sk = array_shift($ss);
|
||||
$array[$sk] = implode("=", $ss);
|
||||
}
|
||||
return ["type" => $type, "params" => $array, "start" => $start, "end" => $end];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user