Skip to content

Commit

Permalink
Add limitStat to keep concurrent requests under control
Browse files Browse the repository at this point in the history
  • Loading branch information
aredridel committed May 18, 2015
1 parent 7b9270e commit 1fe4c1e
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function resolveView(dir, file, ext, cb) {

// <path>.<ext>
path = join(dir, file);
fs.stat(path, function (err, stat) {
limitStat(path, function (err, stat) {
if (err && err.code == 'ENOENT') {
// Skip down
} else if (!err && stat && stat.isFile()) {
Expand All @@ -158,7 +158,7 @@ function resolveView(dir, file, ext, cb) {

// <path>/index.<ext>
path = join(dir, basename(file, ext), 'index' + ext);
fs.stat(path, function (err, stat) {
limitStat(path, function (err, stat) {
if (err && err.code == 'ENOENT') {
return cb(null, null);
} else if (!err && stat && stat.isFile()) {
Expand All @@ -169,3 +169,29 @@ function resolveView(dir, file, ext, cb) {
});
});
}

var pendingStats = [];
var numPendingStats = 0;
/**
* an fs.stat call that limits the number of outstanding requests to 10.
*
* @param {String} path
* @param {Function} cb
*/
function limitStat(path, cb) {
if (++numPendingStats > 10) {
pendingStats.push([path, cb]);
} else {
fs.stat(path, dequeue);
}

function dequeue(err, stat) {
cb(err, stat);
var next = pendingStats.shift();
if (next) {
fs.stat(next[0], next[1]);
} else {
numPendingStats--;
}
}
}

0 comments on commit 1fe4c1e

Please sign in to comment.