Check Redis cache inside of process mutex
This commit is contained in:
parent
2f7f805cd6
commit
5b1750c182
|
@ -3,26 +3,22 @@ require("isomorphic-fetch");
|
|||
const config = require("../config");
|
||||
|
||||
function fetchPackageInfo(packageName) {
|
||||
return new Promise(resolve => {
|
||||
let encodedPackageName;
|
||||
if (packageName.charAt(0) === "@") {
|
||||
encodedPackageName = `@${encodeURIComponent(packageName.substring(1))}`;
|
||||
} else {
|
||||
encodedPackageName = encodeURIComponent(packageName);
|
||||
let encodedPackageName;
|
||||
if (packageName.charAt(0) === "@") {
|
||||
encodedPackageName = `@${encodeURIComponent(packageName.substring(1))}`;
|
||||
} else {
|
||||
encodedPackageName = encodeURIComponent(packageName);
|
||||
}
|
||||
|
||||
const url = `${config.registryURL}/${encodedPackageName}`;
|
||||
|
||||
console.log(`info: Fetching package info from ${url}`);
|
||||
|
||||
return fetch(url, {
|
||||
headers: {
|
||||
Accept: "application/json"
|
||||
}
|
||||
|
||||
const url = `${config.registryURL}/${encodedPackageName}`;
|
||||
|
||||
console.log(`info: Fetching package info from ${url}`);
|
||||
|
||||
resolve(
|
||||
fetch(url, {
|
||||
headers: {
|
||||
Accept: "application/json"
|
||||
}
|
||||
}).then(res => (res.status === 404 ? null : res.json()))
|
||||
);
|
||||
});
|
||||
}).then(res => (res.status === 404 ? null : res.json()));
|
||||
}
|
||||
|
||||
module.exports = fetchPackageInfo;
|
||||
|
|
|
@ -2,53 +2,51 @@ const createCache = require("./createCache");
|
|||
const createMutex = require("./createMutex");
|
||||
const fetchPackageInfo = require("./fetchPackageInfo");
|
||||
|
||||
const packageInfoCache = createCache("packageInfo");
|
||||
const packageNotFound = "PackageNotFound";
|
||||
const cache = createCache("packageInfo");
|
||||
const notFound = "PackageNotFound";
|
||||
|
||||
// This mutex prevents multiple concurrent requests to
|
||||
// the registry for the same package info.
|
||||
const fetchMutex = createMutex((packageName, callback) => {
|
||||
fetchPackageInfo(packageName).then(
|
||||
value => {
|
||||
if (value == null) {
|
||||
// Cache 404s for 5 minutes. This prevents us from making
|
||||
// unnecessary requests to the registry for bad package names.
|
||||
// In the worst case, a brand new package's info will be
|
||||
// available within 5 minutes.
|
||||
packageInfoCache.set(packageName, packageNotFound, 300, () => {
|
||||
callback(null, value);
|
||||
});
|
||||
} else {
|
||||
// Cache valid package info for 1 minute.
|
||||
packageInfoCache.set(packageName, value, 60, () => {
|
||||
callback(null, value);
|
||||
});
|
||||
}
|
||||
},
|
||||
error => {
|
||||
// Do not cache errors.
|
||||
packageInfoCache.del(packageName, () => {
|
||||
callback(error);
|
||||
});
|
||||
cache.get(packageName, (error, value) => {
|
||||
if (error) {
|
||||
callback(error);
|
||||
} else if (value != null) {
|
||||
callback(null, value === notFound ? null : value);
|
||||
} else {
|
||||
fetchPackageInfo(packageName).then(
|
||||
value => {
|
||||
if (value == null) {
|
||||
// Cache 404s for 5 minutes. This prevents us from making
|
||||
// unnecessary requests to the registry for bad package names.
|
||||
// In the worst case, a brand new package's info will be
|
||||
// available within 5 minutes.
|
||||
cache.set(packageName, notFound, 300, () => {
|
||||
callback(null, value);
|
||||
});
|
||||
} else {
|
||||
// Cache valid package info for 1 minute.
|
||||
cache.set(packageName, value, 60, () => {
|
||||
callback(null, value);
|
||||
});
|
||||
}
|
||||
},
|
||||
error => {
|
||||
// Do not cache errors.
|
||||
cache.del(packageName, () => {
|
||||
callback(error);
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
function getPackageInfo(packageName) {
|
||||
return new Promise((resolve, reject) => {
|
||||
packageInfoCache.get(packageName, (error, value) => {
|
||||
fetchMutex(packageName, (error, value) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else if (value != null) {
|
||||
resolve(value === packageNotFound ? null : value);
|
||||
} else {
|
||||
fetchMutex(packageName, (error, value) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue