Permalink
Browse files

Added backpressure support to `staticCache()`

  • Loading branch information...
tj committed Sep 2, 2011
1 parent 142bf02 commit 4123b0dc922739b1dbcb108bbeae224562aa645e
Showing with 14 additions and 4 deletions.
  1. +14 −4 lib/middleware/staticCache.js
@@ -75,11 +75,21 @@ module.exports = function staticCache(options){
// respond with cache
res.writeHead(200, header);
- for (var i = 1, len = hit.length; i < len; ++i) {
- res.write(hit[i]);
+
+ // backpressure
+ function write(i) {
+ var buf = hit[i];
+ if (!buf) return res.end();
+ if (false === res.write(buf)) {
+ res.once('drain', function(){
+ write(++i);
+ });
+ } else {
+ write(++i);
+ }
}
- res.end();
- return;
+
+ return write(1);
}
// cache static

0 comments on commit 4123b0d

Please sign in to comment.