spring-framework: StompBrokerRelay doesn't send heartbeats to broker when @MessageMapping handles received messages
I discovered that StompBrokerRelayMessageHandler just forwards all messages from webscoket connection. This includes heartbeats from STOMP (over webscoket) clients (including WebSocketStompClient).
This works fine. But there is one caveat here. Not all STOMP messages from webscoket are handled by StompBrokerRelayMessageHandler. Some of them handled by SimpAnnotationMethodMessageHandler because their destination has prefix equal to prefix, that is set in MessageBrokerRegistry.setApplicationDestinationPrefixes.
For example:
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableStompBrokerRelay("/topic", "/queue");
}
If WebSocketStompClient just sends a message every 1 second to /app/greeting destination, then this message is handled by controller @MessageMapping("/greeting") and if that controler doesn’t forward it to /topic/greeting in the same tcp-session (with RabbitMQ broker) (i.e. returning a value from controller’s method), then RabbitMQ never receives that message.
It is common situation, I think. Not all application controllers should send something into broker. Especially into the same tcp connection that associated with websocket, from which message received. For example, I want to send message from my controller, but I want to send it through _system_ connection (not through connection, associated with webscoket, from which the message come) to the broker using SimpMessagingTemplate.
And the problem here is that WebSocketStompClient thinks it sends some bytes (it actually does), so it’s org.springframework.messaging.simp.stomp.DefaultStompSession.WriteInactivityTask is never called.
But at the same time these bytes are never delivered to RabbitMQ broker, because they are handled by @MessageMapping controller. So the RabbitMQ doesn’t receive any bytes in the heartbeat interval and closes the connection.
[warning] <0.20898.54> STOMP detected missed client heartbeat(s) on connection 192.168.250.203:41574 -> 172.17.0.2:61613, closing it
I think there are two solutions here:
-
Just always send heartbeats from
WebSocketStompClienton specified interval, without relying on whether some bytes were send recently or not.As I see, stompjs does exactly that. It is not exactly how heartbeats should work in STOMP protocol as described here, but it is more defensive (and produces more traffic of course).
-
Do not forward heartbeats from
WebSocketStompClient(and any other connected websocket STOMP client), but do heartbeating with broker internally instead, like it does for_system_connection. At the same time, do not forward heartbeats from broker to the webscoket, and instead do heartbeating with websocket internally. So, there will be two independent sets of heartbeat senders/chekers: one to broker side, and one to websocket side. So, even if the client will send only application messages (and will not send heartbeats because of that), the broker will still receive heartbeats from internal scheduler and will not close the connection.This soulution is more server resource intensive I think. The relay will have to do some scheduling instead of just relaying heartbeats back and forward, but it looks like a more “right” solution, that doesn’t violate STOMP protocol (in the sense of not sending heartbeats to the broker in specified interval because of
@MessageMapping).While I think this solution is more “right”, I’m worried about performance here. For example, to service 10k webscoket connections, it should be 10k
onWriteInactivityand 10konReadInactivitychecks on both sides , total 40k.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 18 (7 by maintainers)
@spring-issuemaster @xak2000 IMHO this is not an enhancement but a critical bug. I run into it recently and I ended up on this issue while searching. The WebSocketClient sends heartbeats when idle but when there are messages exchanged i.e. actual business functionality is taking place, then we get disconnections! As a temporary workaround in my project we always send a dummy empty message to a “/topic/dummy” Topic from the WebSocketClient, each time we get an actual (business) message from the server.
I think there is a reasonably straight-forward solution to this. As mentioned earlier
StompBrokerRelayMessageHandlerandSimpleBrokerMessageHandlerdo see all messages but ignore some by the destination and we can take actions for such ignored messages too. In factSimpleBrokerMessageHandleralready calls updateSessionReadTime before it ignores any message which means it does not have this issue. ForStompBrokerRelayMessageHandlerwe can maintain a similar per-session timestamp and the timestamp indicates we should send something to the broker we can send a heartbeat if we receive a message that is ignored.spring-projects/spring-framework]
发自我的 iPhone
在 2019年6月29日,09:13,Ruslan Stelmachenko <notifications@github.commailto:notifications@github.com> 写道:
The problem with second option (fixing on WebSocketStompClient side) is that some other stomp client can still encounter problems working with Spring Stomp Relay support and that will be not client’s fault, because that client will not violate STOMP protocol (it sends messages all the time, it just doesn’t know that Spring doesn’t resend some of them to the Broker).
I not checked all possible stomp clients and can’t say how many of them working like this, but here is what I checked:
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/spring-projects/spring-framework/issues/22822?email_source=notifications&email_token=AJ4OKJNSYVFLJUB3MC6C3C3P42ZMFA5CNFSM4HHJ6DOKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODY3OPZY#issuecomment-506914791, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJ4OKJJALQMTTOGUJZGVLLTP42ZMFANCNFSM4HHJ6DOA.