Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Only connect to redis of stats_enabled

Fixes #354
  • Loading branch information...
commit 2896f09975f4deae0523a3a204d61b5f2b143015 1 parent 13f27f9
@baudehlo authored
Showing with 37 additions and 21 deletions.
  1. +25 −19 plugins/dns_list_base.js
  2. +6 −1 plugins/dnsbl.js
  3. +6 −1 plugins/dnswl.js
View
44 plugins/dns_list_base.js
@@ -5,19 +5,25 @@ var is_rfc1918 = require('./net_utils').is_rfc1918;
exports.enable_stats = false;
exports.disable_allowed = false;
-
-var redis_avail = false;
-// See if redis is available
-try {
- var redis = require('redis');
- var client = redis.createClient();
- redis_avail = true;
-}
-catch (err) {}
+exports.redis_host = '127.0.0.1:6379';
+var redis_client;
exports.lookup = function (lookup, zone, cb) {
if (!lookup || !zone) return cb();
+ if (this.enable_stats && !redis_client) {
+ var redis = require('redis');
+ var host_port = this.redis_host.split(':');
+ if (!host_port[0]) host_port[0] = '127.0.0.1';
+ if (!host_port[1]) {
+ host_port[1] = 6379;
+ }
+ else {
+ host_port[1] = parseInt(host_port[1], 10);
+ }
+ redis_client = redis.createClient(host_port[1], host_port[0]);
+ }
+
// Reverse lookup if IPv4 address
if (net.isIPv4(lookup)) {
// Don't query private addresses
@@ -32,7 +38,7 @@ exports.lookup = function (lookup, zone, cb) {
return cb();
}
- if (redis_avail && this.enable_stats) {
+ if (this.enable_stats) {
var start = (new Date).getTime();
}
@@ -45,15 +51,15 @@ exports.lookup = function (lookup, zone, cb) {
this.logdebug('looking up: ' + query);
dns.resolve(query, 'A', function (err, a) {
// Statistics
- if (redis_avail && self.enable_stats) {
+ if (self.enable_stats) {
var elapsed = (new Date).getTime() - start;
- client.hincrby('dns-list-stat:' + zone, 'TOTAL', 1);
+ redis_client.hincrby('dns-list-stat:' + zone, 'TOTAL', 1);
(err)
- ? client.hincrby('dns-list-stat:' + zone, err.code, 1)
- : client.hincrby('dns-list-stat:' + zone, 'LISTED', 1);
- client.hget('dns-list-stat:' + zone, 'AVG_RT', function (err, rt) {
+ ? redis_client.hincrby('dns-list-stat:' + zone, err.code, 1)
+ : redis_client.hincrby('dns-list-stat:' + zone, 'LISTED', 1);
+ redis_client.hget('dns-list-stat:' + zone, 'AVG_RT', function (err, rt) {
if (!err)
- client.hset('dns-list-stat:' + zone, 'AVG_RT',
+ redis_client.hset('dns-list-stat:' + zone, 'AVG_RT',
(parseInt(rt) ? (parseInt(elapsed) + parseInt(rt))/2 : parseInt(elapsed)));
});
}
@@ -85,12 +91,12 @@ exports.multi = function (lookup, zones, cb) {
// All queries completed?
if (pending === 0) {
// Statistics: check hit overlap
- if (redis_avail && self.enable_stats && listed.length > 0) {
+ if (self.enable_stats && listed.length > 0) {
listed.forEach(function (zone) {
for (var i=0; i < listed.length; i++) {
(listed[i] === zone)
- ? client.hincrby('dns-list-overlap:' + zone, 'TOTAL', 1)
- : client.hincrby('dns-list-overlap:' + zone, listed[i], 1);
+ ? redis_client.hincrby('dns-list-overlap:' + zone, 'TOTAL', 1)
+ : redis_client.hincrby('dns-list-overlap:' + zone, listed[i], 1);
}
});
}
View
7 plugins/dnsbl.js
@@ -8,7 +8,12 @@ exports.register = function() {
if (cfg.main.enable_stats) {
this.logdebug('stats reporting enabled');
- this.enable_stats = 1;
+ this.enable_stats = true;
+ }
+
+ if (cfg.main.stats_redis_host) {
+ this.redis_host = cfg.main.stats_redis_host;
+ this.logdebug('set stats redis host to: ' + this.redis_host);
}
this.zones = [];
View
7 plugins/dnswl.js
@@ -8,7 +8,12 @@ exports.register = function() {
if (cfg.main.enable_stats) {
this.logdebug('stats reporting enabled');
- this.enable_stats = 1;
+ this.enable_stats = true;
+ }
+
+ if (cfg.main.stats_redis_host) {
+ this.redis_host = cfg.main.stats_redis_host;
+ this.logdebug('set stats redis host to: ' + this.redis_host);
}
this.zones = [];
Please sign in to comment.
Something went wrong with that request. Please try again.