Permalink
Browse files

[fix] Pool outgoing HTTP clients for requests to logs.loggly.com. Oth…

…erwise it's a big bad memory leak
  • Loading branch information...
1 parent b040dd2 commit faffcafe717a466a632d51034426cda3a234b54e @indexzero indexzero committed Jan 24, 2011
Showing with 37 additions and 14 deletions.
  1. +37 −14 lib/loggly/interns.js
View
@@ -9,10 +9,18 @@
require.paths.unshift(require('path').join(__dirname, '..'));
var request = require('request'),
+ pool = require('pool'),
loggly = require('loggly');
var interns = exports;
+// Setup the PoolManager
+var manager = pool.createPoolManager(),
+ logglyPool = manager.getPool(80, 'logs.loggly.com');
+
+manager.setMinClients(0);
+manager.setMaxClients(500);
+
// Failure HTTP Response codes based
// off Loggly specification.
var failCodes = {
@@ -107,24 +115,39 @@ interns.loggly = function () {
if (requestBody) requestOptions.body = requestBody;
try {
- request(requestOptions, function (err, res, body) {
- if (err) {
- if (callback) {
- callback(err);
+ // Only pool HTTP clients for logging calls since
+ // those will be made with a frequency 3-5 orders of
+ // magnitude greater than subdomain specific calls.
+ if (/logs\.loggly\.com/.test(requestOptions.uri)) {
+ logglyPool.getClient(function (client) {
+ requestOptions.client = client;
+ makeRequest();
+ });
+ }
+ else {
+ makeRequest();
+ }
+
+ function makeRequest() {
+ request(requestOptions, function (err, res, body) {
+ if (err) {
+ if (callback) {
+ callback(err);
+ }
+ return;
}
- return;
- }
- var statusCode = res.statusCode.toString();
- if (Object.keys(failCodes).indexOf(statusCode) !== -1) {
- if (callback) {
- callback(new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode]));
+ var statusCode = res.statusCode.toString();
+ if (Object.keys(failCodes).indexOf(statusCode) !== -1) {
+ if (callback) {
+ callback(new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode]));
+ }
+ return;
}
- return;
- }
- success(res, body);
- });
+ success(res, body);
+ });
+ }
}
catch (ex) {
if (callback) callback(ex);

0 comments on commit faffcaf

Please sign in to comment.