我用workerman做了一个http服务,作用是作为定时器来通知第三方服务器,然后量大了以后,nginx日志里发现了,这种错误:
2024/06/16 16:52:28 [error] 28425#0: *314 recv() failed (104: Connection reset by peer) while reading response header from upstream
页面等待一段时间返回的是nginx502错误。
这是tp框架的代码,下单以后,给workerman发http请求。
$postData = [
'id' => $event,
];
$url = 'http://localhost:2346';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
trace($error_msg, 'error');
}
curl_close($ch);
这是workman代码,接到请求以后,定时发通知
$worker = new Worker('http://0.0.0.0:2346');
$worker->onMessage = function (TcpConnection $connection, Request $request) use ($db) {
$post = $request->post();
$id = $post['id'];
$connection->close();
Timer::add(15, function () use ($connection, $id, $db) {
echo "#{$connection->id} Order {$id}\n";
$MerchantOrder = $db::table('merchant_order')->find($id);
$postData = [
'order_id' => $MerchantOrder['order_id'],
'amount' => $MerchantOrder['amount'],
'status' => $MerchantOrder['status'],
'notes' => $MerchantOrder['notes'],
];
$url = $MerchantOrder['callback_url'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
trace($error_msg, 'error');
}
curl_close($ch);
}, [], false);
Timer::add(30, function () use ($connection, $id, $db) {
echo "#{$connection->id} Order {$id}\n";
$MerchantOrder = $db::table('merchant_order')->find($id);
$postData = [
'order_id' => $MerchantOrder['order_id'],
'amount' => $MerchantOrder['amount'],
'status' => $MerchantOrder['status'],
'notes' => $MerchantOrder['notes'],
];
$url = $MerchantOrder['callback_url'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
trace($error_msg, 'error');
}
curl_close($ch);
}, [], false);
Timer::add(60, function () use ($connection, $id, $db) {
echo "#{$connection->id} Order {$id}\n";
$MerchantOrder = $db::table('merchant_order')->find($id);
$postData = [
'order_id' => $MerchantOrder['order_id'],
'amount' => $MerchantOrder['amount'],
'status' => $MerchantOrder['status'],
'notes' => $MerchantOrder['notes'],
];
$url = $MerchantOrder['callback_url'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
trace($error_msg, 'error');
}
curl_close($ch);
}, [], false);
Timer::add(180, function () use ($connection, $id, $db) {
echo "#{$connection->id} Order {$id}\n";
$MerchantOrder = $db::table('merchant_order')->find($id);
$postData = [
'order_id' => $MerchantOrder['order_id'],
'amount' => $MerchantOrder['amount'],
'status' => $MerchantOrder['status'],
'notes' => $MerchantOrder['notes'],
];
$url = $MerchantOrder['callback_url'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
trace($error_msg, 'error');
}
curl_close($ch);
}, [], false);
Timer::add(300, function () use ($connection, $id, $db) {
echo "#{$connection->id} Order {$id}\n";
$MerchantOrder = $db::table('merchant_order')->find($id);
$postData = [
'order_id' => $MerchantOrder['order_id'],
'amount' => $MerchantOrder['amount'],
'status' => $MerchantOrder['status'],
'notes' => $MerchantOrder['notes'],
];
$url = $MerchantOrder['callback_url'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
trace($error_msg, 'error');
}
curl_close($ch);
}, [], false);
};
Worker::runAll();
2024/06/16 16:52:28 [error] 28425#0: *314 recv() failed (104: Connection reset by peer) while reading response header from upstream
导致以上问题以后,我重启一下workerman,又可以正常运行,但是一段时间以后,必定还会出现这种问题,所以我按照官网说的先安装了event扩展,我现在就是观察状态中。
请问,我该如何优化。
通过netstat查看端口进程情况,发现有很多2346的。
那么现在,我不知道是哪一块出了问题,我该如何面对这一切。
我该不该上:workerman-http或者wokerman-mysql
订单按指定的间隔时间,通知回调方;这个用队列来解决即可。
我去看了rabbitMQ了,目前我掌握不了,时间太紧了。谢谢。
是不是超时没处理断连了
好像是的,我直接给curl加了个2秒超时,就没问题了。谢谢。