Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Enable idle timeout for websocket connections (#1535)

Rich Turner3b888a96fc14
diff --git a/container/src/main/java/org/openremote/container/web/WebService.java b/container/src/main/java/org/openremote/container/web/WebService.java
index 1aae50b87..0858306b3 100644
--- a/container/src/main/java/org/openremote/container/web/WebService.java
+++ b/container/src/main/java/org/openremote/container/web/WebService.java
@@ -31,6 +31,7 @@ import io.undertow.servlet.api.DeploymentManager;
import io.undertow.servlet.api.FilterInfo;
import io.undertow.servlet.util.ImmediateInstanceHandle;
import io.undertow.util.HeaderMap;
+import io.undertow.websockets.core.WebSocketChannel;
import jakarta.servlet.DispatcherType;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.UriBuilder;
@@ -143,6 +144,10 @@ public abstract class WebService implements ContainerService {
.setWorkerOption(Options.WORKER_NAME, "WebService")
.setWorkerOption(Options.THREAD_DAEMON, true)
).build();
+
+ // We have to set system properties for websocket timeouts
+ System.setProperty(WebSocketChannel.WEB_SOCKETS_READ_TIMEOUT, "30000");
+ System.setProperty(WebSocketChannel.WEB_SOCKETS_WRITE_TIMEOUT, "30000");
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
index 2cbea5f93..d8c9032a8 100644
--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
@@ -104,7 +104,7 @@ import static org.openremote.model.Constants.*;
*/
public class ClientEventService extends RouteBuilder implements ContainerService {
public static final int PRIORITY = ManagerWebService.PRIORITY - 200;
- public static final String WEBSOCKET_URI = "undertow://ws://0.0.0.0/websocket/events?fireWebSocketChannelEvents=true&sendTimeout=15000"; // Host is not used as existing undertow instance is utilised
+ public static final String WEBSOCKET_URI = "undertow://ws://0.0.0.0/websocket/events?fireWebSocketChannelEvents=true&sendTimeout=15000&keepAlive=false"; // Host is not used as existing undertow instance is utilised
protected static final System.Logger LOG = System.getLogger(ClientEventService.class.getName());
protected static final String PUBLISH_QUEUE = "direct://ClientPublishQueue";
@@ -194,6 +194,9 @@ public class ClientEventService extends RouteBuilder implements ContainerService
LOG.log(INFO, "Unsupported user principal type: " + principal);
}
+ // Set an idle timeout value
+ webSocketChannel.setIdleTimeout(30000);
+
// Push auth and realm into channel for future use
webSocketChannel.setAttribute(Constants.AUTH_CONTEXT, authContext);
webSocketChannel.setAttribute(Constants.REALM_PARAM_NAME, realm);
diff --git a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy
index 160c1f303..92df8ab65 100644
--- a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy
@@ -1,6 +1,11 @@
package org.openremote.test.gateway
+import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3AsyncClientView
+import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3ClientConfigView
+import com.hivemq.client.mqtt.MqttClientConfig
+import com.hivemq.client.mqtt.MqttClientConnectionConfig
import io.netty.channel.ChannelHandler
+import io.netty.channel.socket.SocketChannel
import org.apache.http.client.utils.URIBuilder
import org.openremote.agent.protocol.http.HTTPAgent
import org.openremote.agent.protocol.http.HTTPAgentLink
@@ -729,7 +734,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
def "Verify gateway client service"() {
given: "the container environment is started"
- def conditions = new PollingConditions(timeout: 15, delay: 0.2)
+ def conditions = new PollingConditions(timeout: 30, delay: 0.2)
def delayedConditions = new PollingConditions(initialDelay: 1, delay: 1, timeout: 10)
def container = startContainer(defaultConfig(), defaultServices())
def timerService = container.getService(TimerService.class)
@@ -807,6 +812,26 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
assert gatewayAssets.size() == cityAssets.size()
}
+ when: "the gateway client is abruptly disconnected"
+ // Overwrite the client connection status to prevent reconnection
+ gatewayClientService.clientRealmMap.get(managerTestSetup.realmCityName).connectionStatus = ConnectionStatus.DISCONNECTED
+ gatewayClientService.clientRealmMap.get(managerTestSetup.realmCityName).channel.close()
+
+ then: "the gateway asset connection status should become DISCONNECTED"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId())
+ assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.DISCONNECTED
+ }
+
+ when: "the connection is reestablished"
+ gatewayClientService.clientRealmMap.get(managerTestSetup.realmCityName).connect()
+
+ then: "the gateway asset connection status should become CONNECTED"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId())
+ assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.CONNECTED
+ }
+
when: "a gateway client asset is modified"
MicrophoneAsset microphone1 = assetStorageService.find(managerTestSetup.microphone1Id)
microphone1.setName("Microphone 1 Updated")