我用workerman做了一個(gè)http服務(wù),作用是作為定時(shí)器來(lái)通知第三方服務(wù)器,然后量大了以后,nginx日志里發(fā)現(xiàn)了,這種錯(cuò)誤:
2024/06/16 16:52:28 [error] 28425#0: *314 recv() failed (104: Connection reset by peer) while reading response header from upstream
頁(yè)面等待一段時(shí)間返回的是nginx502錯(cuò)誤。
這是tp框架的代碼,下單以后,給workerman發(fā)http請(qǐng)求。
$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代碼,接到請(qǐng)求以后,定時(shí)發(fā)通知
$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
導(dǎo)致以上問(wèn)題以后,我重啟一下workerman,又可以正常運(yùn)行,但是一段時(shí)間以后,必定還會(huì)出現(xiàn)這種問(wèn)題,所以我按照官網(wǎng)說(shuō)的先安裝了event擴(kuò)展,我現(xiàn)在就是觀察狀態(tài)中。
請(qǐng)問(wèn),我該如何優(yōu)化。
通過(guò)netstat查看端口進(jìn)程情況,發(fā)現(xiàn)有很多2346的。
那么現(xiàn)在,我不知道是哪一塊出了問(wèn)題,我該如何面對(duì)這一切。
我該不該上:workerman-http或者wokerman-mysql