Skip to content

Commit

Permalink
Merge pull request #31 from thekemkid/loop
Browse files Browse the repository at this point in the history
Fix loop keeping event loop alive
  • Loading branch information
mcollina committed Jun 28, 2016
2 parents 8e86ef3 + a1726e1 commit 0e66882
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
18 changes: 13 additions & 5 deletions lib/myhttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ function Client (opts) {

// cer = current expected response
this.cer = 0
this.destroyed = false

this.timeoutTicker = retimer(() => {
const handleTimeout = () => {
// all pipelined requests have timed out here
this.resData.forEach(() => this.emit('timeout'))
this.cer = 0
this._destroyConnection()
this._connect()
}, this.timeout)

// timeout has already occured, need to set a new timeoutTicker
this.timeoutTicker = retimer(handleTimeout, this.timeout)
}

this.timeoutTicker = retimer(handleTimeout, this.timeout)

this.parser[HTTPParser.kOnHeadersComplete] = (opts) => {
this.resData[this.cer].headers = opts
Expand All @@ -65,6 +71,7 @@ function Client (opts) {

this.cer = this.cer === opts.pipelining - 1 ? 0 : this.cer++
this._doRequest(this.cer)

this.timeoutTicker.reschedule(this.timeout)
}

Expand Down Expand Up @@ -95,7 +102,7 @@ Client.prototype._connect = function () {

this.conn.on('error', () => {
this.emit('connError')
this._connect()
if (!this.destroyed) this._connect()
})

this.conn.on('data', (chunk) => {
Expand All @@ -104,7 +111,7 @@ Client.prototype._connect = function () {
})

this.conn.on('end', () => {
this._connect()
if (!this.destroyed) this._connect()
})

for (let i = 0; i < this.opts.pipelining; i++) {
Expand All @@ -126,8 +133,9 @@ Client.prototype._destroyConnection = function () {
}

Client.prototype.destroy = function () {
this._destroyConnection()
this.destroyed = true
this.timeoutTicker.clear()
this._destroyConnection()
}

Client.prototype.setHeaders = function (newHeaders) {
Expand Down
1 change: 1 addition & 0 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function run (opts, cb) {
'non2xx': statusCodes[0] + statusCodes[2] + statusCodes[3] + statusCodes[4]
}
tracker.emit('done', result)

cb(null, result)
}
}, 1000)
Expand Down
18 changes: 15 additions & 3 deletions test/myhttp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,19 @@ test('client should emit a timeout when no response is received', (t) => {
t.ok(1, 'timeout should have happened')
})

setTimeout(() => {
client.destroy()
}, 1500)
setTimeout(() => client.destroy(), 1500)
})

test('client should emit 2 timeouts when no responses are received', (t) => {
t.plan(2)

const opts = timeoutServer.address()
opts.timeout = 1
const client = new Client(opts)

client.on('timeout', () => {
t.ok(1, 'timeout should have happened')
})

setTimeout(() => client.destroy(), 2500)
})

0 comments on commit 0e66882

Please sign in to comment.