Permalink
Browse files

[fix] fixes after leak

  • Loading branch information...
1 parent ce05f24 commit b9dd1db5a5604aa23e80b470297a0dfd3530d3bd @dscape dscape committed Mar 20, 2012
Showing with 40 additions and 8 deletions.
  1. +26 −0 examples/after/index.js
  2. +6 −1 lib/core.js
  3. +0 −1 lib/routing-stream.js
  4. +8 −6 test/after-test.js
@@ -0,0 +1,26 @@
+var fs = require('fs'),
+ path = require('path'),
+ union = require('../../lib');
+
+var server = union.createServer({
+ before: [ function (req,res) {
+ if(req.url === "/foo") {
+ res.text(201, "foo");
+ }
+ } ],
+ after: [
+ function LoggerStream() {
+ var stream = new union.ResponseStream();
+
+ stream.once("pipe", function (req) {
+ console.log({res: this.res.statusCode, method: this.req.method});
+ });
+
+ return stream;
+ }
+ ]
+});
+
+server.listen(9080);
+console.log('union running on 9080');
+
View
@@ -23,7 +23,12 @@ core.createServer = function (options) {
function requestHandler(req, res) {
var routingStream = new RoutingStream({
before: options.before,
- after: options.after,
+ // without new after is a huge memory leak that pipes to every
+ // single open connection
+ after: Array.isArray(options.after) &&
+ options.after.map(function (a) {
+ return new a;
+ }),
response: res,
limit: options.limit,
headers: options.headers
@@ -79,7 +79,6 @@ RoutingStream.prototype.route = function (req) {
after[i+1].res = this.response;
after[i].pipe(after[i + 1]);
// prevent multiple responses and memory leaks
- after[i].removeAllListeners("error");
after[i].on('error', this.onError);
}
View
@@ -4,14 +4,16 @@ var assert = require('assert'),
union = require('../');
function stream_callback(cb) {
- var stream = new union.ResponseStream();
+ return function () {
+ var stream = new union.ResponseStream();
- stream.once("pipe", function (req) {
- console.log(req);
- return cb ? cb(null,req) : undefined;
- });
+ stream.once("pipe", function (req) {
+ console.log(req);
+ return cb ? cb(null,req) : undefined;
+ });
- return stream;
+ return stream;
+ };
}
vows.describe('union/after').addBatch({

0 comments on commit b9dd1db

Please sign in to comment.