log catastrophic errors in getIndex, clean up comments

pull/1/head
ansuz 6 years ago
parent 62c23a29a7
commit 7642ee9584

@ -229,9 +229,10 @@ module.exports.create = function (cfg) {
as an added bonus: as an added bonus:
if the channel exists but its index does not then it caches the index if the channel exists but its index does not then it caches the index
*/ */
const indexQueue = {}; const indexQueues = {};
const getIndex = (ctx, channelName, cb) => { const getIndex = (ctx, channelName, cb) => {
const chan = ctx.channels[channelName]; const chan = ctx.channels[channelName];
// if there is a channel in memory and it has an index cached, return it
if (chan && chan.index) { if (chan && chan.index) {
// enforce async behaviour // enforce async behaviour
return void setTimeout(function () { return void setTimeout(function () {
@ -239,38 +240,35 @@ module.exports.create = function (cfg) {
}); });
} }
// make a queue of callbacks in case getIndex is called before you
// compute and cache the index
// if a call to computeIndex is already in progress for this channel // if a call to computeIndex is already in progress for this channel
// then add the callback for the latest invocation to the queue // then add the callback for the latest invocation to the queue
// and wait for it to complete // and wait for it to complete
if (indexQueue[channelName]) { if (Array.isArray(indexQueues[channelName])) {
indexQueue.channelName.push(cb); indexQueues[channelName].push(cb);
return; return;
} }
// if computeIndex is not already in progress, make an array // otherwise, make a queue for any 'getIndex' calls made before the following 'computeIndex' call completes
// with the current call first in line, so additional calls var queue = indexQueues[channelName] = (indexQueues[channelName] || [cb]);
// can add their callbacks to the queue
indexQueue[channelName] = (indexQueue[channelName] || []).push(cb);
computeIndex(channelName, (err, ret) => { computeIndex(channelName, (err, ret) => {
// keep a local reference to this channel's queue if (!Array.isArray(queue)) {
let queue = indexQueue[channelName]; // something is very wrong if there's no callback array
return void Log.error("E_INDEX_NO_CALLBACK", channelName);
}
// but remove it from the map of queues and not worry about cleaning up // clean up the queue that you're about to handle, but keep a local copy
// as long as computeIndex always calls back this won't be a memory leak delete indexQueues[channelName];
delete indexQueue[channelName];
// this is most likely an unrecoverable filesystem error
if (err) { if (err) {
// call back every pending function with the error // call back every pending function with the error
return void queue.forEach(function (_cb) { return void queue.forEach(function (_cb) {
_cb(err); _cb(err);
}); });
} }
// if there's a channel to use as a cache, store the result // cache the computed result if possible
// for future use
if (chan) { chan.index = ret; } if (chan) { chan.index = ret; }
// call back every pending function with the result // call back every pending function with the result

Loading…
Cancel
Save