GUACAMOLE-1723: Simplify active window check; do not refresh from DB during active sessions.

This commit is contained in:
James Muehlner
2022-12-02 04:35:56 +00:00
parent 18d971a837
commit 89945caa5f
22 changed files with 123 additions and 432 deletions

View File

@@ -132,6 +132,22 @@ public class GuacamoleSession {
return Collections.unmodifiableList(userContexts);
}
/**
* Returns true if all user contexts associated with this session are
* valid, or false if any user context is not valid. If a session is not
* valid, it may no longer be used, and invalidate() should be invoked.
*
* @return
* true if all user contexts associated with this session are
* valid, or false if any user context is not valid.
*/
public boolean isValid() {
// Immediately return false if any user context is not valid
return !userContexts.stream().anyMatch(
userContext -> !userContext.isValid());
}
/**
* Returns the UserContext associated with this session that originated
* from the AuthenticationProvider with the given identifier. If no such

View File

@@ -94,7 +94,8 @@ public class HashTokenSessionMap implements TokenSessionMap {
/**
* Task which iterates through all active sessions, evicting those sessions
* which are beyond the session timeout.
* which are beyond the session timeout, or are marked as invalid by an
* extension.
*/
private class SessionEvictionTask implements Runnable {
@@ -105,7 +106,8 @@ public class HashTokenSessionMap implements TokenSessionMap {
/**
* Creates a new task which automatically evicts sessions which are
* older than the specified timeout.
* older than the specified timeout, or are marked as invalid by an
* extension.
*
* @param sessionTimeout The maximum age of any session, in
* milliseconds.
@@ -116,16 +118,16 @@ public class HashTokenSessionMap implements TokenSessionMap {
/**
* Iterates through all active sessions, evicting those sessions which
* are beyond the session timeout. Internal errors which would
* otherwise stop the session eviction process are caught, logged, and
* the process is allowed to proceed.
* are beyond the session timeout, or are marked as invalid. Internal
* errors which would otherwise stop the session eviction process are
* caught, logged, and the process is allowed to proceed.
*/
private void evictExpiredSessions() {
private void evictExpiredOrInvalidSessions() {
// Get start time of session check time
long sessionCheckStart = System.currentTimeMillis();
logger.debug("Checking for expired sessions...");
logger.debug("Checking for expired or invalid sessions...");
// For each session, remove sesions which have expired
Iterator<Map.Entry<String, GuacamoleSession>> entries = sessionMap.entrySet().iterator();
@@ -136,6 +138,15 @@ public class HashTokenSessionMap implements TokenSessionMap {
try {
// Invalidate any sessions which have been flagged as invalid by extensions
if (!session.isValid()) {
logger.debug(
"Session \"{}\" has been invalidated by an extension.",
entry.getKey());
entries.remove();
session.invalidate();
}
// Do not expire sessions which are active
if (session.hasTunnels())
continue;
@@ -170,13 +181,13 @@ public class HashTokenSessionMap implements TokenSessionMap {
@Override
public void run() {
// The evictExpiredSessions() function should already
// The evictExpiredOrInvalidSessions() function should already
// automatically handle and log all unexpected internal errors,
// but wrap the entire call in a try/catch plus additional logging
// to ensure that absolutely no errors can result in the entire
// thread dying
try {
evictExpiredSessions();
evictExpiredOrInvalidSessions();
}
catch (Throwable t) {
logger.error("An unexpected internal error prevented the "