tkuhlengel commented on code in PR #1117:
URL: https://github.com/apache/guacamole-client/pull/1117#discussion_r2516558641
##########
guacamole/src/main/java/org/apache/guacamole/GuacamoleSession.java:
##########
@@ -284,6 +293,94 @@ public long getLastAccessedTime() {
return lastAccessedTime;
}
+ /**
+ * Returns the creation time of the specified tunnel, as the number of
+ * milliseconds since midnight January 1, 1970 GMT. Invoking this function
+ * does not affect the last access time of this session.
+ *
+ * @param uuid
+ * The UUID of the tunnel to get the creation time for.
+ *
+ * @return
+ * The time the tunnel was created, or 0 if the tunnel doesn't exist.
+ */
+ public long getTunnelCreationTime(String uuid) {
+ Long creationTime = tunnelCreationTimes.get(uuid);
+ return creationTime != null ? creationTime : 0;
+ }
+
+ /**
+ * Returns the age of the oldest tunnel in this session, in milliseconds.
+ * If no tunnels exist, returns 0. Invoking this function does not affect
+ * the last access time of this session.
+ *
+ * @return
+ * The age of the oldest tunnel in milliseconds, or 0 if no tunnels
exist.
+ */
+ public long getOldestTunnelAge() {
+ if (tunnelCreationTimes.isEmpty()) {
+ return 0;
+ }
+
+ long currentTime = System.currentTimeMillis();
+ long oldestCreationTime = tunnelCreationTimes.values().stream()
+ .mapToLong(Long::longValue)
+ .min()
+ .orElse(currentTime);
+
+ return currentTime - oldestCreationTime;
+ }
+
+ /**
+ * Closes and removes any tunnels in this session that exceed the specified
+ * age limit. Invoking this function does not affect the last access time
+ * of this session.
+ *
+ * @param maxAge
+ * The maximum allowed age of tunnels in milliseconds. Tunnels older
+ * than this will be closed and removed.
+ *
+ * @return
+ * The number of tunnels that were closed and removed.
+ */
+ public int closeExpiredTunnels(long maxAge) {
+ if (maxAge <= 0 || tunnelCreationTimes.isEmpty()) {
+ return 0;
+ }
+
+ long currentTime = System.currentTimeMillis();
+ int closedCount = 0;
+
+ // Find tunnels that exceed the age limit
+ List<String> expiredTunnelIds = new ArrayList<>();
+ for (Map.Entry<String, Long> entry : tunnelCreationTimes.entrySet()) {
+ long tunnelAge = currentTime - entry.getValue();
+ if (tunnelAge >= maxAge) {
+ expiredTunnelIds.add(entry.getKey());
+ }
+ }
+
+ // Close and remove expired tunnels
+ for (String tunnelId : expiredTunnelIds) {
+ UserTunnel tunnel = tunnels.get(tunnelId);
+ if (tunnel != null) {
+ try {
+ tunnel.close();
+ logger.debug("Closed tunnel \"{}\" due to connection
timeout.", tunnelId);
+ }
+ catch (GuacamoleException e) {
+ logger.debug("Unable to close expired tunnel \"" +
tunnelId + "\".", e);
+ }
+ // Remove from both maps regardless of whether close succeeded
+ tunnels.remove(tunnelId);
+ tunnelCreationTimes.remove(tunnelId);
+ closedCount++;
+ }
+ }
Review Comment:
I'm not the expert in this particular case.
My understanding of removeTunnel is that it doesn't close an active tunnel,
only removes it from the list of active tunnels. I think that's the equivalent
of disassociating it from one user?
I could use it on line `+375` in this chunk, but I'm not sure how much
difference it will make to make it smaller or meaningfully better other than
consistency. I still need to close the tunnel first.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]