Skip to content

Commit

Permalink
bugfix: the issue of possible infinite loop when cleaning up expired …
Browse files Browse the repository at this point in the history
…metadata info
  • Loading branch information
funky-eyes committed Jan 22, 2025
1 parent 7d09195 commit 0c7f59b
Showing 1 changed file with 5 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,24 @@ private AbstractServiceDiscovery(ApplicationModel applicationModel, String servi

private void removeExpiredMetadataInfo(int metadataInfoCacheSize, int metadataInfoCacheExpireTime) {
Long nextTime = null;
// Cache cleanup is only required when the cache size exceeds the cache limit.
if (metadataInfos.size() > metadataInfoCacheSize) {
List<MetadataInfoStat> values = new ArrayList<>(metadataInfos.values());
// Place the earliest data at the front
values.sort(Comparator.comparingLong(MetadataInfoStat::getUpdateTime));
for (MetadataInfoStat v : values) {
long time = System.currentTimeMillis() - v.getUpdateTime();
if (time > metadataInfoCacheExpireTime) {
metadataInfos.remove(v.metadataInfo.getRevision(), v);
} else {
// Calculate how long it will take for the next task to start
nextTime = metadataInfoCacheExpireTime - time;
break;
}
}
}
// If there is no metadata to clean up this time, the next task will start within half of the cache expiration
// time.
startRefreshCache(
nextTime == null ? metadataInfoCacheExpireTime / 2 : nextTime,
metadataInfoCacheSize,
Expand Down

0 comments on commit 0c7f59b

Please sign in to comment.