statsd-client
Advanced tools
Comparing version 0.0.0 to 0.0.1
@@ -18,4 +18,5 @@ var dgram = require('dgram'); | ||
// Set up re-usable socket | ||
this.socket = undefined; | ||
this.socket_used = false; | ||
this._socket = undefined; // Store the socket here | ||
this._socket_used = false; // Flag if it has been used | ||
this._socket_timer = undefined; // Reference to check-timer | ||
}; | ||
@@ -61,5 +62,32 @@ | ||
/* | ||
* Close the socket, if in use and cancel the interval-check, if running. | ||
*/ | ||
StatsDClient.prototype.close = function () { | ||
if (!this._socket) { | ||
return; | ||
} | ||
// Cancel the running timer | ||
clearTimeout(this._socket_timer); | ||
// Wait a tick or two, so any remaining stats can be sent. | ||
var that = this; | ||
setTimeout(function () { | ||
that._socket.close(); | ||
that._socket = undefined; | ||
}, 10); | ||
} | ||
/* | ||
* PRIVATE METHODS | ||
*/ | ||
/* | ||
* Check if the socket has been used in the previous socket_timeout-interval. | ||
* If it has, we leave it open and try again later. If it hasn't, close it. | ||
*/ | ||
StatsDClient.prototype._socket_timeout = function () { | ||
// Is it already closed? -- then stop here | ||
if (!this.socket) { | ||
if (!this._socket) { | ||
return; | ||
@@ -69,5 +97,5 @@ } | ||
// Has been used? -- reset use-flag and wait some more | ||
if (this.socket_used) { | ||
this.socket_used = false; | ||
setTimeout(this._socket_timeout.bind(this), this.options.socket_timeout); | ||
if (this._socket_used) { | ||
this._socket_used = false; | ||
this._socket_timer = setTimeout(this._socket_timeout.bind(this), this.options.socket_timeout); | ||
return; | ||
@@ -77,4 +105,3 @@ } | ||
// Not used? -- close the socket | ||
this.socket.close(); | ||
this.socket = undefined; | ||
this.close(); | ||
}; | ||
@@ -87,7 +114,11 @@ | ||
// Create socket if it isn't there | ||
if (!this.socket) { | ||
this.socket = dgram.createSocket('udp4'); | ||
setTimeout(this._socket_timeout.bind(this), this.options.socket_timeout); | ||
if (!this._socket) { | ||
this._socket = dgram.createSocket('udp4'); | ||
// Start timer, if we have a positive timeout | ||
if (this.options.socket_timeout > 0) { | ||
this._socket_timer = setTimeout(this._socket_timeout.bind(this), this.options.socket_timeout); | ||
} | ||
} | ||
this.socket_used = true; | ||
this._socket_used = true; | ||
@@ -100,5 +131,5 @@ var message = new Buffer(data); | ||
this.socket.send(message, 0, message.length, this.options.port, this.host); | ||
this._socket.send(message, 0, message.length, this.options.port, this.host); | ||
}; | ||
module.exports = StatsDClient; |
{ | ||
"author": "Morten Siebuhr <sbhr@sbhr.dk>", | ||
"name": "statsd-client", | ||
"description": "Client for statsd", | ||
"version": "0.0.0", | ||
"description": "Yet another client for Etsy's statsd", | ||
"keywords": ["statsd", "client", "metrics", "udp"], | ||
"version": "0.0.1", | ||
"homepage": "https://github.com/msiebuhr/node-statsd-client", | ||
"bugs": "https://github.com/msiebuhr/node-statsd-client/issues", | ||
"repository": { | ||
@@ -8,0 +10,0 @@ "type": "git", |
@@ -15,3 +15,3 @@ node-statsd-client | ||
default), `port` (default 8125) and `socket_timeout` for when an unused socket | ||
is closed (default 1000 ms), ex: | ||
is closed (default 1000 ms; set to `0` to disable it), ex: | ||
@@ -48,2 +48,17 @@ var SDC = require('statsd-client'), | ||
### Stopping | ||
By default, the socket is closed if it hasn't been used for a second (the `socket_timeout` in the init-options), but it can also be force-closed with `.close()`: | ||
var start = new Date(); | ||
setTimeout(function () { | ||
sdc.timing('random.timeout', start); // 2 - implicitly re-creates socket. | ||
sdc.close(); // 3 - Closes socket after last use. | ||
}, 100 * Math.random()); | ||
sdc.close(); // 1 - Closes socket early. | ||
The call is idempotent, so you can call it "just to be sure". And if you submit | ||
new metrics later, the socket will automatically be re-created, and a new | ||
timeout-timer started. | ||
What's broken | ||
@@ -50,0 +65,0 @@ ------------- |
@@ -9,3 +9,4 @@ var sdc = require('./lib/statsd-client'), | ||
SDC.timing('statsd-client.speed', begin); | ||
SDC.close(); | ||
}, 100 * Math.random()); | ||
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
7127
116
72