Skip to content

Commit ffeab94

Browse files
committed
Add limitStat to keep concurrent requests under control
1 parent 520214a commit ffeab94

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

lib/view.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,11 @@ function View(name, options) {
6161
throw new Error('No default engine was specified and no extension was provided.');
6262
}
6363

64-
var fileName = name;
65-
6664
if (!this.ext) {
6765
// get extension from default engine name
6866
this.ext = this.defaultEngine[0] !== '.'
6967
? '.' + this.defaultEngine
7068
: this.defaultEngine;
71-
72-
fileName += this.ext;
7369
}
7470

7571
if (!opts.engines[this.ext]) {
@@ -215,7 +211,7 @@ function resolveView(dir, file, ext, cb) {
215211
var path = join(dir, file);
216212

217213
// <path>.<ext>
218-
fs.stat(path, function (err, stat) {
214+
limitStat(path, function (err, stat) {
219215
if (err && err.code !== 'ENOENT') {
220216
return cb(err);
221217
} else if (!err && stat && stat.isFile()) {
@@ -224,7 +220,7 @@ function resolveView(dir, file, ext, cb) {
224220

225221
// <path>/index.<ext>
226222
path = join(dir, basename(file, ext), 'index' + ext);
227-
fs.stat(path, function (err, stat) {
223+
limitStat(path, function (err, stat) {
228224
if (err && err.code === 'ENOENT') {
229225
return cb(null, null);
230226
} else if (!err && stat && stat.isFile()) {
@@ -235,3 +231,31 @@ function resolveView(dir, file, ext, cb) {
235231
});
236232
});
237233
}
234+
235+
var pendingStats = [];
236+
var numPendingStats = 0;
237+
/**
238+
* an fs.stat call that limits the number of outstanding requests to 10.
239+
*
240+
* @param {String} path
241+
* @param {Function} cb
242+
*/
243+
function limitStat(path, cb) {
244+
if (++numPendingStats > 10) {
245+
pendingStats.push([path, cb]);
246+
} else {
247+
fs.stat(path, cbAndDequeue(cb));
248+
}
249+
250+
function cbAndDequeue(cb) {
251+
return function (err, stat) {
252+
cb(err, stat);
253+
var next = pendingStats.shift();
254+
if (next) {
255+
fs.stat(next[0], cbAndDequeue(next[1]));
256+
} else {
257+
numPendingStats--;
258+
}
259+
}
260+
}
261+
}

0 commit comments

Comments
 (0)