Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't keepAlive by default, add an option for it

  • Loading branch information...
commit 7e2587b670496eeb6de788e71e32bf262bb89ed7 1 parent 3f708c0
@mhart authored
Showing with 18 additions and 14 deletions.
  1. +0 −2  README.md
  2. +2 −5 example.js
  3. +16 −7 gelfling.js
View
2  README.md
@@ -12,8 +12,6 @@ client.send('Message', function(err) { console.log('Sent') })
client.send({ short_message: 'Message', facility: 'myApp', level: gelfling.INFO })
-client.send('Close when finally done', function() { client.close() })
-
var complexClient = gelfling('localhost', 12201, {
defaults: {
facility: 'myApp',
View
7 example.js
@@ -11,9 +11,6 @@ var msg = {
//line: 345
}
-var client = gelfling('log.adslot.com', 12201, { defaults: { line: function(msg) { return msg.id } } })
-client.send(msg, function(err) {
- console.log("all done")
- client.close()
-})
+var client = gelfling('localhost', 12201, { defaults: { line: function(msg) { return msg.id } } })
+client.send(msg, function(err) { console.log("all done") })
View
23 gelfling.js
@@ -16,14 +16,14 @@ function Gelfling(host, port, options) {
this.maxChunkSize = this.getMaxChunkSize(options.maxChunkSize)
this.defaults = options.defaults || {}
- this.udpClient = dgram.createSocket('udp4')
- this.udpClient.on('error', options.errHandler || console.error)
+ this.errHandler = options.errHandler || console.error
+ this.keepAlive = options.keepAlive
}
Gelfling.prototype.send = function(data, callback) {
if (callback == null) callback = function() {}
if (Buffer.isBuffer(data)) data = [data]
- var remaining, i, that = this
+ var udpClient, remaining, i, that = this
if (!Array.isArray(data))
return this.encode(this.convert(data), function(err, chunks) {
@@ -31,17 +31,26 @@ Gelfling.prototype.send = function(data, callback) {
that.send(chunks, callback)
})
+ if (!this.keepAlive || !this.udpClient) {
+ udpClient = dgram.createSocket('udp4')
+ udpClient.on('error', this.errHandler)
+ if (this.keepAlive) this.udpClient = udpClient
+ } else {
+ udpClient = this.udpClient
+ }
remaining = data.length
function checkDone(err) {
- if (err) return callback(err)
- if (--remaining === 0) callback()
+ if (err || --remaining === 0) {
+ if (!that.keepAlive) udpClient.close()
+ callback(err)
+ }
}
for (i = 0; i < data.length; i++)
- this.udpClient.send(data[i], 0, data[i].length, this.port, this.host, checkDone)
+ udpClient.send(data[i], 0, data[i].length, this.port, this.host, checkDone)
}
Gelfling.prototype.close = function() {
- return this.udpClient.close()
+ if (this.udpClient) this.udpClient.close()
}
Gelfling.prototype.encode = function(msg, callback) {
Please sign in to comment.
Something went wrong with that request. Please try again.