Skip to content

Commit e18b048

Browse files
committed
Refactorization of the method startCleanupThread, add method getBucketsCount and ageBucketsForTesting
1 parent 2d93fc5 commit e18b048

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

src/main/java/org/example/filter/RateLimitingFilter.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class RateLimitingFilter implements Filter {
2727
private static final Map<String, BucketWrapper> buckets = new ConcurrentHashMap<>();
2828
private static final long CAPACITY = 10;
2929
private static final long REFILL_TOKENS = 1;
30-
private final Duration REFILL_PERIOD = Duration.ofSeconds(10);
30+
private final Duration refillPeriod = Duration.ofSeconds(10);
3131
private static final int MAX_BUCKETS_THRESHOLD = 1000;
3232

3333
@Override
@@ -76,15 +76,11 @@ private Bucket createNewBucket() {
7676
return Bucket.builder()
7777
.addLimit(Bandwidth.builder()
7878
.capacity(CAPACITY)
79-
.refillGreedy(REFILL_TOKENS, REFILL_PERIOD)
79+
.refillGreedy(REFILL_TOKENS, refillPeriod)
8080
.build())
8181
.build();
8282
}
8383

84-
public void clearBuckets(){
85-
buckets.clear();
86-
}
87-
8884
/**
8985
* Track the last access time of every bucket
9086
*/
@@ -102,27 +98,40 @@ void updateAccess() {
10298
}
10399
}
104100

105-
private void startCleanupThread() {
106-
Thread cleanupThread = new Thread(() -> {
101+
public void startCleanupThread() {
102+
Thread.ofVirtual().start(() -> {
107103
while (!Thread.currentThread().isInterrupted()) {
108104
try {
109105
//it checks every 10 minutes
110106
Thread.sleep(Duration.ofMinutes(10).toMillis());
111107

112-
//it will only clean when the size of the buckets is more than 1000
113-
if (buckets.size() > MAX_BUCKETS_THRESHOLD) {
114-
long idleThreshold = System.currentTimeMillis() - Duration.ofMinutes(30).toMillis();
115-
buckets.entrySet().removeIf(entry -> entry.getValue().lastAccessTime < idleThreshold);
116-
}
117-
108+
cleanupIdleBuckets();
118109

119-
} catch (InterruptedException e) {
110+
} catch (InterruptedException _) {
120111
Thread.currentThread().interrupt();
112+
break;
121113
}
122114
}
123115
});
124-
cleanupThread.setDaemon(true);
125-
cleanupThread.start();
116+
}
117+
118+
public void cleanupIdleBuckets() {
119+
//it will only clean when the size of the buckets is more than 1000
120+
if (buckets.size() > MAX_BUCKETS_THRESHOLD) {
121+
long idleThreshold = System.currentTimeMillis() - Duration.ofMinutes(30).toMillis();
122+
buckets.entrySet().removeIf(entry -> entry.getValue().lastAccessTime < idleThreshold);
123+
}
124+
}
125+
126+
public int getBucketsCount() {
127+
return buckets.size();
128+
}
129+
130+
public void ageBucketsForTesting(long millisToSubtract) {
131+
for (BucketWrapper wrapper : buckets.values()) {
132+
long oldTime = wrapper.lastAccessTime;
133+
wrapper.lastAccessTime = oldTime - millisToSubtract;
134+
}
126135
}
127136

128137
}

0 commit comments

Comments
 (0)