diff --git a/src/kx/KConnectionStats.java b/src/kx/KConnectionStats.java index 5a7619f7..d5226cf9 100644 --- a/src/kx/KConnectionStats.java +++ b/src/kx/KConnectionStats.java @@ -60,6 +60,10 @@ public synchronized void receivedBytes(long count) { lastBytesReceived = new K.KLong(count); } + public synchronized K.KTimestamp getLastConnectedTime() { + return lastConnectedTime; + } + public static String[] getStatsHeaders() { Pair[] pairs = new KConnectionStats().pairs(); String[] headers = new String[pairs.length]; diff --git a/src/studio/kdb/Session.java b/src/studio/kdb/Session.java index fa09c2f4..77144ddc 100644 --- a/src/studio/kdb/Session.java +++ b/src/studio/kdb/Session.java @@ -16,10 +16,9 @@ public class Session implements ConnectionStateListener, KAuthentication { private KConnection kConn; - private long created; private final Server server; - private static final long HOUR = 1000*3600; + private static final long HOUR_NS = 1_000_000_000L*3600; private static SessionCreator sessionCreator = new SessionCreator(); private final List editors = new ArrayList<>(); @@ -66,7 +65,6 @@ private Session(Server server) { private void init() { kConn = sessionCreator.createConnection(server, this); if (kConn == null) throw new RuntimeException("Failure in the authentication plugin"); - created = System.currentTimeMillis(); kConn.setConnectionStateListener(this); } @@ -99,10 +97,13 @@ public void validate() { if (!Config.getInstance().getBoolean(Config.SESSION_INVALIDATION_ENABLED)) return; int hours = Config.getInstance().getInt(Config.SESSION_INVALIDATION_TIMEOUT_IN_HOURS); - if (created + hours * HOUR < System.currentTimeMillis()) { + + K.KTimestamp created = kConn.getStats().getLastConnectedTime(); + if (created.isNull()) return; + K.KTimespan duration = K.KTimespan.period(created, K.KTimestamp.now()); + if (duration.toLong() > hours * HOUR_NS) { log.info("Closing session to stale server: " + server.getDescription(true)); kConn.close(); - init(); } }