Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(maven): Simplify HEAD requests cache #25868

Merged
merged 3 commits into from Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions lib/modules/datasource/maven/index.ts
Expand Up @@ -3,6 +3,7 @@ import { DateTime } from 'luxon';
import type { XmlDocument } from 'xmldoc';
import { logger } from '../../../logger';
import * as packageCache from '../../../util/cache/package';
import { filterMap } from '../../../util/filter-map';
import * as p from '../../../util/promises';
import { newlineRegex, regEx } from '../../../util/regex';
import { ensureTrailingSlash } from '../../../util/url';
Expand Down Expand Up @@ -220,31 +221,40 @@ export class MavenDatasource extends Datasource {
//
// Even if new version is being released each 10 minutes,
// we still want to reset the whole cache after 24 hours.
const isCacheValid = await packageCache.get<true>(cacheTimeoutNs, cacheKey);
const cacheValid = await packageCache.get<'valid'>(
cacheTimeoutNs,
cacheKey,
);

let cachedReleaseMap: ReleaseMap = {};
// istanbul ignore if
if (isCacheValid) {
if (cacheValid) {
const cache = await packageCache.get<ReleaseMap>(cacheNs, cacheKey);
if (cache) {
cachedReleaseMap = cache;
}
}

// List versions to check with HEAD request
const freshVersions = Object.entries(releaseMap)
.filter(([version, release]) => {
const freshVersions = filterMap(
Object.entries(releaseMap),
([version, release]) => {
// Release is present in maven-metadata.xml,
// but haven't been validated yet
const isValidatedAtPreviousSteps = release !== null;

// Release was validated and cached with HEAD request during previous run
const isValidatedHere = !is.undefined(cachedReleaseMap[version]);

// istanbul ignore if: not easily testable
if (isValidatedAtPreviousSteps || isValidatedHere) {
return null;
}

// Select only valid releases not yet verified with HEAD request
return !isValidatedAtPreviousSteps && !isValidatedHere;
})
.map(([k]) => k);
return version;
},
);

// Update cached data with freshly discovered versions
if (freshVersions.length) {
Expand All @@ -270,9 +280,9 @@ export class MavenDatasource extends Datasource {

await p.all(queue);

if (!isCacheValid) {
if (!cacheValid) {
// Store new TTL flag for 24 hours if the previous one is invalidated
await packageCache.set(cacheTimeoutNs, cacheKey, 'long', 24 * 60);
await packageCache.set(cacheTimeoutNs, cacheKey, 'valid', 24 * 60);
}

// Store updated cache object
Expand Down