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

Emit a reconnectFailure event #81

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 6 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ pino(transport)

### Events

| Name | Callback Signature | Description |
|---------------|----------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| `open` | `(address: AddressInfo) => void` | Emitted when the TCP or UDP connection is established. |
| `socketError` | `(error: Error) => void` | Emitted when an error occurs on the TCP or UDP socket. The socket won't be closed. |
| `close` | `(hadError: Boolean) => void` | Emitted after the TCP or UDP socket is closed. The argument `hadError` is a boolean which says if the socket was closed due to a transmission error. |
| Name | Callback Signature | Description |
|--------------------|-----------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| `open` | `(address: AddressInfo) => void` | Emitted when the TCP or UDP connection is established. |
| `socketError` | `(error: Error) => void` | Emitted when an error occurs on the TCP or UDP socket. The socket won't be closed. |
| `close` | `(hadError: Boolean) => void` | Emitted after the TCP or UDP socket is closed. The argument `hadError` is a boolean which says if the socket was closed due to a transmission error. |
| `reconnectFailure` | `(error: Error|undefined) => void` | Emitted when the maximum number of backoffs (i.e., reconnect tries) is reached on a TCP connection. |

**IMPORTANT:** In version prior to 6.0, an `error` event was emitted on the writable stream when an error occurs on the TCP or UDP socket.
In other words, it was not possible to write data to the writable stream after an error occurs on the TCP or UDP socket.
Expand Down
8 changes: 4 additions & 4 deletions lib/TcpConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ module.exports = function factory (userOptions) {
}
}

function reconnect () {
retryBackoff.backoff()
function reconnect (err) {
retryBackoff.backoff(err)
}
// end: connection handlers

Expand All @@ -144,7 +144,7 @@ module.exports = function factory (userOptions) {
}
}
if (options.reconnect) {
reconnect()
reconnect(hadError && socketError)
Copy link
Contributor Author

@ggrossetie ggrossetie Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina I'm thinking about it because it's a breaking change, do you think we should reconnect if the socket was closed (or ended) without an error?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no we shouldn't reconnect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!
I've also added a test to make sure that a close event is sent on the stream when the socket is gracefully closed using socket.destroy() (without an error)

} else {
outputStream.emit('close', hadError)
}
Expand Down Expand Up @@ -218,7 +218,7 @@ module.exports = function factory (userOptions) {
}
})
})
retry.on('fail', (err) => process.stderr.write(`could not reconnect: ${err.message}`))
retry.on('fail', (err) => outputStream.emit('reconnectFailure', err))
return retry
}

Expand Down
24 changes: 24 additions & 0 deletions test/tcpRetyFail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'
/* eslint-env node, mocha */

const { expect } = require('chai')
const TcpConnection = require('../lib/TcpConnection')

test('tcp retry fail', function testTcpRetryFail (done) {
let socketErrorCount = 0
const tcpConnection = TcpConnection({
address: '127.0.0.1',
port: 0,
reconnect: true,
reconnectTries: 2
}
)
tcpConnection.on('socketError', () => {
socketErrorCount++
})
tcpConnection.on('reconnectFailure', (lastError) => {
expect(socketErrorCount).to.eq(3)
expect(lastError).to.be.an('error')
done()
})
})