long purge()

in concurrency-loadbalancer-core/src/main/java/com/uber/concurrency/loadbalancer/timedcounter/LifespanTracker.java [126:146]


    long purge(long nanoTimestamp) {
        if (windowNanos != 0) {
            long windowId = nanoTimestamp / windowNanos;
            //lastWindowId indicates last logical window which count is in-sync with global count
            long id = lastWindowId.get();
            if (id < windowId && lastWindowId.compareAndSet(id, windowId)) {
                //only one thread can entry here for the same lastWindowId
                int n = 0;
                for (; id < windowId && n < totalWindows; id++, n++) {
                    //recycle all expired window, minus count from global count
                    int index = (int) (((id % totalWindows) + totalWindows) % totalWindows);
                    Window window = windows[index];
                    if (window.count.get() != 0 && window.windowId < windowId) {
                        toBePurged.addAndGet(window.count.getAndSet(0));
                    }
                }
            }
        }

        return toBePurged.getAndSet(0);
    }