Comparing version 6.6.0 to 6.7.0
CHANGELOG | ||
========= | ||
## 6.6.0 (2019-10-7 | ||
## 6.7.0 (2019-10-9) | ||
* @runk Code refactoring to have abstract transports | ||
## 6.6.0 (2019-10-7) | ||
* @NinjaBanjo @msiebuhr Add udsGracefulErrorHandling, ensuring uds | ||
@@ -6,0 +9,0 @@ handles socket errors gracefully |
@@ -1,16 +0,9 @@ | ||
const dgram = require('dgram'), | ||
util = require('util'), | ||
const util = require('util'), | ||
dns = require('dns'), | ||
net = require('net'), | ||
helpers = require('./helpers'), | ||
applyStatsFns = require('./statsFunctions'); | ||
// installed below, only if needed | ||
let unixDgram; | ||
const { PROTOCOL } = require('./constants'); | ||
const createTransport = require('./transport'); | ||
const TCP = 'tcp'; | ||
const UDS = 'uds'; | ||
// UDP constant not needed/used, defaults to udp | ||
const UDS_PATH_DEFAULT = '/var/run/datadog/dsd.socket'; | ||
const UDS_DEFAULT_GRACEFUL_RESTART_LIMIT = 1000; | ||
@@ -47,50 +40,3 @@ | ||
const createSocket = function createSocket(instance, args) { | ||
let socket; | ||
let errMessage; | ||
if (args.protocol === TCP) { | ||
try { | ||
socket = net.connect(args.port, args.host); | ||
socket.setKeepAlive(true); | ||
} catch (err) { | ||
errMessage = `Could not establish connection to ${args.host}:${args.port}: ${err}`; | ||
} | ||
} else if (args.protocol === UDS) { | ||
try { | ||
// this will not always be available, as noted in the error message below | ||
unixDgram = require('unix-dgram'); // eslint-disable-line global-require | ||
} | ||
catch (err) { | ||
errMessage = 'The library unix_dgram, needed for the uds protocol to work, is not installed. You need to pick another protocol to use hot-shots. See the hot-shots README for additional details.'; | ||
} | ||
if (unixDgram) { | ||
const udsPath = args.path ? args.path : UDS_PATH_DEFAULT; | ||
try { | ||
socket = unixDgram.createSocket('unix_dgram'); | ||
socket.connect(udsPath); | ||
} catch (err) { | ||
errMessage = `Could not establish UDS connection to ${udsPath}: ${err}`; | ||
} | ||
} | ||
} else { | ||
try { | ||
socket = dgram.createSocket('udp4'); | ||
} catch (err) { | ||
errMessage = `Could not create socket: ${err}`; | ||
} | ||
} | ||
if (errMessage) { | ||
if (instance.errorHandler) { | ||
instance.errorHandler(new Error(errMessage)); | ||
} else { | ||
console.error(errMessage); | ||
} | ||
} | ||
return socket; | ||
}; | ||
// hidden global_tags option for backwards compatibility | ||
@@ -104,3 +50,3 @@ options.globalTags = options.globalTags || options.global_tags; | ||
this.suffix = options.suffix || ''; | ||
this.socket = options.isChild ? options.socket : createSocket(this, { | ||
this.socket = options.isChild ? options.socket : createTransport(this, { | ||
host: this.host, | ||
@@ -165,3 +111,3 @@ path: options.path, | ||
// does not support options.isChild (how to re-create a socket you didn't create?) | ||
if (!options.isChild && options.protocol === 'uds' && options.udsGracefulErrorHandling) { | ||
if (!options.isChild && options.protocol === PROTOCOL.UDS && options.udsGracefulErrorHandling) { | ||
const socketCreateLimit = options.udsGracefulRestartRateLimit || UDS_DEFAULT_GRACEFUL_RESTART_LIMIT; // only recreate once per second | ||
@@ -180,3 +126,3 @@ const lastSocketCreateTime = Date.now(); | ||
this.socket.close(); | ||
this.socket = createSocket(this, { | ||
this.socket = createTransport(this, { | ||
host: this.host, | ||
@@ -409,4 +355,4 @@ path: options.path, | ||
if (this.protocol === TCP && message.lastIndexOf('\n') !== message.length - 1) { | ||
message += '\n'; | ||
if (!this.socket) { | ||
return callback(new Error('No protocol set. Check previous errors for details.')); | ||
} | ||
@@ -426,3 +372,3 @@ | ||
} else { | ||
console.log(String(errFormatted)); | ||
console.error(String(errFormatted)); | ||
// emit error ourselves on the socket for backwards compatibility | ||
@@ -434,12 +380,5 @@ this.socket.emit('error', errFormatted); | ||
const buf = Buffer.from(message); | ||
try { | ||
this.messagesInFlight++; | ||
if (this.protocol === TCP) { | ||
this.socket.write(buf, 'ascii', handleCallback); | ||
} else if (this.protocol === UDS) { | ||
this.socket.send(buf, handleCallback); | ||
} else { | ||
this.socket.send(buf, 0, buf.length, this.port, this.host, handleCallback); | ||
} | ||
this.socket.send(Buffer.from(message), handleCallback); | ||
} catch (err) { | ||
@@ -537,15 +476,3 @@ handleCallback(err); | ||
try { | ||
if (this.protocol === TCP) { | ||
this.socket.destroy(); | ||
} else if (this.protocol === UDS) { | ||
this.socket.close(); | ||
// this close is synchronous, and the socket will not emit a | ||
// close event, so we can callback right away | ||
if (callback) { | ||
callback(); | ||
} | ||
} | ||
else { | ||
this.socket.close(); | ||
} | ||
this.socket.close(); | ||
} catch (err) { | ||
@@ -552,0 +479,0 @@ handleErr(err); |
{ | ||
"name": "hot-shots", | ||
"description": "Node.js client for StatsD, DogStatsD, and Telegraf", | ||
"version": "6.6.0", | ||
"version": "6.7.0", | ||
"author": "Steve Ivy", | ||
@@ -6,0 +6,0 @@ "types": "./types.d.ts", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71235
12
1357