系统:windows server 2016
workerman:4.0.19
php:7.3.4
push主进程代码:
// 与 接收task 服务建立异步连接
$task_received_connection = new AsyncTcpConnection('tcp://127.0.0.1:2000');
// 告诉接收进程,我是 push 进程
$task_received_connection->send(json_encode(array('type' => 'push_process')));
// 异步获得结果
$task_received_connection->onMessage = function ($task_r_c, $data) {
$msg = 'date: ' . date('Y-m-d H:i:s') . '--- task_received_message: ' . $data;
echo $msg;
};
$task_received_connection->connect(); // 执行接收 task 异步连接
接收进程代码:
<?php
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
use Common\Common\PRedis;
use Common\Common\Bytes;
use Workerman\RedisQueue\Client as RQClient;
use Workerman\Redis\Client;
use Workerman\MySQL\Connection;
class Received
{
public $worker = null; // 初始化 worker 容器,监听 997 端口
public $redis = null; // redis 单例
public $uidConnections = array(); // 保存 客户端的 uid 标识(唯一) 到 connection 的映射
public $mysql = null; // mysql 实例
public $config = array(); // 加载 mysql 配置
public $push_process_str = 'push_process'; // push 进程名称字符串
public $special_mark = '!@#*'; // 每个数据结尾加上此特殊标记,用于分开数据
public function __construct()
{
require_once __DIR__ . '/vendor/autoload.php';
$this->worker = new Worker("tcp://0.0.0.0:2000");
// 这里进程数只能设置为 1
$this->worker->count = 1;
$this->worker->name = 'TaskWorker';
// 调用类的方法
$this->worker->onWorkerStart = array($this, 'onWorkerStart');
$this->worker->onConnect = array($this, 'onConnect');
$this->worker->onMessage = array($this, 'onMessage');
$this->worker->onClose = array($this, 'onClose');
$this->worker->onWorkerStop = array($this, 'onWorkerStop');
// 运行所有的 worker(其实当前只定义了一个)
Worker::runAll();
}
/**
* worker 进程启动后建立一个内部通讯端口
* @param $worker
* @throws Exception
*/
public function onWorkerStart($worker)
{
}
/**
* 当有客户端连接时执行该回调函数
* @param $connection
*/
public function onConnect($connection)
{
$ip_msg = "new connection Received from ip " . $connection->getRemoteIp();
var_dump('date: ' . date('Y-m-d H:i:s') . '---' . $ip_msg);
}
/**
* 当有客户端发来消息时执行该回调函数
* @param $connection
* @param $message
* @throws Exception
*/
public function onMessage($connection, $message)
{
$received_msg = "received 2000 port message: " . $message;
var_dump($received_msg);
$message_arr = json_decode($message, true);
if ( $message_arr['type'] == $this->push_process_str ) { // 当收到来自 push进程 的连接时,保持长连接
$this->uidConnections[ $this->push_process_str ] = $connection;
} else {
// 接收到数据,直接关闭
$connection->close();
$con = isset($this->uidConnections[ $this->push_process_str ]) ? $this->uidConnections[ $this->push_process_str ] : false;
if ($con) { // 连接上的客户端,才执行发送
// 每个数据结尾加上此特殊符号,用于分开数据
$message .= $this->special_mark;
$log = $this->push_process_str . '--- message: ' . $message;
var_dump('date: ' . date('Y-m-d H:i:s') . '---' . $log);
// 向 push 客户端发送数据
$con->send($message);
} else {
$log = $this->push_process_str . ' No connection established !';
var_dump('date: ' . date('Y-m-d H:i:s') . '---' . $log);
}
}
}
/**
* 当有客户端连接断开时
* @param $connection
* @throws Exception
*/
public function onClose($connection)
{
}
/**
* 当 worker 进程停止时
* @param $connection
*/
public function onWorkerStop($connection)
{
}
}
new Received();
问题:
他们两建立连接,并可以通信,启动几天或几个小时之后,Received 无法发送信息到 push 进程中去,windows 端口状态截图:
截图下无法发送时界面打印的日志
没记录,send 返回的结果, $log = $this->push_process_str . '--- message: ' . $message; 这一步是能正常执行的,我添加一下 log
task 进程发送代码:
$res = $con->send($message);
echo 'received send result = ' . $res;
打印日志:
received send result = 1
send 返回的结果是 true,现在换到 CentOS Linux release 7.5.1804 (Core) 系统中,还是会出现偶尔异步次进程发送成功,但主进程无法接收的情况,还额外添加了每隔 35秒 执行心跳,reload 一下又可以正常通信,一个月大概出现一次,打印的状态:
[root@VM-0-7-centos Workerman]# ./status.sh
Workerman[Received.php] status
----------------------------------------------GLOBAL STATUS----------------------------------------------------
Workerman version:4.0.19 PHP version:7.3.4
start time:2022-08-19 09:57:10 run 19 days 4 hours
load average: 1.16, 1, 1 event-loop:\Workerman\Events\React\StreamSelectLoop
1 workers 1 processes
worker_name exit_status exit_count
TaskWorker 0 0
----------------------------------------------PROCESS STATUS---------------------------------------------------
pid memory listening worker_name connections send_fail timers total_request qps status
1259 2M tcp://0.0.0.0:2000 TaskWorker 1 0 0 51853 0 [idle]
----------------------------------------------PROCESS STATUS---------------------------------------------------
Summary 2M - - 1 0 0 51853 0 [Summary]
Workerman[SendPush.php] status
----------------------------------------------GLOBAL STATUS----------------------------------------------------
Workerman version:4.0.19 PHP version:7.3.4
start time:2022-08-19 09:57:10 run 19 days 4 hours
load average: 1.16, 1, 1 event-loop:\Workerman\Events\React\StreamSelectLoop
1 workers 1 processes
worker_name exit_status exit_count
TaskWorker 0 0
----------------------------------------------PROCESS STATUS---------------------------------------------------
pid memory listening worker_name connections send_fail timers total_request qps status
1263 2M tcp://0.0.0.0:2001 TaskWorker 1 0 2 47094 0 [idle]
----------------------------------------------PROCESS STATUS---------------------------------------------------
Summary 2M - - 1 0 2 47094 0 [Summary]
Workerman[PushNew.php] status
----------------------------------------------GLOBAL STATUS----------------------------------------------------
Workerman version:4.0.19 PHP version:7.3.4
start time:2022-08-19 09:57:10 run 19 days 4 hours
load average: 1.16, 1, 1 event-loop:\Workerman\Events\React\StreamSelectLoop
1 workers 1 processes
worker_name exit_status exit_count
none 0 0
----------------------------------------------PROCESS STATUS---------------------------------------------------
pid memory listening worker_name connections send_fail timers total_request qps status
1267 4M tcp://0.0.0.0:997 none 46 0 2 261810 0 [idle]
----------------------------------------------PROCESS STATUS---------------------------------------------------
Summary 4M - - 46 0 2 261810 0 [Summary]
[root@VM-0-7-centos Workerman]# ./connections.sh
Workerman[Received.php] connections
--------------------------------------------------------------------- WORKERMAN CONNECTION STATUS --------------------------------------------------------------------------------
PID Worker CID Trans Protocol ipv4 ipv6 Recv-Q Send-Q Bytes-R Bytes-W Status Local Address Foreign Address
1259 TaskWorker 1 tcp tcp 1 0 0B 0B 1.1MB 2.6MB ESTABLISHED 127.0.0.1:2000 127.0.0.1:38027
Workerman[SendPush.php] connections
--------------------------------------------------------------------- WORKERMAN CONNECTION STATUS --------------------------------------------------------------------------------
PID Worker CID Trans Protocol ipv4 ipv6 Recv-Q Send-Q Bytes-R Bytes-W Status Local Address Foreign Address
1263 TaskWorker 1 tcp tcp 1 0 0B 0B 1.1MB 3.6MB ESTABLISHED 127.0.0.1:2001 127.0.0.1:55405
Workerman[PushNew.php] connections
--------------------------------------------------------------------- WORKERMAN CONNECTION STATUS --------------------------------------------------------------------------------
PID Worker CID Trans Protocol ipv4 ipv6 Recv-Q Send-Q Bytes-R Bytes-W Status Local Address Foreign Address
1267 none 1 tcp tcp 1 0 0B 0B 2.6MB 1.1MB ESTABLISHED 127.0.0.1:38027 127.0.0.1:2000
1267 none 2 tcp tcp 1 0 0B 0B 3.6MB 1.1MB ESTABLISHED 127.0.0.1:55405 127.0.0.1:2001
1267 none 34 tcp tcp 1 0 0B 0B 357.5KB 12.6KB ESTABLISHED 172.20.0.7:997 172.20.0.5:56911
1267 none 54 tcp tcp 1 0 0B 0B 528.7KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:57250
1267 none 55 tcp tcp 1 0 0B 0B 528.7KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:57258
1267 none 56 tcp tcp 1 0 0B 0B 529.1KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:57265
1267 none 57 tcp tcp 1 0 0B 0B 5.9MB 17.3KB ESTABLISHED 172.20.0.7:997 172.20.0.5:57270
1267 none 58 tcp tcp 1 0 0B 0B 528.7KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:57275
1267 none 59 tcp tcp 1 0 0B 0B 536.5KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:57279
1267 none 60 tcp tcp 1 0 0B 0B 528.7KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:57289
1267 none 61 tcp tcp 1 0 0B 0B 15.9MB 37.7KB ESTABLISHED 172.20.0.7:997 172.20.0.5:57291
1267 none 63 tcp tcp 1 0 0B 0B 576.9KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:57307
1267 none 66 tcp tcp 1 0 1.4KB 0B 11.9MB 31.9KB ESTABLISHED 172.20.0.7:997 172.20.0.5:57324
1267 none 76 tcp tcp 1 0 0B 0B 529.8KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:57381
1267 none 82 tcp tcp 1 0 0B 0B 2.3MB 67.7KB ESTABLISHED 172.20.0.7:997 172.20.0.5:57412
1267 none 83 tcp tcp 1 0 0B 0B 1.6MB 82KB ESTABLISHED 172.20.0.7:997 172.20.0.5:57417
1267 none 84 tcp tcp 1 0 0B 0B 3.3MB 63.3KB ESTABLISHED 172.20.0.7:997 172.20.0.5:57423
1267 none 98 tcp tcp 1 0 0B 0B 227MB 13.1KB ESTABLISHED 172.20.0.7:997 172.20.0.9:49709
1267 none 107 tcp tcp 1 0 0B 0B 3.2MB 121.4KB ESTABLISHED 172.20.0.7:997 172.20.0.5:62951
1267 none 112 tcp tcp 1 0 0B 0B 16MB 53.4KB ESTABLISHED 172.20.0.7:997 172.20.0.5:51941
1267 none 113 tcp tcp 1 0 0B 0B 6.9MB 38KB ESTABLISHED 172.20.0.7:997 172.20.0.5:52368
1267 none 122 tcp tcp 1 0 0B 0B 3.8MB 143.2KB ESTABLISHED 172.20.0.7:997 172.20.0.5:58738
1267 none 123 tcp tcp 1 0 0B 0B 4MB 174.8KB ESTABLISHED 172.20.0.7:997 172.20.0.5:58764
1267 none 124 tcp tcp 1 0 0B 0B 1.2MB 63.7KB ESTABLISHED 172.20.0.7:997 172.20.0.5:58783
1267 none 125 tcp tcp 1 0 0B 0B 342.2KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:58795
1267 none 126 tcp tcp 1 0 0B 0B 342.2KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:58815
1267 none 127 tcp tcp 1 0 0B 0B 342.2KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.5:58836
1267 none 143 tcp tcp 1 0 0B 0B 3.3MB 17.3KB ESTABLISHED 172.20.0.7:997 172.20.0.5:62871
1267 none 199 tcp tcp 1 0 0B 0B 149.5KB 4.4KB ESTABLISHED 172.20.0.7:997 172.20.0.9:62724
1267 none 201 tcp tcp 1 0 0B 0B 140.3KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.9:62737
1267 none 203 tcp tcp 1 0 0B 0B 167.3KB 9.7KB ESTABLISHED 172.20.0.7:997 172.20.0.9:62744
1267 none 207 tcp tcp 1 0 0B 0B 140.3KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.9:62754
1267 none 209 tcp tcp 1 0 0B 0B 141KB 504B ESTABLISHED 172.20.0.7:997 172.20.0.9:62768
1267 none 211 tcp tcp 1 0 0B 0B 151KB 8.2KB ESTABLISHED 172.20.0.7:997 172.20.0.9:62778
1267 none 212 tcp tcp 1 0 0B 0B 164KB 8.9KB ESTABLISHED 172.20.0.7:997 172.20.0.9:62780
1267 none 213 tcp tcp 1 0 0B 0B 169.2KB 11.2KB ESTABLISHED 172.20.0.7:997 172.20.0.9:62783
1267 none 214 tcp tcp 1 0 0B 0B 150.7KB 6.3KB ESTABLISHED 172.20.0.7:997 172.20.0.9:62787
1267 none 216 tcp tcp 1 0 0B 0B 139.8KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.9:62795
1267 none 217 tcp tcp 1 0 0B 0B 188KB 17.2KB ESTABLISHED 172.20.0.7:997 172.20.0.9:62799
1267 none 218 tcp tcp 1 0 0B 0B 155.3KB 4.1KB ESTABLISHED 172.20.0.7:997 172.20.0.9:62802
1267 none 219 tcp tcp 1 0 0B 0B 139.8KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.9:62807
1267 none 220 tcp tcp 1 0 0B 0B 139.8KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.9:62812
1267 none 221 tcp tcp 1 0 0B 0B 139.8KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.9:62817
1267 none 222 tcp tcp 1 0 0B 0B 139.8KB 0B ESTABLISHED 172.20.0.7:997 172.20.0.9:62822
1267 none 228 tcp tcp 1 0 0B 0B 77.6KB 5KB ESTABLISHED 172.20.0.7:997 172.20.0.9:54402
[root@VM-0-7-centos Workerman]# netstat -anp | grep 997
tcp 0 0 0.0.0.0:997 0.0.0.0: LISTEN 1266/WorkerMan: mas
tcp 4256 0 172.20.0.7:997 172.20.0.9:62724 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57275 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62783 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57265 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:62951 ESTABLISHED 1267/WorkerMan: wor
tcp 22571 0 172.20.0.7:997 172.20.0.5:57291 ESTABLISHED 1267/WorkerMan: wor
tcp 43509 0 172.20.0.7:997 172.20.0.5:58764 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62780 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62795 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62778 ESTABLISHED 1267/WorkerMan: wor
tcp 0 0 172.20.0.7:997 172.20.0.5:56911 ESTABLISHED 1267/WorkerMan: wor
tcp 1644291 0 172.20.0.7:997 172.20.0.9:49709 ESTABLISHED 1267/WorkerMan: wor
tcp 5300 0 172.20.0.7:997 172.20.0.9:62787 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62812 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62799 ESTABLISHED 1267/WorkerMan: wor
tcp 4312 0 172.20.0.7:997 172.20.0.9:62744 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57279 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57307 ESTABLISHED 1267/WorkerMan: wor
tcp 4496 0 172.20.0.7:997 172.20.0.5:52368 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57412 ESTABLISHED 1267/WorkerMan: wor
tcp 28431 0 172.20.0.7:997 172.20.0.5:58783 ESTABLISHED 1267/WorkerMan: wor
tcp 4312 0 172.20.0.7:997 172.20.0.5:57270 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57289 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57381 ESTABLISHED 1267/WorkerMan: wor
tcp 8829 0 172.20.0.7:997 172.20.0.5:51941 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62807 ESTABLISHED 1267/WorkerMan: wor
tcp 71125 0 172.20.0.7:997 172.20.0.5:58738 ESTABLISHED 1267/WorkerMan: wor
tcp 3934 0 172.20.0.7:997 172.20.0.5:62871 ESTABLISHED 1267/WorkerMan: wor
tcp 4604 0 172.20.0.7:997 172.20.0.9:62802 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:58836 ESTABLISHED 1267/WorkerMan: wor
tcp 4272 0 172.20.0.7:997 172.20.0.9:62737 ESTABLISHED 1267/WorkerMan: wor
tcp 4272 0 172.20.0.7:997 172.20.0.9:62754 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:58815 ESTABLISHED 1267/WorkerMan: wor
tcp 4077 0 172.20.0.7:997 172.20.0.5:57423 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:58795 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57250 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62817 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62768 ESTABLISHED 1267/WorkerMan: wor
tcp 4256 0 172.20.0.7:997 172.20.0.9:62822 ESTABLISHED 1267/WorkerMan: wor
tcp 3934 0 172.20.0.7:997 172.20.0.5:57324 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57258 ESTABLISHED 1267/WorkerMan: wor
tcp 3881 0 172.20.0.7:997 172.20.0.9:54402 ESTABLISHED 1267/WorkerMan: wor
tcp 3745 0 172.20.0.7:997 172.20.0.5:57417 ESTABLISHED 1267/WorkerMan: wor
[root@VM-0-7-centos Workerman]# netstat -anp | grep 2000
tcp 0 0 0.0.0.0:2000 0.0.0.0: LISTEN 1258/WorkerMan: mas
tcp 9631 0 127.0.0.1:38027 127.0.0.1:2000 ESTABLISHED 1267/WorkerMan: wor
tcp 0 0 127.0.0.1:2000 127.0.0.1:38027 ESTABLISHED 1259/WorkerMan: wor
[root@VM-0-7-centos Workerman]# netstat -anp | grep 2001
tcp 0 0 0.0.0.0:2001 0.0.0.0:* LISTEN 1262/WorkerMan: mas
tcp 0 0 127.0.0.1:55405 127.0.0.1:2001 ESTABLISHED 1267/WorkerMan: wor
tcp 0 0 127.0.0.1:2001 127.0.0.1:55405 ESTABLISHED 1263/WorkerMan: wor
大佬帮忙看看是为啥,感谢!!
大神,能回复一下吗,这个问题困扰我很久了,前面太忙了,没时间修复,现在又重现了,期待您的回复.