Permalink
Browse files

dgram: avoid EventEmitter leak warning

When a datagram socket hasn't been bound yet, node will defer `send()`
operations until binding has completed. Before this patch a `listening`
listener would be installed every time `send` was called. This triggered
an EventEmitter leak warning when more than 10 packets were sent in a
tight loop. Therefore switch to using a single `listening` listener, and
use an array to enqueue outbound packets.
  • Loading branch information...
1 parent 5e7e51c commit f4f86cf3c357bc35d4af689958f64baad2480328 @piscisaureus committed Jan 28, 2013
Showing with 14 additions and 3 deletions.
  1. +14 −3 lib/dgram.js
View
@@ -243,10 +243,21 @@ Socket.prototype.send = function(buffer,
if (self._bindState == BIND_STATE_UNBOUND)
self.bind(0, null);
+ // If the socket hasn't been bound yet, push the outbound packet onto the
+ // send queue and send after binding is complete.
if (self._bindState != BIND_STATE_BOUND) {
- self.once('listening', function() {
- self.send(buffer, offset, length, port, address, callback);
- });
+ // If the send queue hasn't been initialized yet, do it, and install an
+ // event handler that flushes the bind queue after binding is done.
+ if (!self._sendQueue) {
+ self._sendQueue = [];
+ self.once('listening', function() {
+ // Flush the send queue.
+ for (var i = 0; i < self._sendQueue.length; i++)
+ self.send.apply(self, self._sendQueue[i]);
+ self._sendQueue = undefined;
+ });
+ }
+ self._sendQueue.push([buffer, offset, length, port, address, callback]);
return;
}

0 comments on commit f4f86cf

Please sign in to comment.