Skip to content

Commit e953151

Browse files
committed
Honour consoleproxy.session.timeout for noVNC sessions
1 parent 45c3bbe commit e953151

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxy.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public class ConsoleProxy {
8080
static String factoryClzName;
8181
static boolean standaloneStart = false;
8282

83+
// New: session timeout in milliseconds, default 300000 (5 minutes)
84+
static int sessionTimeoutMillis = 300000;
85+
8386
static String encryptorPassword = "Dummy";
8487
static final String[] skipProperties = new String[]{"certificate", "cacertificate", "keystore_password", "privatekey"};
8588

@@ -165,6 +168,18 @@ private static void configProxy(Properties conf) {
165168
defaultBufferSize = Integer.parseInt(s);
166169
LOGGER.info("Setting defaultBufferSize=" + defaultBufferSize);
167170
}
171+
172+
// New: read consoleproxy.session.timeout (milliseconds)
173+
s = conf.getProperty("consoleproxy.session.timeout");
174+
if (s != null) {
175+
try {
176+
sessionTimeoutMillis = Integer.parseInt(s);
177+
LOGGER.info("Setting consoleproxy.session.timeout=" + sessionTimeoutMillis);
178+
} catch (NumberFormatException e) {
179+
LOGGER.warn("Invalid value for consoleproxy.session.timeout: " + s +
180+
", using default " + sessionTimeoutMillis, e);
181+
}
182+
}
168183
}
169184

170185
public static ConsoleProxyServerFactory getHttpServerFactory() {
@@ -379,7 +394,7 @@ public static void start(Properties conf) {
379394
LOGGER.info("HTTP command port is disabled");
380395
}
381396

382-
ConsoleProxyGCThread cthread = new ConsoleProxyGCThread(connectionMap, removedSessionsSet);
397+
ConsoleProxyGCThread cthread = new ConsoleProxyGCThread(connectionMap, removedSessionsSet /*, sessionTimeoutMillis */);
383398
cthread.setName("Console Proxy GC Thread");
384399
cthread.start();
385400
}

services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyGCThread.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public class ConsoleProxyGCThread extends Thread {
3535
protected Logger logger = LogManager.getLogger(ConsoleProxyGCThread.class);
3636

37-
private final static int MAX_SESSION_IDLE_SECONDS = 180;
37+
private final static int DEFAULT_MAX_SESSION_IDLE_SECONDS = 180;
3838

3939
private final Map<String, ConsoleProxyClient> connMap;
4040
private final Set<String> removedSessionsSet;
@@ -45,6 +45,13 @@ public ConsoleProxyGCThread(Map<String, ConsoleProxyClient> connMap, Set<String>
4545
this.removedSessionsSet = removedSet;
4646
}
4747

48+
private int getMaxSessionIdleSeconds() {
49+
if (ConsoleProxy.sessionTimeoutMillis <= 0) {
50+
return DEFAULT_MAX_SESSION_IDLE_SECONDS;
51+
}
52+
return ConsoleProxy.sessionTimeoutMillis / 1000;
53+
}
54+
4855
private void cleanupLogging() {
4956
if (lastLogScan != 0 && System.currentTimeMillis() - lastLogScan < 3600000)
5057
return;
@@ -92,7 +99,7 @@ public void run() {
9299
}
93100

94101
long seconds_unused = (System.currentTimeMillis() - client.getClientLastFrontEndActivityTime()) / 1000;
95-
if (seconds_unused < MAX_SESSION_IDLE_SECONDS) {
102+
if (seconds_unused < getMaxSessionIdleSeconds()) {
96103
continue;
97104
}
98105

services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyNoVNCHandler.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ public void onConnect(final Session session) throws IOException, InterruptedExce
125125
}
126126

127127
try {
128+
session.setIdleTimeout(ConsoleProxy.sessionTimeoutMillis);
129+
logger.debug("Set noVNC WebSocket idle timeout to {} ms for session UUID: {}.",
130+
ConsoleProxy.sessionTimeoutMillis, sessionUuid);
131+
128132
ConsoleProxyClientParam param = new ConsoleProxyClientParam();
129133
param.setClientHostAddress(host);
130134
param.setClientHostPort(port);
@@ -185,12 +189,21 @@ public void onClose(Session session, int statusCode, String reason) throws IOExc
185189

186190
@OnWebSocketFrame
187191
public void onFrame(Frame f) throws IOException {
192+
if (viewer == null) {
193+
logger.warn("Ignoring WebSocket frame because viewer is not initialized yet.");
194+
return;
195+
}
188196
logger.trace("Sending client [ID: {}] frame of {} bytes.", viewer.getClientId(), f.getPayloadLength());
189197
viewer.sendClientFrame(f);
190198
}
191199

192200
@OnWebSocketError
193201
public void onError(Throwable cause) {
194-
logger.error("Error on WebSocket [client ID: {}, session UUID: {}].", cause, viewer.getClientId(), viewer.getSessionUuid());
202+
if (viewer != null) {
203+
logger.error("Error on WebSocket [client ID: {}, session UUID: {}].",
204+
viewer.getClientId(), viewer.getSessionUuid(), cause);
205+
} else {
206+
logger.error("Error on WebSocket before viewer initialization.", cause);
207+
}
195208
}
196209
}

0 commit comments

Comments
 (0)