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


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java:
##########
@@ -53,13 +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);
+        }

Review Comment:
   Note that if `loggerRef.get()` returns `null`, we still need to remove the 
entry that holds the `WeakReference`.
   
   What do you think about such an approach:
   
   1. We `poll()` the queue until its empty.
   2. If there was any reference in the queue we iterate **once** over all the 
entries of the map and we remove **all** the invalidated `WeakReference`s, not 
just those that were in the queue.
   
   As of [my previous 
comment](https://github.com/apache/logging-log4j2/pull/3474#discussion_r1965597651),
 this seems a little bit inefficient if `loggerRef.get()` is not null, but is 
more efficient than iterating over all entries **once-per-reference**.



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