From 8aa2bc76bdf517d69242311f5783ff4dd041cce6 Mon Sep 17 00:00:00 2001 From: svencc Date: Sun, 1 Dec 2024 12:06:30 +0100 Subject: [PATCH] add configurable Heartbeat alive timeout --- .../com/recom/RecomBackendApplication.java | 4 +++- .../com/recom/api/HeartbeatController.java | 1 + .../RECOMServiceHeartbeatProperties.java | 20 +++++++++++++++++++ .../recom/service/AliveClientsService.java | 8 ++++++-- .../src/main/resources/application.properties | 4 +++- 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 services/recom-backend/src/main/java/com/recom/property/RECOMServiceHeartbeatProperties.java diff --git a/services/recom-backend/src/main/java/com/recom/RecomBackendApplication.java b/services/recom-backend/src/main/java/com/recom/RecomBackendApplication.java index eec61915..1cce55f7 100644 --- a/services/recom-backend/src/main/java/com/recom/RecomBackendApplication.java +++ b/services/recom-backend/src/main/java/com/recom/RecomBackendApplication.java @@ -1,6 +1,7 @@ package com.recom; import com.recom.property.RECOMAsyncProperties; +import com.recom.property.RECOMServiceHeartbeatProperties; import com.recom.property.RECOMSecurityProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -18,7 +19,8 @@ @SpringBootApplication(scanBasePackages = {"com.recom"}) @EnableConfigurationProperties({ RECOMSecurityProperties.class, - RECOMAsyncProperties.class + RECOMAsyncProperties.class, + RECOMServiceHeartbeatProperties.class }) public class RecomBackendApplication { diff --git a/services/recom-backend/src/main/java/com/recom/api/HeartbeatController.java b/services/recom-backend/src/main/java/com/recom/api/HeartbeatController.java index a21a30b2..734be2b3 100644 --- a/services/recom-backend/src/main/java/com/recom/api/HeartbeatController.java +++ b/services/recom-backend/src/main/java/com/recom/api/HeartbeatController.java @@ -33,6 +33,7 @@ public class HeartbeatController { @NonNull private final AliveClientsService aliveClientsService; + @Operation( summary = "Heartbeat", description = "Sends an is-alive message." diff --git a/services/recom-backend/src/main/java/com/recom/property/RECOMServiceHeartbeatProperties.java b/services/recom-backend/src/main/java/com/recom/property/RECOMServiceHeartbeatProperties.java new file mode 100644 index 00000000..f4b3ba57 --- /dev/null +++ b/services/recom-backend/src/main/java/com/recom/property/RECOMServiceHeartbeatProperties.java @@ -0,0 +1,20 @@ +package com.recom.property; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.time.Duration; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@ConfigurationProperties(prefix = "recom.service.heartbeat") +public class RECOMServiceHeartbeatProperties { + + private Duration aliveTimeout; + +} diff --git a/services/recom-backend/src/main/java/com/recom/service/AliveClientsService.java b/services/recom-backend/src/main/java/com/recom/service/AliveClientsService.java index a670ee11..b306f2de 100644 --- a/services/recom-backend/src/main/java/com/recom/service/AliveClientsService.java +++ b/services/recom-backend/src/main/java/com/recom/service/AliveClientsService.java @@ -1,19 +1,23 @@ package com.recom.service; import com.recom.model.RECOMClient; +import com.recom.property.RECOMServiceHeartbeatProperties; import com.recom.security.account.RECOMAccount; import lombok.Locked; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.*; @Service +@RequiredArgsConstructor public class AliveClientsService { @NonNull - private final Long HEARTBEAT_TIMEOUT = 10L; + private final RECOMServiceHeartbeatProperties heartbeatProperties; + @NonNull private Map knownClients = new HashMap<>(); @@ -31,7 +35,7 @@ public void trackHeartbeat(@NonNull final RECOMAccount account) { @NonNull public List getAliveClients() { List aliveClients = knownClients.values().stream() - .filter(client -> client.getLastHeartbeat().isAfter(LocalDateTime.now().minusSeconds(HEARTBEAT_TIMEOUT))) + .filter(client -> client.getLastHeartbeat().isAfter(LocalDateTime.now().minusSeconds(heartbeatProperties.getAliveTimeout().toSeconds()))) .toList(); return List.copyOf(aliveClients); diff --git a/services/recom-backend/src/main/resources/application.properties b/services/recom-backend/src/main/resources/application.properties index 61fa2245..4d9e3a45 100644 --- a/services/recom-backend/src/main/resources/application.properties +++ b/services/recom-backend/src/main/resources/application.properties @@ -80,4 +80,6 @@ recom.security.jwt-expiration-time=60s recom.security.key-path=${recom.data-base-dir}/RECOMKey recom.async.core-pool-size=1 -recom.async.max-pool-size=10 \ No newline at end of file +recom.async.max-pool-size=10 + +recom.service.heartbeat.alive-timeout=7s \ No newline at end of file