From 3e20cde1e6d1fcc38f02afbf49f9ed654624e98b Mon Sep 17 00:00:00 2001 From: lq <13849061902@qq.com> Date: Wed, 26 Jun 2024 17:13:45 +0800 Subject: [PATCH] add code --- boot.php | 8 +- core/BeanFactory.php | 3 +- core/init/MyDB.php | 4 +- core/lib/DBPool.php | 23 ++++- core/lib/PDOPool.php | 19 +++- core/server/HttpServer.php | 12 +-- swoole-cli.exe.stackdump | 173 +++++++++++++++++++------------------ 7 files changed, 137 insertions(+), 105 deletions(-) diff --git a/boot.php b/boot.php index 98d51ec..b14c607 100644 --- a/boot.php +++ b/boot.php @@ -2,9 +2,9 @@ require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . "/app/config/define.php"; \Swoole\Runtime::enableCoroutine(true); +//手撸一个自己的swoole框架 // command swoole-cli boot.php start use Swoole\Process; -use Core\server\HttpServer; if ($argc >= 2) { $cmd = $argv[1]; if ($cmd == 'start') { @@ -12,7 +12,7 @@ if ($argc >= 2) { if ($argc == 3 && $argv[2] == '-d') { $daemonize = true; } - $http = new HttpServer($daemonize); + $http = new Core\server\HttpServer($daemonize); $http->run(); } else if ($cmd == 'stop') { $getpid = intval(file_get_contents("./Buddha.pid")); //获取上一次程序运行的 master_id @@ -29,7 +29,7 @@ if ($argc >= 2) { if ($argc == 3 && isset($argv[2]) && $argv == '-d') { $daemonize = true; } - $http = new HttpServer($daemonize); + $http = new Core\server\HttpServer($daemonize); echo "restart done" . PHP_EOL; $http->run(); } else { @@ -38,4 +38,6 @@ if ($argc >= 2) { } else { echo '无效命令'; } +}else{ + echo '请输入命令: swoole-cli boot.php start'; } \ No newline at end of file diff --git a/core/BeanFactory.php b/core/BeanFactory.php index 6abaf5c..05549d1 100644 --- a/core/BeanFactory.php +++ b/core/BeanFactory.php @@ -15,7 +15,7 @@ class BeanFactory private static $handlers = []; //初始化函数 - public static function init() + public static function init($bool = false) { self::$env = parse_ini_file(ROOT_PATH . "/.env"); //初始容器Builder @@ -125,6 +125,7 @@ class BeanFactory private static function handlerPropAnno(&$instance, \ReflectionClass $refClass, AnnotationReader $reader) { + //var_dump($refClass->getName()); //读取反射对象的属性 $props = $refClass->getProperties(); foreach ($props as $prop) { diff --git a/core/init/MyDB.php b/core/init/MyDB.php index a9af9a8..3ae7fb7 100644 --- a/core/init/MyDB.php +++ b/core/init/MyDB.php @@ -37,16 +37,16 @@ class MyDB $this->lvDB->bootEloquent(); } $this->transctionDB = $db_obj; - $this->pdopool = BeanFactory::getBean(PDOPool::class); + $this->pdopool = new PDOPool();//BeanFactory::getBean(PDOPool::class); if ($db_obj) { $this->lvDB->getConnection($this->dbSource)->setPdo($this->transctionDB->db); $this->lvDB->getConnection($this->dbSource)->beginTransaction(); } + } public function __call($methodName, $arguments) { - try{ if ($this->transctionDB) {//事务对象 $pdo_object = $this->transctionDB; diff --git a/core/lib/DBPool.php b/core/lib/DBPool.php index 9231b99..080ba36 100644 --- a/core/lib/DBPool.php +++ b/core/lib/DBPool.php @@ -18,6 +18,25 @@ abstract class DBPool for ($i = 0; $i < $min; $i++) { $this->addDBToPool(); } + + \Swoole\Timer::tick(30000, [$this, 'keepAlive']); + //\Swoole\Timer::tick(30000, [$this, 'keepAlive']); + } + + public function keepAlive() + { + for ($i = 0; $i < $this->conns->length(); $i++) { + $dbObj = $this->getConnection(); + try { + $res = $dbObj->db->query('select 1'); + //var_dump($res); + $this->close($dbObj); + }catch (\Exception $e) { + throw new \Exception($e->getMessage()); + } + } + + } public function initPool() @@ -44,12 +63,12 @@ abstract class DBPool if ($dbObject) { $dbObject->usedTime = time(); } - $res = $this->pdoPing($dbObject->db); + /**$res = $this->pdoPing($dbObject->db); if (!$res) { $this->count--; $this->addDBToPool(); return $this->getConnection(); - } + }*/ return $dbObject; } diff --git a/core/lib/PDOPool.php b/core/lib/PDOPool.php index 5cdef8f..c095b29 100644 --- a/core/lib/PDOPool.php +++ b/core/lib/PDOPool.php @@ -2,6 +2,8 @@ namespace Core\lib; +use function DI\get; + class PDOPool extends DBPool { public function __construct($min = 5, $max = 10) @@ -24,10 +26,21 @@ class PDOPool extends DBPool $password = $default["password"]; $dsn = "$driver:host=$host;dbname=$dbname"; } - $options = array( + $options = [ + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + \PDO::ATTR_EMULATE_PREPARES => false, + ]; + $options1 = array( \PDO::ATTR_PERSISTENT => true ); - $pdo = new \PDO($dsn, $username, $password); - return $pdo; + + try { + $pdo = new \PDO($dsn, $username, $password); + return $pdo; + }catch (\Exception $e) { + echo $e->getMessage() . PHP_EOL; + throw new \Exception('数据库链接失败'); + } } } \ No newline at end of file diff --git a/core/server/HttpServer.php b/core/server/HttpServer.php index e1d0d4a..0956996 100644 --- a/core/server/HttpServer.php +++ b/core/server/HttpServer.php @@ -12,23 +12,19 @@ class HttpServer private $dispatcher; private $host = 'localhost'; - private $port = 8085; + private $port = 8086; public function __construct($daemonize = false) { $this->server = new Server($this->host,$this->port); echo 'host: http://' . $this->host . ':' . $this->port . PHP_EOL; //配置参数 https://wiki.swoole.com/#/server/setting $this->server->set(array( - 'worker_num' => 2, //设置启动的 Worker 进程数。【默认值:CPU 核数】 - 'daemonize' => $daemonize // 使用docker 不需要设置守护进程 + 'worker_num' => 4, //设置启动的 Worker 进程数。【默认值:CPU 核数】 + 'daemonize' => $daemonize //设置守护进程 )); - //$this->server->on('request',function ($req,$res){}); $this->server->on('Request', [$this, 'onRequset']); - $this->server->on('Start', [$this, 'onStart']); - $this->server->on('ShutDown', [$this, 'onShutDown']); - $this->server->on('WorkerStart',[$this,'onWorkerStart']); $this->server->on('ManagerStart', [$this,"onManagerStart"]); } @@ -36,7 +32,7 @@ class HttpServer public function onWorkerStart(Server $server,int $workerId) { //cli_set_process_title('baihand worker');//设置进程名称 - \Core\BeanFactory::init(); + \Core\BeanFactory::init(true); $this->dispatcher = \Core\BeanFactory::getBean('RouterCollector')->getDispatcher(); } public function onManagerStart(Server $server) diff --git a/swoole-cli.exe.stackdump b/swoole-cli.exe.stackdump index f5b3cf1..b2d1c5a 100644 --- a/swoole-cli.exe.stackdump +++ b/swoole-cli.exe.stackdump @@ -1,163 +1,164 @@ Exception: STATUS_ACCESS_VIOLATION at rip=0001008C745A -rax=00000001008308E0 rbx=0000000A0053F010 rcx=0000000000000000 -rdx=0000000000000000 rsi=00000007FFFF8DB0 rdi=0000000000000068 -r8 =0000000000000000 r9 =0000000A0012F198 r10=0000000800000000 -r11=00000003FF76D542 r12=0000000000000000 r13=00006FFFFFDD0090 -r14=00000007FFFF8E40 r15=0000000100B7F110 -rbp=00000001015C881F rsp=00000007FFFF8CC0 -program=D:\software\swoole-cli\bin\swoole-cli.exe, pid 146, thread main +rax=00000001008308E0 rbx=0000000A00540780 rcx=0000000000000000 +rdx=0000000000000000 rsi=00000007FFFF6CC0 rdi=0000000000000080 +r8 =0000000000000000 r9 =0000000A0012F2C8 r10=0000000800000000 +r11=00000003FF76D542 r12=0000000000000000 r13=00006FFFFFDD0008 +r14=00000007FFFF6D50 r15=0000000100B7F110 +rbp=00000001015C881F rsp=00000007FFFF6BD0 +program=D:\software\swoole-cli\bin\swoole-cli.exe, pid 1973, thread main cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B Stack trace: Frame Function Args -0001015C881F 0001008C745A (000000000028, 0001015C881F, 0001015C87F0, 6FFFFFDD0090) swoole-cli.exe+0x4C745A -0001015C881F 0001008CED83 (000A00000012, 6FFF000001EF, 000A0042B0D8, 000000000000) swoole-cli.exe+0x4CED83 -0001015C881F 00010090A9F4 (000000000000, 000000000000, 000000200308, 000100911940) swoole-cli.exe+0x50A9F4 -00010171FBA0 000100922BC9 (000000000000, 000000000020, 6FFFFE4B0008, 6FFFFF62D500) swoole-cli.exe+0x522BC9 -000000200308 00010090EFC2 (000100922940, 000000000020, 6FFFFFDF0008, 000A00417180) swoole-cli.exe+0x50EFC2 -000000200308 000100910D5F (000100917E02, 000000200308, 000A00417180, 6FFFFFDF0198) swoole-cli.exe+0x510D5F -000000200308 00010091126A (000100B7F030, 000A003C4CE0, 000100A6A343, 000A00417C08) swoole-cli.exe+0x51126A -000000200308 00010091AA75 (6FFFFF62D510, 6FFFFFE13380, 6FFFFF638970, 6FFFFFE13410) swoole-cli.exe+0x51AA75 -000000200308 0001008A4A4A (0007FFFF9320, 000000000003, 000000000008, 6FFFFFE133F0) swoole-cli.exe+0x4A4A4A +0001015C881F 0001008C745A (0000000000D0, 0001015C87F0, 6FFFFFDD0008, 0007FFFF6D50) swoole-cli.exe+0x4C745A +0001015C881F 0001008CED83 (000A0000001B, 6FFF0000035A, 000A0042BA88, 000000000000) swoole-cli.exe+0x4CED83 +0001015C881F 00010090A9F4 (000000000000, 0007FFFF8F70, 6FFFFFDF0198, 000100911940) swoole-cli.exe+0x50A9F4 +00010171FBA0 000100922BC9 (07B1000000FF, 0007FFFF6EBC, 0007FFFF8F70, 000A00417290) swoole-cli.exe+0x522BC9 +6FFFFFDF0198 00010090EFC2 (0007FFFF6EBC, 000000000000, 7FF832386706, 000000000001) swoole-cli.exe+0x50EFC2 +6FFFFFDF0198 00010090FE7C (000100922940, 000000000020, 6FFFFFDF0008, 000A00417290) swoole-cli.exe+0x50FE7C +000000200308 000100910E3A (000100917E02, 000000200308, 000A00417290, 6FFFFFDF0198) swoole-cli.exe+0x510E3A +000000200308 00010091126A (000100B7F030, 000A003C4DF0, 000100A6A343, 000A00417D18) swoole-cli.exe+0x51126A +000000200308 00010091AA75 (6FFFFF62D510, 6FFFFFE13370, 6FFFFF638970, 6FFFFFE13400) swoole-cli.exe+0x51AA75 +000000200308 0001008A4A4A (0007FFFF9320, 000000000003, 000000000008, 6FFFFFE133E0) swoole-cli.exe+0x4A4A4A 000000200308 000100A4BA74 (0001017238E0, 6FFFFFE77D00, 0001009F00C6, 000000000000) swoole-cli.exe+0x64BA74 000000000000 000100A4DA72 (000700000008, 0007FFFFA630, 0007FFFFB8B0, 0007FFFFCC38) swoole-cli.exe+0x64DA72 0007FFFF9320 0001009E2A2B (0001009CA40D, 000000000000, 000000000000, 0007FFFFB810) swoole-cli.exe+0x5E2A2B 0007FFFFA380 00010097E3E3 (00010097C6F0, 000100BBCEE8, 00010097C6E0, 000700000000) swoole-cli.exe+0x57E3E3 000000000000 000100AC434D (0007FFFFCC30, 000000000025, 0000FF52424B, 0007FFFFCA84) swoole-cli.exe+0x6C434D -000000000000 000100BDD758 (0007FFFFCC30, 000000000000, 7FFD56028035, 0007FFFFCC50) swoole-cli.exe+0x7DD758 -0007FFFFCD30 7FFD560280A1 (000000000000, 000000000000, 000000000000, 000000000000) cygwin1.dll+0x80A1 -0007FFFFFFF0 7FFD56025C86 (000000000000, 000000000000, 000000000000, 000000000000) cygwin1.dll+0x5C86 -0007FFFFFFF0 7FFD56025D34 (000000000000, 000000000000, 000000000000, 000000000000) cygwin1.dll+0x5D34 +000000000000 000100BDD758 (0007FFFFCC30, 000000000000, 7FF832378035, 0007FFFFCC50) swoole-cli.exe+0x7DD758 +0007FFFFCD30 7FF8323780A1 (000000000000, 000000000000, 000000000000, 000000000000) cygwin1.dll+0x80A1 +0007FFFFFFF0 7FF832375C86 (000000000000, 000000000000, 000000000000, 000000000000) cygwin1.dll+0x5C86 +0007FFFFFFF0 7FF832375D34 (000000000000, 000000000000, 000000000000, 000000000000) cygwin1.dll+0x5D34 End of stack trace Loaded modules: 000100400000 swoole-cli.exe -7FFDE0E50000 ntdll.dll -7FFDDF570000 KERNEL32.DLL -7FFDDE170000 KERNELBASE.dll -0003F3A30000 cygMagickCore-7.Q16HDRI-7.dll +7FF8CEF70000 ntdll.dll +7FF8CEAA0000 KERNEL32.DLL +7FF8CC7D0000 KERNELBASE.dll 0003F38F0000 cygMagickWand-7.Q16HDRI-7.dll -7FFDDF190000 ADVAPI32.dll -0003FFE60000 cygbrotlidec-1.dll +0003F3A30000 cygMagickCore-7.Q16HDRI-7.dll 0003F3540000 cygbrotlienc-1.dll -7FFDE05C0000 msvcrt.dll -7FFDDF410000 sechost.dll -7FFDDE520000 bcrypt.dll +0003FFE60000 cygbrotlidec-1.dll 0003FFE40000 cygbz2-1.dll -7FFDDEB80000 RPCRT4.dll -7FFDE03D0000 GDI32.dll -7FFDDE550000 win32u.dll +7FF8CD130000 ADVAPI32.dll 0003F33C0000 cygcares-2.dll -0003FFAB0000 cygcurl-4.dll -7FFDDE580000 gdi32full.dll +7FF8CED80000 msvcrt.dll +7FF8CCD20000 sechost.dll 0003FFB60000 cygcrypto-1.1.dll -7FFDDE7C0000 msvcp_win.dll -7FFDDE6A0000 ucrtbase.dll -7FFDDEF30000 USER32.dll +7FF8CC4A0000 bcrypt.dll +7FF8CCE20000 RPCRT4.dll +0003FFAB0000 cygcurl-4.dll +7FF832370000 cygwin1.dll 0003F41C0000 cygexslt-0.dll -7FFD56020000 cygwin1.dll -0003FF580000 cyggmp-10.dll +7FF8CD670000 GDI32.dll 0003F2980000 cygfreetype-6.dll +0003FF580000 cyggmp-10.dll +7FF8CC310000 win32u.dll 0003F23D0000 cygiconv-2.dll -0003FF4F0000 cygicuio72.dll +7FF8CCB80000 gdi32full.dll 0003F20F0000 cygicui18n72.dll -0003EE7B0000 cygonig-5.dll -0003EEAE0000 cygjpeg-8.dll +7FF8CC400000 msvcp_win.dll +0003FF4F0000 cygicuio72.dll +7FF8CC4D0000 ucrtbase.dll 0003F1F30000 cygicuuc72.dll +7FF8CDB30000 USER32.dll +0003EEAE0000 cygjpeg-8.dll +0003EE7B0000 cygonig-5.dll +0003EE570000 cygpng16-16.dll 0003FE600000 cygreadline7.dll 0003F5940000 cygsqlite3-0.dll -0003EE570000 cygpng16-16.dll 0003FE470000 cygssl-1.1.dll 0003F4180000 cygxslt-1.dll +0003EDC80000 cygyaml-0-2.dll 0003FDF20000 cygxml2-2.dll 0003FDF00000 cygz.dll -0003EDC80000 cygyaml-0-2.dll 0003ED690000 cygzip-5.dll +0003FF540000 cyggomp-1.dll 0003FF760000 cyggcc_s-seh-1.dll 0003F37B0000 cygX11-6.dll -0003F3730000 cygXext-6.dll -0003FF540000 cyggomp-1.dll 0003FE290000 cygstdc++-6.dll -0003F3420000 cygcairo-2.dll +0003F3730000 cygXext-6.dll +0003FFE80000 cygbrotlicommon-1.dll 0003F3380000 cygcgraph-6.dll +0003F3420000 cygcairo-2.dll 0003F3160000 cygdjvulibre-21.dll -0003F2F20000 cygfftw3-3.dll -0003F2AE0000 cygfpx-1.dll -0003F2590000 cygglib-2.0-0.dll -0003F2520000 cyggobject-2.0-0.dll 0003F2B80000 cygfontconfig-1.dll -0003F0E70000 cyggvc-6.dll +0003F2AE0000 cygfpx-1.dll +0003F2F20000 cygfftw3-3.dll +0003F2520000 cyggobject-2.0-0.dll 0003EC0E0000 cyggs-9.dll +0003F2590000 cygglib-2.0-0.dll +0003F0E70000 cyggvc-6.dll 0003EEBA0000 cygjbig-2.dll +0003EEA60000 cyglqr-1-0.dll 0003EEA80000 cyglcms2-2.dll +0003FED10000 cyglzma-5.dll 0003EE700000 cygpango-1.0-0.dll 0003EE6E0000 cygpangocairo-1.0-0.dll -0003FED10000 cyglzma-5.dll -0003EEA60000 cyglqr-1-0.dll 0003EE240000 cygraqm-0.dll 0003EE060000 cygraw_r-16.dll -0003EDD80000 cygwebp-7.dll -0003EDD30000 cygwebpdemux-2.dll 0003EE020000 cygrsvg-2-2.dll 0003EDE10000 cygtiff-6.dll +0003EDD80000 cygwebp-7.dll +0003EDD30000 cygwebpdemux-2.dll 0003EDD10000 cygwebpmux-3.dll -0003FFE80000 cygbrotlicommon-1.dll 0003FF730000 cyggsasl-18.dll -0003FE750000 cygidn2-0.dll 0003FF4A0000 cyggssapi_krb5-2.dll +0003FE750000 cygidn2-0.dll +0003FEF80000 cyglber-2.dll 0003FEE60000 cygldap-2.dll 0003FE710000 cygnghttp2-14.dll -0003FEF80000 cyglber-2.dll -0003FE500000 cygssh2-1.dll 0003FE6B0000 cygpsl-5.dll +0003FE500000 cygssh2-1.dll 0003FDE50000 cygzstd-1.dll 0003FF640000 cyggcrypt-20.dll -0003FE9B0000 cygncursesw-10.dll 0003EA300000 cygicudata72.dll -0003EDCE0000 cygxcb-1.dll +0003FE9B0000 cygncursesw-10.dll 0003ED720000 cygcrypto-1.0.0.dll +0003EDCE0000 cygxcb-1.dll 0003EE5D0000 cygpixman-1-0.dll 0003F33A0000 cygcdt-5.dll 0003EDCC0000 cygxcb-render-0.dll 0003EDCB0000 cygxcb-shm-0.dll -0003FF2E0000 cygintl-8.dll 0003F3670000 cygXrender-1.dll -0003FE7B0000 cygpcre-1.dll -0003FF7A0000 cygffi-6.dll -0003FF810000 cygexpat-1.dll -0003EE680000 cygpathplan-4.dll 0003FE0A0000 cyguuid-1.dll -0003F3FE0000 cygltdl-7.dll +0003FF810000 cygexpat-1.dll +0003FF2E0000 cygintl-8.dll +0003FF7A0000 cygffi-6.dll 0003F3610000 cygXt-6.dll -0003F5E50000 cygtiff-7.dll 0003FF370000 cygidn-12.dll -0003EDE90000 cygthai-0.dll -0003EE6C0000 cygpangoft2-1.0-0.dll +0003FE7B0000 cygpcre-1.dll 0003EE6A0000 cygpaper-1.dll +0003F5E50000 cygtiff-7.dll +0003EE680000 cygpathplan-4.dll +0003F3FE0000 cygltdl-7.dll +0003EE6C0000 cygpangoft2-1.0-0.dll +0003EDE90000 cygthai-0.dll 0003F2960000 cygfribidi-0.dll -0003EEBC0000 cygjasper-4.dll -0003FEDB0000 cygsharpyuv-0.dll -0003F3300000 cygdeflate-0.dll 0003F5ED0000 cygharfbuzz-0.dll +0003EEBC0000 cygjasper-4.dll 0003F3330000 cygcroco-0.6-3.dll 0003F2820000 cyggdk_pixbuf-2.0-0.dll +0003F3300000 cygdeflate-0.dll +0003FEDB0000 cygsharpyuv-0.dll 0003F2690000 cyggio-2.0-0.dll -0003FF000000 cygkrb5-3.dll -0003FF0D0000 cygk5crypto-3.dll -0003FEFE0000 cygkrb5support-0.dll -0003FE590000 cygsasl2-3.dll -0003FFE30000 cygcom_err-2.dll 0003FE960000 cygntlm-0.dll +0003FF0D0000 cygk5crypto-3.dll +0003FF000000 cygkrb5-3.dll +0003FEFE0000 cygkrb5support-0.dll +0003FFE30000 cygcom_err-2.dll +0003FE590000 cygsasl2-3.dll 0003F1AF0000 cygunistring-5.dll 0003FF510000 cyggpg-error-0.dll 0003F37A0000 cygXau-6.dll 0003F3750000 cygXdmcp-6.dll 0003F3ED0000 cygICE-6.dll -0003F3320000 cygdatrie-1.dll 0003F38E0000 cygSM-6.dll +0003F3320000 cygdatrie-1.dll 0003F24F0000 cyggraphite2-3.dll 0003F2580000 cyggmodule-2.0-0.dll -7FFDDD8A0000 CRYPTBASE.DLL -7FFDDE9D0000 bcryptPrimitives.dll -7FFDDF530000 IMM32.DLL -7FFDDCD50000 authz.dll -7FFDDECA0000 ws2_32.dll -7FFDDD650000 mswsock.dll +7FF8CB990000 CRYPTBASE.DLL +7FF8CC290000 bcryptPrimitives.dll +7FF8CEB70000 IMM32.DLL +7FF8CAE50000 authz.dll +7FF8CCCA0000 ws2_32.dll +7FF8CB750000 mswsock.dll