Browse Source

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.
v0.9.9-release
Bert Belder 12 years ago
parent
commit
6311f1c30a
  1. 17
      lib/dgram.js

17
lib/dgram.js

@ -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 send 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;
}

Loading…
Cancel
Save