ppkarwasz commented on code in PR #3474:
URL: https://github.com/apache/logging-log4j2/pull/3474#discussion_r1965597651


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java:
##########
@@ -53,23 +57,48 @@ public final class InternalLoggerRegistry {
             new WeakHashMap<>();
 
     private final ReadWriteLock lock = new ReentrantReadWriteLock();
-
     private final Lock readLock = lock.readLock();
-
     private final Lock writeLock = lock.writeLock();
 
+    // ReferenceQueue to track stale WeakReferences
+    private final ReferenceQueue<Logger> staleLoggerRefs = new 
ReferenceQueue<>();
+
     public InternalLoggerRegistry() {}
 
+    /**
+     * Expunges stale logger references from the registry.
+     */
+    private void expungeStaleEntries() {
+        Reference<? extends Logger> loggerRef;
+        while ((loggerRef = staleLoggerRefs.poll()) != null) {
+            removeLogger(loggerRef);
+        }
+    }
+
+    /**
+     * Removes a logger from the registry.
+     */
+    private void removeLogger(Reference<? extends Logger> loggerRef) {
+        writeLock.lock();
+        try {
+            loggerRefByNameByMessageFactory.values().forEach(map -> 
map.values().removeIf(ref -> ref == loggerRef));
+        } finally {
+            writeLock.unlock();
+        }

Review Comment:
   I didn't benchmark this, but it seems highly inefficient:
   
   - the write lock is acquired for each logger being collected separately.
   - we iterate over the whole map, even if we know which entry must be 
removed: `(logger.getMessageFactory(), logger.getName())`.



-- 
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: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to