Moving the http.js, net.js FreeList to being standalone.
This commit is contained in:
parent
b7947e45c0
commit
57ea07ac91
22
lib/freelist.js
Normal file
22
lib/freelist.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// This is a free list to avoid creating so many of the same object.
|
||||||
|
exports.FreeList = function(name, max, constructor) {
|
||||||
|
this.name = name;
|
||||||
|
this.constructor = constructor;
|
||||||
|
this.max = max;
|
||||||
|
this.list = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
exports.FreeList.prototype.alloc = function () {
|
||||||
|
//debug("alloc " + this.name + " " + this.list.length);
|
||||||
|
return this.list.length ? this.list.shift()
|
||||||
|
: this.constructor.apply(this, arguments);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
exports.FreeList.prototype.free = function (obj) {
|
||||||
|
//debug("free " + this.name + " " + this.list.length);
|
||||||
|
if (this.list.length < this.max) {
|
||||||
|
this.list.push(obj);
|
||||||
|
}
|
||||||
|
};
|
32
lib/http.js
32
lib/http.js
@ -11,17 +11,11 @@ var sys = require('sys');
|
|||||||
var net = require('net');
|
var net = require('net');
|
||||||
var events = require('events');
|
var events = require('events');
|
||||||
|
|
||||||
|
var FreeList = require('freelist').FreeList;
|
||||||
var HTTPParser = process.binding('http_parser').HTTPParser;
|
var HTTPParser = process.binding('http_parser').HTTPParser;
|
||||||
|
|
||||||
var parserFreeList = [];
|
var parsers = new FreeList('parsers', 1000, function () {
|
||||||
|
var parser = new HTTPParser('request');
|
||||||
function newParser (type) {
|
|
||||||
var parser;
|
|
||||||
if (parserFreeList.length) {
|
|
||||||
parser = parserFreeList.shift();
|
|
||||||
parser.reinitialize(type);
|
|
||||||
} else {
|
|
||||||
parser = new HTTPParser(type);
|
|
||||||
|
|
||||||
parser.onMessageBegin = function () {
|
parser.onMessageBegin = function () {
|
||||||
parser.incoming = new IncomingMessage(parser.socket);
|
parser.incoming = new IncomingMessage(parser.socket);
|
||||||
@ -99,13 +93,9 @@ function newParser (type) {
|
|||||||
parser.onMessageComplete = function () {
|
parser.onMessageComplete = function () {
|
||||||
parser.incoming.emit("end");
|
parser.incoming.emit("end");
|
||||||
};
|
};
|
||||||
}
|
|
||||||
return parser;
|
|
||||||
}
|
|
||||||
|
|
||||||
function freeParser (parser) {
|
return parser;
|
||||||
if (parserFreeList.length < 1000) parserFreeList.push(parser);
|
});
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var CRLF = "\r\n";
|
var CRLF = "\r\n";
|
||||||
@ -517,7 +507,9 @@ function connectionListener (socket) {
|
|||||||
// we need to keep track of the order they were sent.
|
// we need to keep track of the order they were sent.
|
||||||
var responses = [];
|
var responses = [];
|
||||||
|
|
||||||
var parser = newParser('request');
|
var parser = parsers.alloc();
|
||||||
|
parser.reinitialize('request');
|
||||||
|
parser.socket = socket;
|
||||||
|
|
||||||
socket.ondata = function (d, start, end) {
|
socket.ondata = function (d, start, end) {
|
||||||
parser.execute(d, start, end - start);
|
parser.execute(d, start, end - start);
|
||||||
@ -526,7 +518,7 @@ function connectionListener (socket) {
|
|||||||
socket.onend = function () {
|
socket.onend = function () {
|
||||||
parser.finish();
|
parser.finish();
|
||||||
// unref the parser for easy gc
|
// unref the parser for easy gc
|
||||||
freeParser(parser);
|
parsers.free(parser);
|
||||||
|
|
||||||
if (responses.length == 0) {
|
if (responses.length == 0) {
|
||||||
socket.end();
|
socket.end();
|
||||||
@ -535,7 +527,6 @@ function connectionListener (socket) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
parser.socket = socket;
|
|
||||||
// The following callback is issued after the headers have been read on a
|
// The following callback is issued after the headers have been read on a
|
||||||
// new message. In this callback we setup the response object and pass it
|
// new message. In this callback we setup the response object and pass it
|
||||||
// to the user.
|
// to the user.
|
||||||
@ -563,7 +554,8 @@ function Client ( ) {
|
|||||||
var requests = [];
|
var requests = [];
|
||||||
var currentRequest;
|
var currentRequest;
|
||||||
|
|
||||||
var parser = newParser('response');
|
var parser = parsers.alloc();
|
||||||
|
parser.reinitialize('response');
|
||||||
parser.socket = this;
|
parser.socket = this;
|
||||||
|
|
||||||
self._reconnect = function () {
|
self._reconnect = function () {
|
||||||
@ -617,7 +609,7 @@ function Client ( ) {
|
|||||||
if (requests.length > 0) {
|
if (requests.length > 0) {
|
||||||
self._reconnect();
|
self._reconnect();
|
||||||
} else {
|
} else {
|
||||||
freeParser(parser);
|
parsers.free(parser);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
30
lib/net.js
30
lib/net.js
@ -22,6 +22,7 @@ var binding = process.binding('net');
|
|||||||
// yet convinced that every use-case can be fit into that abstraction, so
|
// yet convinced that every use-case can be fit into that abstraction, so
|
||||||
// waiting to implement it until I get more experience with this.
|
// waiting to implement it until I get more experience with this.
|
||||||
var Buffer = require('buffer').Buffer;
|
var Buffer = require('buffer').Buffer;
|
||||||
|
var FreeList = require('freelist').FreeList;
|
||||||
|
|
||||||
var IOWatcher = process.IOWatcher;
|
var IOWatcher = process.IOWatcher;
|
||||||
var assert = process.assert;
|
var assert = process.assert;
|
||||||
@ -205,35 +206,6 @@ var timeout = new (function () {
|
|||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// This is a free list to avoid creating so many of the same object.
|
|
||||||
|
|
||||||
function FreeList (name, max, constructor) {
|
|
||||||
this.name = name;
|
|
||||||
this.constructor = constructor;
|
|
||||||
this.max = max;
|
|
||||||
this.list = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
FreeList.prototype.alloc = function () {
|
|
||||||
//debug("alloc " + this.name + " " + this.list.length);
|
|
||||||
return this.list.length ? this.list.shift()
|
|
||||||
: this.constructor.apply(this, arguments);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
FreeList.prototype.free = function (obj) {
|
|
||||||
//debug("free " + this.name + " " + this.list.length);
|
|
||||||
if (this.list.length < this.max) {
|
|
||||||
this.list.push(obj);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
var ioWatchers = new FreeList("iowatcher", 100, function () {
|
var ioWatchers = new FreeList("iowatcher", 100, function () {
|
||||||
return new IOWatcher();
|
return new IOWatcher();
|
||||||
});
|
});
|
||||||
|
@ -1268,6 +1268,7 @@ static Handle<Value> Binding(const Arguments& args) {
|
|||||||
exports->Set(String::New("dns"), String::New(native_dns));
|
exports->Set(String::New("dns"), String::New(native_dns));
|
||||||
exports->Set(String::New("events"), String::New(native_events));
|
exports->Set(String::New("events"), String::New(native_events));
|
||||||
exports->Set(String::New("file"), String::New(native_file));
|
exports->Set(String::New("file"), String::New(native_file));
|
||||||
|
exports->Set(String::New("freelist"), String::New(native_freelist));
|
||||||
exports->Set(String::New("fs"), String::New(native_fs));
|
exports->Set(String::New("fs"), String::New(native_fs));
|
||||||
exports->Set(String::New("http"), String::New(native_http));
|
exports->Set(String::New("http"), String::New(native_http));
|
||||||
exports->Set(String::New("http_old"), String::New(native_http_old));
|
exports->Set(String::New("http_old"), String::New(native_http_old));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user