Skip to content

Commit

Permalink
fix: ✏️ Heartbeat negotiation interceptor (#183)
Browse files Browse the repository at this point in the history
* fix: add system heartbeat interval env

* fix: server - mq heartbeath interver 25s to 20s

* chore: rabbitmq requested-heartbeat set 20 sec

* feat: create heartbeat negotiation interceptor

* fix: modify negotiation rule when client send 0,0 or null heartbeat header

* style: 협상 결과 log 위치 수정
  • Loading branch information
psychology50 authored Oct 25, 2024
1 parent 149756f commit 7f74e8c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class RabbitMqProperties {
private final String username;
private final String password;
private final String virtualHost;
private final int requestedHeartbeat;

@Override
public String toString() {
Expand All @@ -22,6 +23,7 @@ public String toString() {
", username='" + username + '\'' +
", password='" + password + '\'' +
", virtualHost='" + virtualHost + '\'' +
", requestedHeartbeat=" + requestedHeartbeat +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public ConnectionFactory createConnectionFactory() {
factory.setPassword(rabbitMqProperties.getPassword());
factory.setPort(rabbitMqProperties.getPort());
factory.setVirtualHost(rabbitMqProperties.getVirtualHost());
factory.setRequestedHeartBeat(rabbitMqProperties.getRequestedHeartbeat());

return factory;
}
Expand Down
1 change: 1 addition & 0 deletions pennyway-infra/src/main/resources/application-infra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ spring:
username: ${RABBITMQ_USERNAME:guest}
password: ${RABBITMQ_PASSWORD:guest}
virtual-host: ${RABBITMQ_VIRTUAL_HOST:/}
requested-heartbeat: ${RABBITMQ_REQUESTED_HEARTBEAT:20}

app:
question-address: ${ADMIN_ADDRESS:[email protected]}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package kr.co.pennyway.socket.common.interceptor.handler.inbound;

import kr.co.pennyway.socket.common.interceptor.marker.ConnectCommandHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class HeartBeatNegotiationInterceptor implements ConnectCommandHandler {
private static final String HEART_BEAT_HEADER = "heart-beat";

private static final long SERVER_HEARTBEAT_SEND = 25000; // sx
private static final long SERVER_HEARTBEAT_RECEIVE = 25000; // sy

@Override
public boolean isSupport(StompCommand command) {
return StompCommand.CONNECT.equals(command);
}

@Override
public void handle(Message<?> message, StompHeaderAccessor accessor) {
String heartbeat = accessor.getFirstNativeHeader(HEART_BEAT_HEADER);

long clientToServer = SERVER_HEARTBEAT_RECEIVE;
long serverToClient = SERVER_HEARTBEAT_SEND;

if (heartbeat == null || heartbeat.equals("0,0")) {
log.debug("Client attempted connection without heart-beat. Enforcing server's heart-beat policy: {},{}",
SERVER_HEARTBEAT_SEND, SERVER_HEARTBEAT_RECEIVE);
}

if (heartbeat != null) {
String[] parts = heartbeat.split(",");

if (parts.length == 2) {
long cx = Long.parseLong(parts[0]);
long cy = Long.parseLong(parts[1]);

clientToServer = (cx != 0) ? Math.max(cx, SERVER_HEARTBEAT_RECEIVE) : SERVER_HEARTBEAT_RECEIVE;
serverToClient = (cy != 0) ? Math.max(SERVER_HEARTBEAT_SEND, cy) : SERVER_HEARTBEAT_SEND;

log.debug("Heart-beat negotiation - Client wants: {}, Server wants: {}",
heartbeat, SERVER_HEARTBEAT_SEND + "," + SERVER_HEARTBEAT_RECEIVE);
}
}

log.info("Negotiated heart-beat - Client to Server: {}, Server to Client: {}",
clientToServer, serverToClient);

accessor.setNativeHeader(HEART_BEAT_HEADER, clientToServer + "," + serverToClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class MessageBrokerProperties {
private final String clientPassword;
private final String userPrefix;
private final String publishExchange;
private final int heartbeatSendInterval;
private final int heartbeatReceiveInterval;

@Override
public String toString() {
Expand All @@ -28,6 +30,8 @@ public String toString() {
", clientPassword='" + clientPassword + '\'' +
", userPrefix='" + userPrefix + '\'' +
", publishExchange='" + publishExchange + '\'' +
", heartbeatSendInterval=" + heartbeatSendInterval +
", heartbeatReceiveInterval=" + heartbeatReceiveInterval +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public void configureMessageBroker(MessageBrokerRegistry config) {
.setClientLogin(messageBrokerProperties.getClientId())
.setClientPasscode(messageBrokerProperties.getClientPassword())
.setRelayHost(messageBrokerProperties.getHost())
.setRelayPort(messageBrokerProperties.getPort());
.setRelayPort(messageBrokerProperties.getPort())
.setSystemHeartbeatSendInterval(messageBrokerProperties.getHeartbeatSendInterval())
.setSystemHeartbeatReceiveInterval(messageBrokerProperties.getHeartbeatReceiveInterval());

config.setUserDestinationPrefix(messageBrokerProperties.getUserPrefix());
config.setPathMatcher(new AntPathMatcher("."));
Expand Down
2 changes: 2 additions & 0 deletions pennyway-socket/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ message-broker:
client-password: ${MESSAGE_BROKER_CLIENT_PASSWORD:guest}
user-prefix: ${MESSAGE_BROKER_USER_PREFIX:/usr}
publish-exchange: ${MESSAGE_BROKER_PUBLISH_EXCHANGE:/topic}
heartbeat-send-interval: ${MESSAGE_BROKER_HEARTBEAT_SEND_INTERVAL:20000}
heartbeat-receive-interval: ${MESSAGE_BROKER_HEARTBEAT_RECEIVE_INTERVAL:20000}

jwt:
secret-key:
Expand Down

0 comments on commit 7f74e8c

Please sign in to comment.