Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

statsd-client

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

statsd-client - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

test/EphemeralSocket.js

108

lib/EphemeralSocket.js

@@ -5,3 +5,3 @@ var dgram = require('dgram');

function ephemeralSocket(options) {
function EphemeralSocket(options) {
this.options = options || {};

@@ -16,4 +16,4 @@

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
this._socketUsed = false; // Flag if it has been used
this._socketTimer = undefined; // Reference to check-timer
}

@@ -25,3 +25,3 @@

*/
ephemeralSocket.prototype._socket_timeout = function () {
EphemeralSocket.prototype._socket_timeout = function () {
// Is it already closed? -- then stop here

@@ -33,5 +33,4 @@ if (!this._socket) {

// Has been used? -- reset use-flag and wait some more
if (this._socket_used) {
this._socket_used = false;
this._socket_timer = setTimeout(this._socket_timeout.bind(this), this.options.socket_timeout);
if (this._socketUsed) {
this._socketUsed = false;
return;

@@ -48,3 +47,3 @@ }

*/
ephemeralSocket.prototype.close = function () {
EphemeralSocket.prototype.close = function () {
if (!this._socket) {

@@ -55,3 +54,3 @@ return;

// Cancel the running timer
clearTimeout(this._socket_timer);
clearInterval(this._socketTimer);

@@ -66,52 +65,24 @@ // Wait a tick or two, so any remaining stats can be sent.

/*
* Enqueue data to be sent when the socket is created
*/
ephemeralSocket.prototype._enqueue_data = function (data) {
// Make sure our array exists
this._enqueued_data = (this._enqueued_data || []);
this._enqueued_data.push(data);
};
EphemeralSocket.prototype._createSocket = function (callback) {
var that = this;
if (this._socket) {
return callback();
}
/*
* Callback called when socket is created.
* When socket is ready, send all enqueued data, and mark as ready.
*/
ephemeralSocket.prototype._on_socket_ready = function () {
var self = this;
this._socket_ready = true;
this._socket = dgram.createSocket('udp4');
// Exit early if we have no data enqueued
if (!this._enqueued_data || this._enqueued_data.length === 0) {
return;
}
// Listen on 'error'-events, so they don't bubble up to the main
// application. Try closing the socket for now, forcing it to be re-created
// later.
this._socket.once('error', this.close.bind(this));
this._enqueued_data.forEach(function (datum) {
self.send(datum);
// Call on when the socket is ready.
this._socket.once('listening', function () {
return callback();
});
this._enqueued_data = [];
};
this._socket.bind(0, null);
ephemeralSocket.prototype._create_socket = function () {
var that = this;
if (!this._socket) {
this._socket = dgram.createSocket('udp4');
// Call on when the socket is ready.
/*
this._socket.on('listening', function () {
that._on_socket_ready();
});
this._socket.bind();
*/
// Assume the socket is ready after a few ticks
setTimeout(function () {
that._on_socket_ready();
}, 10);
// 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);
}
// Start timer, if we have a positive timeout
if (this.options.socket_timeout > 0) {
this._socketTimer = setInterval(this._socket_timeout.bind(this), this.options.socket_timeout);
}

@@ -123,26 +94,21 @@ };

*/
ephemeralSocket.prototype.send = function (data) {
EphemeralSocket.prototype.send = function (data) {
// If we don't have a socket, or we have created one but it isn't
// ready yet, we need to enqueue data to send once the socket is ready.
if (!this._socket || !this._socket_ready) {
this._enqueue_data(data);
var that = this;
if (!this._socket) {
this._create_socket();
}
return;
}
this._socket_used = true;
this._createSocket(function () {
that._socketUsed = true;
// Create message
var message = new Buffer(data);
// Create message
var message = new Buffer(data);
if (this.options.debug) {
console.warn(message.toString());
}
if (that.options.debug) {
console.warn(message.toString());
}
this._socket.send(message, 0, message.length, this.options.port, this.options.host);
that._socket.send(message, 0, message.length, that.options.port, that.options.host);
});
};
module.exports = ephemeralSocket;
module.exports = EphemeralSocket;

@@ -6,3 +6,3 @@ {

"keywords": ["statsd", "client", "metrics", "udp"],
"version": "0.0.6",
"version": "0.0.7",
"homepage": "https://github.com/msiebuhr/node-statsd-client",

@@ -9,0 +9,0 @@ "bugs": "https://github.com/msiebuhr/node-statsd-client/issues",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc