[Pkg-javascript-commits] [node-ain2] 70/102: Fixed high-concurrency crashes due to opening too many sockets at the same time, breaking the maximum limit per process

Jonas Smedegaard js at moszumanska.debian.org
Tue Apr 29 11:59:50 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository node-ain2.

commit 8ff4312017d17c4614c0a8d3ff523d18b4b6f08f
Author: Carlos Lage <carlos.lage at livestream.com>
Date:   Wed Sep 19 14:29:47 2012 -0400

    Fixed high-concurrency crashes due to opening too many sockets at the same time, breaking the maximum limit per process
---
 index.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)

diff --git a/index.js b/index.js
index 04ce270..ab6129c 100644
--- a/index.js
+++ b/index.js
@@ -7,19 +7,63 @@ var DefaultAddress = "127.0.0.1";
 var SingletonInstance = null;
 
 
+ var socket
+   , socketUsers = 0
+   , releaseTimeout
+   , socketErrorHandler = function (err) {
+         if (err) {
+             nodeConsole.error('socket error: ' + err)    
+         } else {
+             nodeConsole.error('unknown socket error!')
+         }
+         
+         if (socket !== undefined) {
+             socket.close()
+             socket = undefined
+             socketUsers = 0                        
+         }
+     }
+   , getSocket = function () {
+         if (undefined === socket) {
+             socket = dgram.createSocket('udp4')
+             socket.on('error', socketErrorHandler)
+         }
+         ++socketUsers
+         //console.log(socketUsers)
+         return socket
+     }
+   , releaseSocket = function () {
+         --socketUsers
+         //console.log(socketUsers)
+         if (0 == socketUsers && undefined === releaseTimeout) {
+             releaseTimeout = setTimeout(function () {
+                 if (0 == socketUsers && socket !== undefined) {
+                     //console.log('closing socket!')
+                     socket.close()
+                     socket = undefined
+                 } /*else {
+                     console.log('another user attached while waiting to close the socket')
+                 }*/
+                 releaseTimeout = undefined
+             }, 1000)
+         }
+     }
+
+
+
 var Transport = {
     UDP: function(message, severity) {
-        var client = dgram.createSocket('udp4');
         var self = this;
         var syslogMessage = this.composerFunction(message, severity);
-        client.send(syslogMessage,
+
+        getSocket().send(syslogMessage,
                     0,
                     syslogMessage.length,
                     this.port,
                     this.address,
                     function(err, bytes) {
                       self._logError(err, bytes);
-                      client.close();
+                      releaseSocket();
                     }
         );
     },

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-ain2.git



More information about the Pkg-javascript-commits mailing list