workbunny / webman-rabbitmq

🚀🐇 A PHP implementation of RabbitMQ Client for webman plugin.
https://www.workerman.net/plugin/67
MIT License
23 stars 4 forks source link

关于消费队列退出优雅关闭连接 #2

Closed jeyfang0110 closed 1 year ago

jeyfang0110 commented 1 year ago

在本地启了一个docker运行rabbitmq,通过实时跟踪日志查看到,当webman退出时,调用消费队列进程的onWorkerStop方法时,虽然FasterBuilder中针对连接处理$this->_connection->close();,但发现rabbitmq这边有warning日志:

2022-09-04 02:55:53.458 [warning] <0.20366.1> closing AMQP connection <0.20366.1> (172.19.0.1:60280 -> 172.19.0.2:5672, vhost: '/', user: 'rabbitmq'):
client unexpectedly closed TCP connection

同时发现如果在发布消息时,通过调用helpers的async_publish方法,设置close参数为true时,连接是正常关闭的:

2022-09-04 03:02:36.367 [info] <0.20824.1> connection <0.20824.1> (172.19.0.1:60316 -> 172.19.0.2:5672): user 'rabbitmq' authenticated and granted access to vhost '/'
******
2022-09-04 03:02:36.421 [info] <0.20824.1> closing AMQP connection <0.20824.1> (172.19.0.1:60316 -> 172.19.0.2:5672, vhost: '/', user: 'rabbitmq')
chaz6chez commented 1 year ago

在本地启了一个docker运行rabbitmq,通过实时跟踪日志查看到,当webman退出时,调用消费队列进程的onWorkerStop方法时,虽然FasterBuilder中针对连接处理$this->_connection->close();,但发现rabbitmq这边有warning日志:

2022-09-04 02:55:53.458 [warning] <0.20366.1> closing AMQP connection <0.20366.1> (172.19.0.1:60280 -> 172.19.0.2:5672, vhost: '/', user: 'rabbitmq'):
client unexpectedly closed TCP connection

这里是因为消费者的所有行为都是异步的,包括close;当消费者调用close后,该进程已经被workerman的主进程回收了,所以实际上并没有在eventloop中等待close真正的逻辑执行完,这个时候rabbitmq自然会有一条客户端closed的日志消息。针对这种情况实际上我们现在正在想办法优雅的解决。

chaz6chez commented 1 year ago

同时发现如果在发布消息时,通过调用helpers的async_publish方法,设置close参数为true时,连接是正常关闭的:

2022-09-04 03:02:36.367 [info] <0.20824.1> connection <0.20824.1> (172.19.0.1:60316 -> 172.19.0.2:5672): user 'rabbitmq' authenticated and granted access to vhost '/'
******
2022-09-04 03:02:36.421 [info] <0.20824.1> closing AMQP connection <0.20824.1> (172.19.0.1:60316 -> 172.19.0.2:5672, vhost: '/', user: 'rabbitmq')

因为在async_publish的时候,虽然是一条异步执行任务,但执行该异步任务的event-loop和进程都正常存活,所以任务会等待轮到它的event-loop周期后执行该close逻辑。

jeyfang0110 commented 1 year ago

同时发现如果在发布消息时,通过调用helpers的async_publish方法,设置close参数为true时,连接是正常关闭的:

2022-09-04 03:02:36.367 [info] <0.20824.1> connection <0.20824.1> (172.19.0.1:60316 -> 172.19.0.2:5672): user 'rabbitmq' authenticated and granted access to vhost '/'
******
2022-09-04 03:02:36.421 [info] <0.20824.1> closing AMQP connection <0.20824.1> (172.19.0.1:60316 -> 172.19.0.2:5672, vhost: '/', user: 'rabbitmq')

因为在async_publish的时候,虽然是一条异步执行任务,但执行该异步任务的event-loop和进程都正常存活,所以任务会等待轮到它的event-loop周期后执行该close逻辑。

明白了:+1: ,看来得研究下异步这一块