Stream files during cleanup
This should be more memory efficient than the previous behaviour, which
was to maintain all files in a list before checking their expiry in
order to delete them.
Bug: Issue 15562
Change-Id: I59910ac5959b24f7e1938a0f5b09fdebd9654c3c
diff --git a/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCache.java b/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCache.java
index 3167a48..cbdc8aa 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCache.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCache.java
@@ -44,6 +44,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
+import java.util.function.Consumer;
@Singleton
public class FlatFileWebSessionCache implements Cache<String, WebSessionManager.Val> {
@@ -96,19 +97,16 @@
@Override
public void cleanUp() {
- for (Path path : listFiles()) {
- try {
- Val val = readFile(path);
- if (val != null) {
- Instant expires = Instant.ofEpochMilli(val.getExpiresAt());
- if (expires.isBefore(TimeMachine.now())) {
- deleteFile(path);
+ foreachSession(
+ path -> {
+ Val val = readFile(path);
+ if (val != null) {
+ Instant expires = Instant.ofEpochMilli(val.getExpiresAt());
+ if (expires.isBefore(TimeMachine.now())) {
+ deleteFile(path);
+ }
}
- }
- } catch (Exception e) {
- log.atSevere().withCause(e).log("Exception while cleaning %s", path);
- }
- }
+ });
}
@Override
@@ -241,4 +239,12 @@
}
return files;
}
+
+ private void foreachSession(Consumer<Path> sessionPath) {
+ try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(websessionsDir)) {
+ dirStream.forEach(sessionPath);
+ } catch (IOException e) {
+ log.atSevere().withCause(e).log("Cannot list files in cache %s", websessionsDir);
+ }
+ }
}