Skip to content

Commit

Permalink
Fix issue: reconnection only happends for 1 time after connection drops
Browse files Browse the repository at this point in the history
Since the PR #125 fixed duplicate connections after reconnection by using a `connectionInProgress` lock to avoid function `connect()` be called duplicately.

But it forgot to release the `connectionInProgress` lock when request error happends, in that case, our client can only retry for 1 time and never get the lock again.

So it's needed to release the `connectionInProgress` lock when error happends.

Signed-off-by: icy_fish <keith519@qq.com>
  • Loading branch information
icy-fish committed May 24, 2020
1 parent 61e1b19 commit 9a4190f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ function EventSource (url, eventSourceInitDict) {
})

req.on('error', function (err) {
self.connectionInProgress = false
onConnectionClosed(err.message)
})

Expand Down
28 changes: 28 additions & 0 deletions test/eventsource_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,34 @@ describe('Reconnection', function () {
}
})

it('continuing attempts when server is down', function (done) {
// Seems set reconnectInterval=0 not work here, this makes total time spent for current case more than 3S
this.timeout(4000)

var es = new EventSource('http://localhost:' + _port++)
es.reconnectInterval = 0
var reconnectCount = 0

es.onerror = function () {
reconnectCount++
// make sure client is keeping reconnecting
if (reconnectCount > 2) {
es.onerror = null
var port = u.parse(es.url).port
configureServer(http.createServer(), 'http', port, function (err, server) {
if (err) return done(err)

server.on('request', writeEvents(['data: hello\n\n']))

es.onmessage = function (m) {
assert.equal('hello', m.data)
server.close(done)
}
})
}
}
})

it('is attempted when server goes down after connection', function (done) {
createServer(function (err, server) {
if (err) return done(err)
Expand Down

0 comments on commit 9a4190f

Please sign in to comment.