changeset 2724:7ddc0e91ad19

[commands] Explicitly set agent session idle timeout. Reviewed-by: jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/024246.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Fri, 21 Jul 2017 09:53:48 +0200
parents e109ac6f4bae
children 4f2764035d3b
files plugins/commands/agent/src/main/java/com/redhat/thermostat/commands/agent/internal/socket/CmdChannelAgentSocket.java plugins/commands/agent/src/test/java/com/redhat/thermostat/commands/agent/internal/socket/CmdChannelAgentSocketTest.java
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/commands/agent/src/main/java/com/redhat/thermostat/commands/agent/internal/socket/CmdChannelAgentSocket.java	Fri Jul 14 18:33:40 2017 +0200
+++ b/plugins/commands/agent/src/main/java/com/redhat/thermostat/commands/agent/internal/socket/CmdChannelAgentSocket.java	Fri Jul 21 09:53:48 2017 +0200
@@ -39,6 +39,7 @@
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -65,6 +66,7 @@
 @WebSocket
 public class CmdChannelAgentSocket {
 
+    private static final long SOCKET_SESSION_IDLE_TIMEOUT = TimeUnit.MINUTES.toMillis(10);
     private static final Logger logger = LoggingUtils
             .getLogger(CmdChannelAgentSocket.class);
     private final GsonFacade gson;
@@ -134,6 +136,13 @@
     @OnWebSocketConnect
     public void onConnect(Session session) {
         this.session = session;
+        // Be sure to have the socket timeout the same as on the
+        // microservice endpoint. Otherwise the agent socket might
+        // time out where it should not since the microservice will
+        // send a ping periodically which is strictly less than
+        // the configured idle timeout.
+        this.session.setIdleTimeout(SOCKET_SESSION_IDLE_TIMEOUT);
+        logger.config("Socket session idle timeout: " + session.getIdleTimeout() + "ms");
         this.connectLatch.countDown();
     }
 
--- a/plugins/commands/agent/src/test/java/com/redhat/thermostat/commands/agent/internal/socket/CmdChannelAgentSocketTest.java	Fri Jul 14 18:33:40 2017 +0200
+++ b/plugins/commands/agent/src/test/java/com/redhat/thermostat/commands/agent/internal/socket/CmdChannelAgentSocketTest.java	Fri Jul 21 09:53:48 2017 +0200
@@ -125,4 +125,19 @@
         socket.closeSession();
         verify(session).close();
     }
+    
+    /**
+     * The commands microservice sets the idle timeout to 10 minutes as well. This
+     * is to ensure the timeouts match up. Otherwise there is a risk of timing out
+     * before the microservice can send a ping for this to NOT happen.
+     */
+    @Test
+    public void verifySetsSocketSessionIdleTimeout() {
+        long expectedSessionTimeout = 600_000;
+        CountDownLatch connectLatch = new CountDownLatch(1);
+        Session session = mock(Session.class);
+        CmdChannelAgentSocket socket = new CmdChannelAgentSocket(null, connectLatch, "foo-agent");
+        socket.onConnect(session);
+        verify(session).setIdleTimeout(expectedSessionTimeout);
+    }
 }