且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用长轮询检索实时通知时,是否有可能丢失事件?

更新时间:2022-04-12 09:52:24

握手时收到的客户端ID连接到服务器端的队列.该队列保留下一次连接之前您无法接收的所有通知.当您重新连接时,它会传送它们. (尝试使用Postman:在连接返回后,发送几个事件,然后再次连接.您会注意到您将立即获得所有事件.)

The client ID that you receive when handshaking is connected to a queue on the server side. That queue keeps all notifications that you are not able to receive until the next connect. It delivers them when you reconnect. (Try it out with Postman: After a connect returns, send a couple of events, then connect again. You will notice that you will get all events at once.)

但是,正如您所注意到的,队列不会永远保留.如果您不能在两个小时内重新连接(我相信),则该队列将被丢弃,以免阻塞服务器资源.这就是您所注意到的.在这种情况下,您需要查询数据库以确定任何遗漏的事件(例如,轮询设备中处于待处理状态的所有操作).

However, as you noticed, the queue is not kept forever. If you are not able to reconnect within two hours (I believe), the queue is thrown away in order to not block server resources. This is what you noticed. In that case, you need to query the database to determine any missed events (e.g., poll any operations in pending state from devices).