Skip to content

Commit

Permalink
Do not recreate kConnection during validate to keep the session stats
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Dec 8, 2023
1 parent 8e8ea8d commit d210461
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/kx/KConnectionStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
11 changes: 6 additions & 5 deletions src/studio/kdb/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<EditorTab> editors = new ArrayList<>();

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit d210461

Please sign in to comment.