Comments (10)
或者你可以判断$stmt = $obj->query('SELECT 1');
是不是返回false,是的话将$obj->error
作为错误信息返回出去
from swoole-src.
添加断线重连代码即可
参考 MySQL server has gone away ,添加断线重连代码
解决办法:
Try to reconnect to the database.
reference
- #4131 (comment)
- #2041
- https://github.com/swoole/swoole-wiki/blob/master/doc/15.11%20-%20MySQL%E7%9A%84%E8%BF%9E%E6%8E%A5%E6%B1%A0%E3%80%81%E5%BC%82%E6%AD%A5%E3%80%81%E6%96%AD%E7%BA%BF%E9%87%8D%E8%BF%9E.md
- #4141
- #5143
最佳的方案是进行断线重连
from swoole-src.
添加断线重连代码即可
参考 MySQL server has gone away ,添加断线重连代码
解决办法:
Try to reconnect to the database. reference
- MySQL server has gone away - how to deal with it #4131 (comment)
- Swoole 4.2.3 Task中使用协程MySQL报错 #2041
- https://github.com/swoole/swoole-wiki/blob/master/doc/15.11%20-%20MySQL%E7%9A%84%E8%BF%9E%E6%8E%A5%E6%B1%A0%E3%80%81%E5%BC%82%E6%AD%A5%E3%80%81%E6%96%AD%E7%BA%BF%E9%87%8D%E8%BF%9E.md
- PDOPool does not reconnect on connection timeout. #4141
- php8.2版本 swoole 5.0.3 使用mysqlipool 会报错 mysql server has gone away in @swoole/library/core/Database/MysqliProxy.php(51) #5143
最佳的方案是进行断线重连
无法捕获异常,如何知道断线了呢? pdo断线了会抛出 PDOEXCEPTION
from swoole-src.
执行sql 之前,需要判断 $conn
是否有效
$reconnect_count=0
A:
$conn = $obj->connect("host=postgres port=5432 dbname=postges user=postgres password=postgres", 3);
if (!$conn) {
var_dump($pg->error);
$reconnect_count++
if($reconnect_count<4){
goto A;
}
return;
}
from swoole-src.
或者用Swoole\Database\PDOPool
代替PostgreSQL协程客户端,这里已经封装了断线重连的逻辑了
from swoole-src.
执行sql 之前,需要判断
$conn
是否有效
$conn当然是有效的, 第一次的sql都能正常执行,是等了30秒后的第二次sql执行出问题
from swoole-src.
或者用Swoole\Database\PDOPool代替PostgreSQL协程客户端,这里已经封装了断线重连的逻辑了
Coroutine\PostgreSQL 之后是不再维护了么? 我们线上客户环境都没有安装pdo_pgsql扩展
另外这个真的不是断线重连的问题,只要抛出异常让用户知道故障了就行
from swoole-src.
或者你可以判断
$stmt = $obj->query('SELECT 1');
是不是返回false,是的话将$obj->error
作为错误信息返回出去
漏说了一句, 因为我有注册 set_error_handler, 这个注册的函数能拿到这个错误:
Swoole\Coroutine\PostgreSQL::query(): swoole_event_del failed
但是try catch 却捕获不到, 导致断线之后想在局部捕获都捕获不了,一定会抛给全局的错误处理函数
from swoole-src.
Swoole\Coroutine\PostgreSQL::query(): swoole_event_del failed
这个错误它不是异常来的,是swoole
内核直接将错误信息写入终端
from swoole-src.
需要检查 query 方法的返回值是不是 false,并获取错误码。据此实现断线重连
建议使用 pdo_pgsql + Runtime Hook, Swoole\Coroutine\PostgreSQL
已废弃了
from swoole-src.
Related Issues (20)
- Using package_length_func HOT 9
- Coroutine\Socket客户端绑定地址失败 HOT 5
- php.ini enable swoole extension composer error : segmentation fault php composer HOT 3
- 5.1.2 Context::parse_multipart_data 导致abort 进程退出 HOT 11
- A bug occurred in Swoole-v5.1.1, please report it. HOT 19
- Websocket connection failed. SSL related Issue? HOT 1
- Swoole not supporting partitioned cookie parameter HOT 1
- Can allow_redirects be supported on the HTTP/WebSocket? HOT 7
- Alpine + Swoole + PgSQL on the host = `could not send SSL negotiation packet: Resource temporarily unavailable` HOT 15
- 已解决
- 开启一键协程化之后, `proc_open()` 似乎会受到先前空方法体的 `run(function() {});` 的影响。 HOT 1
- Worker exits without much information HOT 3
- Websocket服務,調用Task做訊息發送,出現session does not exits HOT 5
- \Swoole\Server::shutdown never gets called and signals don't seem to work with \Swoole\Process::signal() / pcntl_signal() HOT 4
- A bug occurred in Swoole-v5.1.2, please report it HOT 25
- PHP Startup Unable to load dynamic library 'swoole' HOT 2
- guzzlehttp在stream=true时,设置超时无效 HOT 5
- 协程被长时间挂起会导致内存大量增加 HOT 49
- windows swoole-cli-v4.8.13-cygwin-x64.zip 在windows上 run(function{}) 内的curl 问题 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swoole-src.