Skip to content

Commit

Permalink
Work-around node cluster bugs with dgram.send()
Browse files Browse the repository at this point in the history
See nodejs/node-v0.x-archive#8027, work-around is to create a single dgram socket,
and to pass it to Lynx.
  • Loading branch information
sam-github committed Jul 31, 2014
1 parent c4a250c commit 6dd43cb
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var debug = require('debug')('strong-agent-statsd');
var dgram = require('dgram');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Lynx = require('lynx');
Expand All @@ -18,9 +19,30 @@ module.exports = function (options) {
};

function Publisher(port, host, scope) {
var self = this;

this.port = port;
this.host = host;
this.scope = scope;

// Lynx creates a new UDP socket if it isn't used for 1 second. This has
// two problems:
// 1. It forces sockets to be created every time we use one.
// 2. It tickles a bug in v0.11 cluster causing the master to assert.
// Work around this by creating the socket once, which Lynx supports.
// Also, emulate Lynx's error handling, which it does only for its own
// created sockets.
this.socket = dgram.createSocket('udp4');
this.socket.unref();
this.socket.on('error', function (err) {
err.reason = err.message;
err.f = 'send';
err.message = 'Failed sending the buffer';
err.args = arguments;
self.onError(err);
return;
});

this.stats = new Lynx(this.host, this.port, {
scope: this.scope,
on_error: this.onError.bind(this),
Expand Down

0 comments on commit 6dd43cb

Please sign in to comment.