Permalink
Browse files

Create one UDP socket and re-use it

  • Loading branch information...
1 parent 9fe5820 commit f008117955ef5dac4c16245d7034693eb42c277c @tmm1 tmm1 committed Aug 3, 2011
Showing with 2 additions and 4 deletions.
  1. +2 −4 lib/statsd.js
View
@@ -1,5 +1,5 @@
var sys = require('sys')
- , dgram = require('dgram')
+ , socket = require('dgram').createSocket('udp4')
, mersenne = require('mersenne')
, mt = new mersenne.MersenneTwister19937();
@@ -59,15 +59,13 @@ Client.prototype.send = function (data, sample_rate) {
sampled_data=data;
}
for (stat in sampled_data) {
- var client = dgram.createSocket('udp4');
send_data = stat+":"+sampled_data[stat];
send_data = new Buffer(send_data);
- client.send(send_data, 0, send_data.length, self.port, self.host,
+ socket.send(send_data, 0, send_data.length, self.port, self.host,
function (err, bytes) {
if (err) {
console.log(err.msg);
}
- client.close();
}
);
}

1 comment on commit f008117

@sivy
sivy commented on f008117 Aug 3, 2011

This is a much better way to handle this. Can you submit a pull req and I'll be glad to merge it in.

Please sign in to comment.