Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce listeners added to pipelining socket #39

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ function attachFinishedListener (msg, callback) {

function onFinish (error) {
eeMsg.cancel()
eeSocket.cancel()

finished = true
callback(error)
}
Expand All @@ -110,8 +108,13 @@ function attachFinishedListener (msg, callback) {
if (finished) return
if (eeMsg !== eeSocket) return

var attached = socket.__onFinished
if (!attached || !attached.queue) {
attached = socket.__onFinished = createListener(socket)
eeSocket = first([[socket, 'error', 'close']], attached)
}
// finished on first socket event
eeSocket = first([[socket, 'error', 'close']], onFinish)
attached.queue.push(onFinish)
}

if (msg.socket) {
Expand Down
67 changes: 46 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,12 @@ describe('onFinished(res, listener)', function () {
describe('when calling many times on same response', function () {
it('should not print warnings', function (done) {
var server = http.createServer(function (req, res) {
var stderr = captureStderr(function () {
for (var i = 0; i < 400; i++) {
onFinished(res, noop)
}
})

var stderr = captureStderr()
for (var i = 0; i < 400; i++) {
onFinished(res, noop)
}
onFinished(res, done)
assert.strictEqual(stderr, '')
assert.strictEqual(stderr(), '')
res.end()
})

Expand Down Expand Up @@ -537,14 +535,12 @@ describe('onFinished(req, listener)', function () {
describe('when calling many times on same request', function () {
it('should not print warnings', function (done) {
var server = http.createServer(function (req, res) {
var stderr = captureStderr(function () {
for (var i = 0; i < 400; i++) {
onFinished(req, noop)
}
})

var stderr = captureStderr()
for (var i = 0; i < 400; i++) {
onFinished(req, noop)
}
onFinished(req, done)
assert.strictEqual(stderr, '')
assert.strictEqual(stderr(), '')
res.end()
})

Expand All @@ -558,6 +554,38 @@ describe('onFinished(req, listener)', function () {
})
})

describe('when a client is pipelining requests', function () {
it('should not print warnings', function (done) {
var stderr = captureStderr()
var server = http.createServer(function (req, res) {
onFinished(req, noop)
res.end()
})
server.on('close', function () {
assert.strictEqual(stderr(), '')
done()
})
server.listen(function () {
var pipelineCount = 12
net.connect(this.address().port, function () {
var socket = this
for (var i = 0; i < pipelineCount; i++) {
writeRequest(this)
}
var responses = ''
var resRe = /HTTP\/1.1/g
socket.on('data', function (chunk) {
responses += chunk
if (responses.match(resRe).length === pipelineCount) {
socket.end()
server.close()
}
})
})
})
})
})

describe('when CONNECT method', function () {
it('should fire when request finishes', function (done) {
var client
Expand Down Expand Up @@ -1031,21 +1059,18 @@ describe('isFinished(req)', function () {
})
})

function captureStderr (fn) {
function captureStderr () {
var chunks = []
var write = process.stderr.write

process.stderr.write = function write (chunk, encoding) {
chunks.push(new Buffer(chunk, encoding)) // eslint-disable-line node/no-deprecated-api
chunks.push(Buffer.from ? Buffer.from(chunk, encoding) : new Buffer(chunk, encoding)) // eslint-disable-line node/no-deprecated-api
}

try {
fn()
} finally {
return function restore () {
process.stderr.write = write
return Buffer.concat(chunks).toString('utf8')
}

return Buffer.concat(chunks).toString('utf8')
}

function close (server, callback) {
Expand Down