Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: sivy/node-statsd
base: master
...
head fork: goodeggs/node-statsd
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 2 files changed
  • 0 commit comments
  • 3 contributors
Commits on May 16, 2012
@mojodna mojodna Default to ephemeral sockets
The use of a single, shared socket that's never closed prevents node processes
from completing normally.

This change allows an optional shared socket to be passed into the constructor.
If present, node-statsd will use that (and will assume that the provider will
close it when appropriate).  Otherwise, node-statsd will create new sockets for
each connection (the old, less efficient behavior).
4157651
@lloyd lloyd lazily close ephemerally allocated UDP ports to share ephemeral ports…
… between multiple requests that are temporally close. prevents massive allocation of UDP ports when under load.
f0d67e6
@lloyd lloyd allow client to explicitly close() statsd shutting down all resources…
… that would keep a process from exiting.
20a0f16
Commits on May 21, 2012
@bobzoller bobzoller fix typo (cancelTimeout -> clearTimeout) 54ee181
@bobzoller bobzoller bump version f610957
Showing with 67 additions and 11 deletions.
  1. +66 −10 lib/statsd.js
  2. +1 −1  package.json
View
76 lib/statsd.js
@@ -1,10 +1,21 @@
-var socket = require('dgram').createSocket('udp4')
+var dgram = require('dgram')
, mersenne = require('mersenne')
, mt = new mersenne.MersenneTwister19937();
-Client = function (host, port) {
+const EPHEMERAL_LIFETIME_MS = 1000;
+
+Client = function (host, port, socket) {
this.host = host;
this.port = port;
+
+ // optional shared socket
+ this.socket = socket;
+
+ // when a *shared* socked isn't provided, an ephemeral
+ // socket is demand allocated. This ephemeral socket is closed
+ // after being idle for EPHEMERAL_LIFETIME_MS.
+ this.ephemeral_socket = undefined;
+ this.last_used_timer = undefined;
}
Client.prototype.timing = function (stat, time, sample_rate) {
@@ -46,6 +57,43 @@ Client.prototype.update_stats = function (stats, delta, sampleRate) {
self.send(data, sampleRate);
}
+// An internal function update the last time the socket was
+// used. This function is called when the socket is used
+// and causes demand allocated ephemeral sockets to be closed
+// after a period of inactivity.
+Client.prototype._update_last_used = function () {
+ if (this.ephemeral_socket) {
+ if (this.last_used_timer) clearTimeout(this.last_used_timer);
+ var self = this;
+ this.last_used_timer = setTimeout(function() {
+ if (self.ephemeral_socket) self.ephemeral_socket.close();
+ delete self.ephemeral_socket;
+ }, EPHEMERAL_LIFETIME_MS);
+ }
+};
+
+Client.prototype.send_data = function (buffer) {
+ var self = this;
+ var socket;
+
+ if (this.socket === undefined) {
+ if (!this.ephemeral_socket) {
+ this.ephemeral_socket = dgram.createSocket('udp4');
+ }
+ socket = this.ephemeral_socket;
+ } else {
+ socket = this.socket;
+ }
+
+ this._update_last_used();
+
+ socket.send(buffer, 0, buffer.length, this.port, this.host, function (err, bytes) {
+ if (err) {
+ console.log("Error while sending data:", err.msg);
+ }
+ });
+}
+
Client.prototype.send = function (data, sample_rate) {
var self = this;
if (!sample_rate) {
@@ -66,14 +114,22 @@ Client.prototype.send = function (data, sample_rate) {
}
for (var stat in sampled_data) {
var send_data = stat+":"+sampled_data[stat];
- send_data = new Buffer(send_data);
- socket.send(send_data, 0, send_data.length, self.port, self.host,
- function (err, bytes) {
- if (err) {
- console.log(err.msg);
- }
- }
- );
+ this.send_data(new Buffer(send_data));
+ }
+};
+
+Client.prototype.close = function () {
+ if (this.socket) {
+ this.socket.close();
+ this.socket = undefined;
+ }
+ if (this.ephemeral_socket) {
+ this.ephemeral_socket.close();
+ this.ephemeral_socket = undefined;
+ }
+ if (this.last_used_timer) {
+ clearTimeout(this.last_used_timer);
+ this.last_used_timer = undefined;
}
};
View
2  package.json
@@ -1,6 +1,6 @@
{ "name" : "node-statsd"
, "description" : "node client for Etsy'd StatsD server"
-, "version" : "0.0.2"
+, "version" : "0.0.2-1"
, "author" : "Steve Ivy"
, "repository" :
{ "type" : "git"

No commit comments for this range

Something went wrong with that request. Please try again.