zeroflag commented on code in PR #620:
URL: https://github.com/apache/knox/pull/620#discussion_r950167301


##########
gateway-server/src/main/java/org/apache/knox/gateway/session/control/InMemoryConcurrentSessionVerifier.java:
##########
@@ -122,17 +129,34 @@ public void init(GatewayConfig config, Map<String, 
String> options) throws Servi
     this.nonPrivilegedUsers = config.getNonPrivilegedUsers();
     this.privilegedUserConcurrentSessionLimit = 
config.getPrivilegedUsersConcurrentSessionLimit();
     this.nonPrivilegedUserConcurrentSessionLimit = 
config.getNonPrivilegedUsersConcurrentSessionLimit();
+    this.cleaningPeriod = 
config.getConcurrentSessionVerifierExpiredTokensCleaningPeriod();
     this.concurrentSessionCounter = new ConcurrentHashMap<>();
   }
 
   @Override
   public void start() throws ServiceLifecycleException {
-
+    expiredTokenRemover.scheduleAtFixedRate(this::removeExpiredTokens, 
cleaningPeriod, cleaningPeriod, TimeUnit.SECONDS);
   }
 
   @Override
   public void stop() throws ServiceLifecycleException {
+    expiredTokenRemover.shutdown();
+  }
 
+  private void removeExpiredTokens() {
+    sessionCountModifyLock.lock();
+    try {
+      Iterator<Map.Entry<String, Set<SessionJWT>>> 
concurrentSessionCounterIterator = 
concurrentSessionCounter.entrySet().iterator();
+      while (concurrentSessionCounterIterator.hasNext()) {
+        Map.Entry<String, Set<SessionJWT>> concurrentSessionCounterMapEntry = 
concurrentSessionCounterIterator.next();
+        
concurrentSessionCounterMapEntry.setValue(concurrentSessionCounterMapEntry.getValue().stream().filter(sessionJWT
 -> !sessionJWT.hasExpired()).collect(Collectors.toSet()));

Review Comment:
   This might be shorter and easer to read than the filter expression. Plus 
it's probably faster too.
   
   ```java
   concurrentSessionCounterMapEntry.getValue().removeIf(session -> 
session.hasExpired());
   ```



##########
gateway-server/src/main/java/org/apache/knox/gateway/services/factory/ConcurrentSessionVerifierFactory.java:
##########
@@ -17,21 +17,37 @@
  */
 package org.apache.knox.gateway.services.factory;
 
+import static java.util.Arrays.asList;
+import static java.util.Collections.unmodifiableList;
+
+import java.util.Collection;
+import java.util.Map;
+
 import org.apache.knox.gateway.config.GatewayConfig;
 import org.apache.knox.gateway.services.GatewayServices;
 import org.apache.knox.gateway.services.Service;
 import org.apache.knox.gateway.services.ServiceLifecycleException;
 import org.apache.knox.gateway.services.ServiceType;
+import org.apache.knox.gateway.session.control.EmptyConcurrentSessionVerifier;
 import 
org.apache.knox.gateway.session.control.InMemoryConcurrentSessionVerifier;
 
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-
 public class ConcurrentSessionVerifierFactory extends AbstractServiceFactory {
   @Override
   protected Service createService(GatewayServices gatewayServices, ServiceType 
serviceType, GatewayConfig gatewayConfig, Map<String, String> options, String 
implementation) throws ServiceLifecycleException {
-    return shouldCreateService(implementation) ? new 
InMemoryConcurrentSessionVerifier() : null;
+    Service service = null;
+    if (shouldCreateService(implementation)) {

Review Comment:
   If `shouldCreateService` returns false we don't create any implementation 
and we return null? Later at other place we have assumption that there is 
always an implementation available (either the null object or the real one).



-- 
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]

Reply via email to