Permalink
Browse files

Flush when the batch size exceeds 8kb

  • Loading branch information...
mojodna committed Mar 6, 2012
1 parent a1ca8d3 commit 2782b6df5ef32d81dd9e35647df9ca7faeb1daa7
Showing with 44 additions and 19 deletions.
  1. +8 −0 lib/metricsd.js
  2. +36 −19 test/index.js
View
@@ -248,6 +248,14 @@ module.exports = function(options) {
if (this.batch) {
batchedMetrics.push(metric);
+
+ // flush if there are already enough pending metrics to be
+ // problematic
+ // 8kb is an arbitrary size determined after observing that UDP
+ // sends begin failing around 9200 bytes (on Lion)
+ if (Buffer.byteLength(batchedMetrics.join()) >= 8164) {
+ flushBatch();
+ }
} else {
API._send(metric);
}
View
@@ -314,12 +314,34 @@ describe("metrics", function() {
});
describe("#write", function() {
+ var port = 1234;
+ var sink;
+
+ beforeEach(function(done) {
+ metrics = metricsd({
+ port: port
+ });
+
+ sink = require("dgram").createSocket("udp4");
+
+ sink.once("listening", done);
+
+ sink.bind(port);
+ });
+
+ afterEach(function(done) {
+ sink.once("close", done);
+
+ sink.close();
+ });
+
describe("in batched mode", function() {
var values = ["metric.name:1234|g\n", "metric.name2:2345|h\n"];
beforeEach(function() {
metrics = metricsd({
- batch: true
+ batch: true,
+ port: port
});
});
@@ -366,30 +388,25 @@ describe("metrics", function() {
metrics.write(x);
});
});
- });
-
- describe("in non-batched mode", function() {
- var port = 1234;
- var sink;
- beforeEach(function(done) {
- metrics = metricsd({
- port: port
- });
-
- sink = require("dgram").createSocket("udp4");
+ it("should flush metrics in small enough batches that they actually get sent", function(done) {
+ var values = [];
- sink.once("listening", done);
+ for (var i = 0; i < 1000; i++) {
+ values.push("prefixed.metric" + i + ".value:12345|g\n");
+ }
- sink.bind(port);
- });
-
- afterEach(function(done) {
- sink.once("close", done);
+ sink.once("message", function(msg, rinfo) {
+ done();
+ });
- sink.close();
+ values.forEach(function(x) {
+ metrics.write(x);
+ });
});
+ });
+ describe("in non-batched mode", function() {
it("should write a metricsd string to the network", function(done) {
var metric = "prefix.metric.name:1234|g\n";

0 comments on commit 2782b6d

Please sign in to comment.