Skip to content
Open
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
42 changes: 18 additions & 24 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const os = require('os'),
process = require('process');
const process = require('process');

exports.PROTOCOL = {
TCP: 'tcp',
Expand All @@ -10,41 +9,36 @@ exports.PROTOCOL = {

/**
* Determines error codes that signify a connection to a TCP socket
* has failed in a way that can be retried. This codes are OS-specific.
* @returns {number[]} An array of the error codes.
* has failed in a way that can be retried. These are string error codes
* matching Node.js socket error.code values (e.g., 'EPIPE', 'ECONNRESET').
* @returns {string[]} An array of the error codes.
*/
function tcpErrors() {
function tcpErrors() {
return [
os.constants.errno.WSAENOTCONN,
os.constants.errno.WSAECONNREFUSED,
os.constants.errno.WSAECONNRESET,
os.constants.errno.WSAENOTCONN,
os.constants.errno.EDESTADDRREQ,
os.constants.errno.ECONNRESET,
os.constants.errno.EPIPE,
os.constants.errno.ENOTCONN,
os.constants.errno.ECONNREFUSED,
'WSAENOTCONN',
'WSAECONNREFUSED',
'WSAECONNRESET',
'EDESTADDRREQ',
'ECONNRESET',
'EPIPE',
'ENOTCONN',
'ECONNREFUSED',
];
}

/**
* Determines error codes that signify a connection to a Unix Domain Socket (UDS)
* has failed in a way that can be retried. This codes are OS-specific.
* @returns {number[]} An array of the error codes.
* has failed in a way that can be retried. These are string error codes
* matching Node.js socket error.code values. OS-specific.
* @returns {string[]} An array of the error codes.
*/
function udsErrors() {
if (process.platform === 'linux') {
return [
os.constants.errno.ENOTCONN,
os.constants.errno.ECONNREFUSED,
];
return ['ENOTCONN', 'ECONNREFUSED'];
}

if (process.platform === 'darwin') {
return [
os.constants.errno.EDESTADDRREQ,
os.constants.errno.ECONNRESET,
];
return ['EDESTADDRREQ', 'ECONNRESET'];
}

// Unknown / not yet implemented
Expand Down
4 changes: 2 additions & 2 deletions lib/statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,9 @@ function protocolErrorHandler(client, protocol, err) {

// recreate the socket, but only once within `tcpGracefulRestartRateLimit`/`udsGracefulRestartRateLimit`.

if (protocol === PROTOCOL.TCP && (!TCP_ERROR_CODES.includes(-err.code) || Date.now() - client.socket.createdAt < client.tcpGracefulRestartRateLimit)) {
if (protocol === PROTOCOL.TCP && (!TCP_ERROR_CODES.includes(err.code) || Date.now() - client.socket.createdAt < client.tcpGracefulRestartRateLimit)) {
return;
} else if (protocol === PROTOCOL.UDS && (!UDS_ERROR_CODES.includes(-err.code) || Date.now() - client.socket.createdAt < client.udsGracefulRestartRateLimit)) {
} else if (protocol === PROTOCOL.UDS && (!UDS_ERROR_CODES.includes(err.code) || Date.now() - client.socket.createdAt < client.udsGracefulRestartRateLimit)) {
return;
}

Expand Down
67 changes: 29 additions & 38 deletions test/errorHandling.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const assert = require('assert');
const os = require('os');
const process = require('process');
const path = require('path');
const helpers = require('./helpers/helpers.js');
Expand Down Expand Up @@ -904,88 +903,80 @@ describe('#errorHandling', () => {
});

/**
* Return system error code for a "bad connection" to a TCP (e.g. does not
* exist).
* Return the Node.js error code string for a "bad connection" to a TCP socket
* (e.g. server not accepting connections).
*
* The value is negated because of the way errors are returned, e.g. by `libuv`.
*
* - 111 (ECONNREFUSED) on Linux
* - 54 (ECONNRESET) on macOS
* - "not-implemented" on other platforms
* - 'ECONNREFUSED' on Linux
* - 'ECONNRESET' on macOS
* - 'not-implemented' on other platforms
*/
function badTCPConnectionCode() {
function badTCPConnectionCode() {
if (process.platform === 'linux') {
return -os.constants.errno.ECONNREFUSED;
return 'ECONNREFUSED';
}

if (process.platform === 'darwin') {
return -os.constants.errno.ECONNRESET;
return 'ECONNRESET';
}

return 'not-implemented';
}

/**
* Return system error code for a "bad connection" to a UDS (e.g. does not
* exist).
*
* The value is negated because of the way errors are returned, e.g. by `libuv`.
* Return the Node.js error code string for a "bad connection" to a UDS
* (e.g. server not accepting connections or peer reset).
*
* - 111 (ECONNREFUSED) on Linux
* - 54 (ECONNRESET) on macOS
* - "not-implemented" on other platforms
* - 'ECONNREFUSED' on Linux
* - 'ECONNRESET' on macOS
* - 'not-implemented' on other platforms
*/
function badUDSConnectionCode() {
if (process.platform === 'linux') {
return -os.constants.errno.ECONNREFUSED;
return 'ECONNREFUSED';
}

if (process.platform === 'darwin') {
return -os.constants.errno.ECONNRESET;
return 'ECONNRESET';
}

return 'not-implemented';
}

/**
* Return system error code for a "bad descriptor" (e.g. descriptor exists
* but server is gone).
* Return the Node.js error code string for a "bad descriptor" (e.g. descriptor
* exists but server is gone).
*
* The value is negated because of the way errors are returned, e.g. by `libuv`.
*
* - 107 (ENOTCONN) on Linux
* - 39 (EDESTADDRREQ) on macOS
* - "not-implemented" on other platforms
* - 'ENOTCONN' on Linux
* - 'EDESTADDRREQ' on macOS
* - 'not-implemented' on other platforms
*/
function badTCPDescriptorCode() {
function badTCPDescriptorCode() {
if (process.platform === 'linux') {
return -os.constants.errno.ENOTCONN;
return 'ENOTCONN';
}

if (process.platform === 'darwin') {
return -os.constants.errno.EDESTADDRREQ;
return 'EDESTADDRREQ';
}

return 'not-implemented';
}

/**
* Return system error code for a "bad descriptor" (e.g. descriptor exists
* but server is gone).
*
* The value is negated because of the way errors are returned, e.g. by `libuv`.
* but server is gone). Returns the string error code matching Node.js socket error.code.
*
* - 107 (ENOTCONN) on Linux
* - 39 (EDESTADDRREQ) on macOS
* - "not-implemented" on other platforms
* - 'ENOTCONN' on Linux
* - 'EDESTADDRREQ' on macOS
* - 'not-implemented' on other platforms
*/
function badUDSDescriptorCode() {
if (process.platform === 'linux') {
return -os.constants.errno.ENOTCONN;
return 'ENOTCONN';
}

if (process.platform === 'darwin') {
return -os.constants.errno.EDESTADDRREQ;
return 'EDESTADDRREQ';
}

return 'not-implemented';
Expand Down