Comparing version 6.0.1 to 6.1.0
@@ -91,3 +91,3 @@ module.exports = { | ||
], | ||
"lines-around-comment": "error", | ||
"lines-around-comment": "off", | ||
"lines-around-directive": "error", | ||
@@ -108,3 +108,3 @@ "lines-between-class-members": [ | ||
"multiline-comment-style": [ | ||
"error", | ||
"off", | ||
"separate-lines" | ||
@@ -209,3 +209,3 @@ ], | ||
"no-void": "error", | ||
"no-warning-comments": "error", | ||
"no-warning-comments": "off", | ||
"no-whitespace-before-property": "error", | ||
@@ -212,0 +212,0 @@ "no-with": "error", |
CHANGELOG | ||
========= | ||
## 6.1.0 (2019-1-5) | ||
* @bdeitte Ensure close() call always sends data before closing | ||
* @bdeitte Recommend errorHandler over client.socket.on() for handling | ||
errors | ||
*@mbellerose Fix the timer function type definition | ||
## 6.0.1 (2018-12-17) | ||
@@ -5,0 +11,0 @@ * @msmnc Fix regression when tag value is a number |
@@ -123,2 +123,3 @@ const dgram = require('dgram'), | ||
this.messagesInFlight = 0; | ||
this.CHECKS = { | ||
@@ -211,3 +212,2 @@ OK: 0, | ||
let message = `${this.prefix + stat + this.suffix}:${value}|${type}`; | ||
sampleRate = sampleRate || this.sampleRate; | ||
@@ -315,4 +315,3 @@ if (sampleRate && sampleRate < 1) { | ||
Client.prototype.sendMessage = function (message, callback) { | ||
// Guard against 'RangeError: Offset into buffer too large' in node 0.10 | ||
// https://github.com/nodejs/node-v0.x-archive/issues/7884 | ||
// don't waste the time if we aren't sending anything | ||
if (message === '') { | ||
@@ -329,18 +328,29 @@ if (callback) { | ||
const handleCallback = (err) => { | ||
this.messagesInFlight--; | ||
const errFormatted = err ? new Error(`Error sending hot-shots message: ${err}`) : null; | ||
if (errFormatted) { | ||
errFormatted.code = err.code; | ||
} | ||
if (callback) { | ||
callback(errFormatted); | ||
} else if (this.errorHandler) { | ||
this.errorHandler(errFormatted); | ||
} else if (errFormatted) { | ||
console.log(String(errFormatted)); | ||
// emit error ourselves on the socket for backwards compatibility | ||
this.socket.emit('error', errFormatted); | ||
} | ||
}; | ||
const buf = Buffer.from(message); | ||
try { | ||
this.messagesInFlight++; | ||
if (this.protocol === 'tcp') { | ||
this.socket.write(buf, 'ascii', callback); | ||
this.socket.write(buf, 'ascii', handleCallback); | ||
} else { | ||
this.socket.send(buf, 0, buf.length, this.port, this.host, callback); | ||
this.socket.send(buf, 0, buf.length, this.port, this.host, handleCallback); | ||
} | ||
} catch (err) { | ||
const errMessage = `Error sending hot-shots message: ${err}`; | ||
if (callback) { | ||
callback(new Error(errMessage)); | ||
} else if (this.errorHandler) { | ||
this.errorHandler(new Error(errMessage)); | ||
} else { | ||
console.log(errMessage); | ||
} | ||
handleCallback(err); | ||
} | ||
@@ -360,2 +370,3 @@ }; | ||
Client.prototype.close = function (callback) { | ||
// stop trying to flush the queue on an interval | ||
if (this.intervalHandle) { | ||
@@ -365,4 +376,35 @@ clearInterval(this.intervalHandle); | ||
this.flushQueue(); | ||
// flush the queue one last time, if needed | ||
this.flushQueue((err) => { | ||
if (err) { | ||
if (callback) { | ||
return callback(err); | ||
} | ||
else { | ||
return console.log(err); | ||
} | ||
} | ||
// FIXME: we have entered callback hell, and this whole file is in need of an async rework | ||
// wait until there are no more messages in flight before really closing the socket | ||
let intervalAttempts = 0; | ||
const waitForMessages = setInterval(() => { | ||
intervalAttempts++; | ||
if (intervalAttempts > 10) { | ||
console.log('hot-shots could not clear out messages in flight but closing anyways'); | ||
this.messagesInFlight = 0; | ||
} | ||
if (this.messagesInFlight <= 0) { | ||
clearInterval(waitForMessages); | ||
this._close(callback); | ||
} | ||
}, 50); | ||
}); | ||
}; | ||
/** | ||
* Really close the socket and handle any errors related to it | ||
*/ | ||
Client.prototype._close = function (callback) { | ||
// error function to use in callback and catch below | ||
@@ -369,0 +411,0 @@ let handledError = false; |
{ | ||
"name": "hot-shots", | ||
"description": "Node.js client for StatsD, DogStatsD, and Telegraf", | ||
"version": "6.0.1", | ||
"version": "6.1.0", | ||
"author": "Steve Ivy", | ||
@@ -6,0 +6,0 @@ "types": "./types.d.ts", |
@@ -87,11 +87,4 @@ # hot-shots | ||
var StatsD = require('hot-shots'), | ||
client = new StatsD({ port: 8020, globalTags: { env: | ||
process.env.NODE_ENV }); | ||
client = new StatsD({ port: 8020, globalTags: { env: process.env.NODE_ENV, errorHandler: errorHandler }); | ||
// Catch socket errors so they don't go unhandled, as explained | ||
// in the Errors section below | ||
client.socket.on('error', function(error) { | ||
console.error("Error in socket: ", error); | ||
}); | ||
// Timing: sends a timing command with the specified milliseconds | ||
@@ -204,5 +197,9 @@ client.timing('response_time', 42); | ||
If the optional callback is not given, an error is thrown in some cases and a console.log message is used in others. An error will only be thrown when there is a missing callback if it is some potential configuration issue to be fixed. | ||
If the optional callback is not given, an error is thrown in some | ||
cases and a console.log message is used in others. An error will only | ||
be explitly thrown when there is a missing callback or if it is some potential configuration issue to be fixed. | ||
In the event that there is a socket error, `hot-shots` will allow this error to bubble up unless an `errorHandler` is specified. If you would like to catch the errors, either specify an `errorHandler` in your root client or just attach a listener to the socket property on the instance. | ||
If you would like ensure all errors are caught, specify an `errorHandler` in your root | ||
client. This will catch errors in socket setup, sending of messages, | ||
and closing of the socket. If you specify an errorHandler and a callback, the callback will take precedence. | ||
@@ -218,9 +215,2 @@ ```javascript | ||
```javascript | ||
// Attaching an error handler to client's socket | ||
client.socket.on('error', function(error) { | ||
console.error("Error in socket: ", error); | ||
}); | ||
``` | ||
## Submitting changes | ||
@@ -227,0 +217,0 @@ |
@@ -86,6 +86,6 @@ import dgram = require("dgram"); | ||
timer(func: (...args: any[]) => any, stat: string | string[], value: number, sampleRate?: number, tags?: Tags, callback?: StatsCb): (...args: any[]) => any; | ||
timer(func: (...args: any[]) => any, stat: string | string[], value: number, tags?: Tags, callback?: StatsCb): (...args: any[]) => any; | ||
timer(func: (...args: any[]) => any, stat: string | string[], value: number, callback?: StatsCb): (...args: any[]) => any; | ||
timer(func: (...args: any[]) => any, stat: string | string[], value: number, sampleRate?: number, callback?: StatsCb): (...args: any[]) => any; | ||
timer(func: (...args: any[]) => any, stat: string | string[], sampleRate?: number, tags?: Tags, callback?: StatsCb): (...args: any[]) => any; | ||
timer(func: (...args: any[]) => any, stat: string | string[], tags?: Tags, callback?: StatsCb): (...args: any[]) => any; | ||
timer(func: (...args: any[]) => any, stat: string | string[], callback?: StatsCb): (...args: any[]) => any; | ||
timer(func: (...args: any[]) => any, stat: string | string[], sampleRate?: number, callback?: StatsCb): (...args: any[]) => any; | ||
@@ -92,0 +92,0 @@ asyncTimer<T>(func: (...args: any[]) => Promise<T>, stat: string | string[], sampleRate?: number, tags?: Tags, callback?: StatsCb): (...args: any[]) => Promise<T>; |
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
63533
1229
237