Skip to content

Commit

Permalink
[#noissue] Reduce Memory Usage
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jul 16, 2024
1 parent ea3c9eb commit b283b83
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import org.apache.logging.log4j.Logger;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;

/**
* @author Woonduk Kang(emeroad)
*/
public class ThrottledLogger {
private static final AtomicLongFieldUpdater<ThrottledLogger> UPDATER = AtomicLongFieldUpdater.newUpdater(ThrottledLogger.class, "counter");

private volatile long counter;

private final Logger logger;
private final long ratio;
private final AtomicLong counter = new AtomicLong();

public static ThrottledLogger getLogger(Logger logger, long ratio) {
Objects.requireNonNull(logger, "logger");
Expand All @@ -40,14 +43,14 @@ private ThrottledLogger(Logger logger, long ratio) {
}

private boolean checkLogCounter() {
if (counter.getAndIncrement() % ratio == 0) {
if (UPDATER.getAndIncrement(this) % ratio == 0) {
return true;
}
return false;
}

public long getCounter() {
return counter.get();
return UPDATER.get(this);
}

public boolean isInfoEnabled() {
Expand Down

0 comments on commit b283b83

Please sign in to comment.