Remove process-level mutex
This commit is contained in:
parent
c7231b4c5e
commit
e7244e6000
|
@ -1,34 +0,0 @@
|
||||||
const invariant = require("invariant");
|
|
||||||
|
|
||||||
function defaultCreateKey(payload) {
|
|
||||||
return payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createMutex(doWork, createKey = defaultCreateKey) {
|
|
||||||
const mutex = Object.create(null);
|
|
||||||
|
|
||||||
return (payload, callback) => {
|
|
||||||
const key = createKey(payload);
|
|
||||||
|
|
||||||
invariant(
|
|
||||||
typeof key === "string",
|
|
||||||
"Mutex needs a string key; please provide a createKey function that returns a string"
|
|
||||||
);
|
|
||||||
|
|
||||||
if (mutex[key]) {
|
|
||||||
mutex[key].push(callback);
|
|
||||||
} else {
|
|
||||||
mutex[key] = [callback];
|
|
||||||
|
|
||||||
doWork(payload, (error, value) => {
|
|
||||||
mutex[key].forEach(callback => {
|
|
||||||
callback(error, value);
|
|
||||||
});
|
|
||||||
|
|
||||||
delete mutex[key];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = createMutex;
|
|
|
@ -1,54 +1,37 @@
|
||||||
const cache = require("./cache");
|
const cache = require("./cache");
|
||||||
const createMutex = require("./createMutex");
|
|
||||||
const fetchNpmPackageInfo = require("./fetchNpmPackageInfo");
|
const fetchNpmPackageInfo = require("./fetchNpmPackageInfo");
|
||||||
|
|
||||||
const notFound = "PackageNotFound";
|
const notFound = "PackageNotFound";
|
||||||
|
|
||||||
const mutex = createMutex((packageName, callback) => {
|
function getNpmPackageInfo(packageName) {
|
||||||
const key = `packageInfo2-${packageName}`;
|
return new Promise((resolve, reject) => {
|
||||||
|
const key = `npmPackageInfo-${packageName}`;
|
||||||
|
|
||||||
cache.get(key, (error, value) => {
|
cache.get(key, (error, value) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
callback(error);
|
reject(error);
|
||||||
} else if (value != null) {
|
} else if (value != null) {
|
||||||
callback(null, value === notFound ? null : JSON.parse(value));
|
resolve(value === notFound ? null : JSON.parse(value));
|
||||||
} else {
|
} else {
|
||||||
fetchNpmPackageInfo(packageName).then(
|
fetchNpmPackageInfo(packageName).then(value => {
|
||||||
value => {
|
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
|
resolve(null);
|
||||||
|
|
||||||
// Cache 404s for 5 minutes. This prevents us from making
|
// Cache 404s for 5 minutes. This prevents us from making
|
||||||
// unnecessary requests to the registry for bad package names.
|
// unnecessary requests to the registry for bad package names.
|
||||||
// In the worst case, a brand new package's info will be
|
// In the worst case, a brand new package's info will be
|
||||||
// available within 5 minutes.
|
// available within 5 minutes.
|
||||||
cache.setex(key, 300, notFound, () => {
|
cache.setex(key, 300, notFound);
|
||||||
callback(null, null);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
|
resolve(value);
|
||||||
|
|
||||||
// Cache valid package info for 1 minute. In the worst case,
|
// Cache valid package info for 1 minute. In the worst case,
|
||||||
// new versions won't be available for 1 minute.
|
// new versions won't be available for 1 minute.
|
||||||
cache.setnx(key, JSON.stringify(value), (error, reply) => {
|
cache.setnx(key, JSON.stringify(value), (error, reply) => {
|
||||||
if (reply === 1) cache.expire(key, 60);
|
if (reply === 1) cache.expire(key, 60);
|
||||||
callback(null, value);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
}, reject);
|
||||||
error => {
|
|
||||||
// Do not cache errors.
|
|
||||||
cache.del(key);
|
|
||||||
callback(error);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
function getNpmPackageInfo(packageName) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
mutex(packageName, (error, value) => {
|
|
||||||
if (error) {
|
|
||||||
reject(error);
|
|
||||||
} else {
|
|
||||||
resolve(value);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue