Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

add keepalive option #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ var dns = require('dns'),
dnscache = require('dnscache')({
"enable" : true,
"ttl" : 300,
"cachesize" : 1000
"cachesize" : 1000,
"useStale": false
});

//to use the cached dns either of dnscache or dns can be called.
Expand All @@ -52,6 +53,7 @@ Configuration
* `ttl` - ttl for cache-entries. Default: `300`
* `cachesize` - number of cache entries, defaults to `1000`
* `cache` - If a custom cache needs to be used instead of the supplied cache implementation. Only for Advanced Usage. Custom Cache needs to have same interface for `get` and `set`.
* `useStale` - When cache expires, user can be served stale value and re-fetched in the background, defaults to `false`.


Advanced Caching
Expand Down
18 changes: 13 additions & 5 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var CacheObject = function (conf) {
conf = conf || {};
conf.ttl = parseInt(conf.ttl, 10) || 300; //0 is not permissible
conf.cachesize = parseInt(conf.cachesize, 10) || 1000; //0 is not permissible

conf.useStale = conf.useStale || false;

this.ttl = conf.ttl * 1000;
this.max = conf.cachesize;

Expand Down Expand Up @@ -36,15 +37,15 @@ var CacheObject = function (conf) {
}

self.head.val = value;
self.head.hit = 0;
self.head.ts = Date.now();
} else {
// key is not exist
self.data[key] = {
"key" : key,
"val" : value,
"hit" : 0,
"ts" : Date.now()
"ts" : Date.now(),
"stale": false
};

if (!self.head) {
Expand Down Expand Up @@ -84,7 +85,8 @@ var CacheObject = function (conf) {
}
next(function () {
var value;
if (conf.ttl !== 0 && (Date.now() - self.data[key].ts) >= self.ttl) {
var expired = conf.ttl !== 0 && (Date.now() - self.data[key].ts) >= self.ttl;
if (expired && !conf.useStale) {
if (self.data[key].newer) {
if (self.data[key].older) {
// in the middle of the list
Expand Down Expand Up @@ -113,7 +115,13 @@ var CacheObject = function (conf) {
self.data[key].hit = self.data[key].hit + 1;
value = self.data[key].val;
}
callback(null, value);

var staleCallback;
if (expired && conf.useStale && !self.data[key].stale) {
self.data[key].stale = true;
staleCallback = function () { self.data[key].stale = false; };
}
callback(null, value, staleCallback);
});
};
};
Expand Down
168 changes: 145 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var EnhanceDns = function (conf) {
conf = conf || {};
conf.ttl = parseInt(conf.ttl, 10) || 300; //0 is not allowed ie it ttl is set to 0, it will take the default
conf.cachesize = parseInt(conf.cachesize, 10); //0 is allowed but it will disable the caching
conf.useStale = conf.useStale || false;

if (isNaN(conf.cachesize)) {
conf.cachesize = 1000; //set default cache size to 1000 records max
Expand Down Expand Up @@ -71,13 +72,26 @@ var EnhanceDns = function (conf) {
}
}

cache.get('lookup_' + domain + '_' + family + '_' + hints + '_' + all, function (error, record) {
cache.get('lookup_' + domain + '_' + family + '_' + hints + '_' + all, function (error, record, staleCallback) {
if (record) {
/*istanbul ignore next - "all" option require node 4+*/
if (Array.isArray(record)) {
return callback(error, record);
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
/*istanbul ignore next - "all" option require node 4+*/
if (Array.isArray(record)) {
callback(error, record);
} else {
callback(error, record.address, record.family);
}
} catch (e) {
if (staleCallback) {
staleCallback();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be saying "if an exception is thrown mark the cache entry as not stale", does that make sense?

}
throw e;
}
if (! staleCallback) {
return;
}
return callback(error, record.address, record.family);
callback = staleCallback;
}

try{
Expand Down Expand Up @@ -118,9 +132,21 @@ var EnhanceDns = function (conf) {
callback_new = type;
}

cache.get('resolve_' + domain + '_' + type_new, function (error, record) {
cache.get('resolve_' + domain + '_' + type_new, function (error, record, staleCallback) {
if (record) {
return callback_new(error, deepCopy(record), true);
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
callback_new(error, deepCopy(record), true);
} catch (e) {
if (staleCallback) {
staleCallback();
}
throw e;
}
if (! staleCallback) {
return;
}
callback_new = staleCallback;
}
try {
backup_object.resolve(domain, type_new, function (err, addresses) {
Expand All @@ -140,9 +166,21 @@ var EnhanceDns = function (conf) {

// override dns.resolve4 method
dns.resolve4 = function (domain, callback) {
cache.get('resolve4_' + domain, function (error, record) {
cache.get('resolve4_' + domain, function (error, record, staleCallback) {
if (record) {
return callback(error, deepCopy(record));
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
callback(error, deepCopy(record));
} catch (e) {
if (staleCallback) {
staleCallback();
}
throw e;
}
if (! staleCallback) {
return;
}
callback = staleCallback;
}
try {
backup_object.resolve4(domain, function (err, addresses) {
Expand All @@ -162,9 +200,21 @@ var EnhanceDns = function (conf) {

// override dns.resolve6 method
dns.resolve6 = function (domain, callback) {
cache.get('resolve6_' + domain, function (error, record) {
cache.get('resolve6_' + domain, function (error, record, staleCallback) {
if (record) {
return callback(error, deepCopy(record));
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
callback(error, deepCopy(record));
} catch (e) {
if (staleCallback) {
staleCallback();
}
throw e;
}
if (! staleCallback) {
return;
}
callback = staleCallback;
}
try {
backup_object.resolve6(domain, function (err, addresses) {
Expand All @@ -184,9 +234,21 @@ var EnhanceDns = function (conf) {

// override dns.resolveMx method
dns.resolveMx = function (domain, callback) {
cache.get('resolveMx_' + domain, function (error, record) {
cache.get('resolveMx_' + domain, function (error, record, staleCallback) {
if (record) {
return callback(error, deepCopy(record));
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
callback(error, deepCopy(record));
} catch (e) {
if (staleCallback) {
staleCallback();
}
throw e;
}
if (! staleCallback) {
return;
}
callback = staleCallback;
}
try {
backup_object.resolveMx(domain, function (err, addresses) {
Expand All @@ -206,9 +268,21 @@ var EnhanceDns = function (conf) {

// override dns.resolveTxt method
dns.resolveTxt = function (domain, callback) {
cache.get('resolveTxt_' + domain, function (error, record) {
cache.get('resolveTxt_' + domain, function (error, record, staleCallback) {
if (record) {
return callback(error, deepCopy(record));
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
callback(error, deepCopy(record));
} catch (e) {
if (staleCallback) {
staleCallback();
}
throw e;
}
if (! staleCallback) {
return;
}
callback = staleCallback;
}
try {
backup_object.resolveTxt(domain, function (err, addresses) {
Expand All @@ -228,9 +302,21 @@ var EnhanceDns = function (conf) {

// override dns.resolveSrv method
dns.resolveSrv = function (domain, callback) {
cache.get('resolveSrv_' + domain, function (error, record) {
cache.get('resolveSrv_' + domain, function (error, record, staleCallback) {
if (record) {
return callback(error, deepCopy(record));
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
callback(error, deepCopy(record));
} catch (e) {
if (staleCallback) {
staleCallback();
}
throw e;
}
if (! staleCallback) {
return;
}
callback = staleCallback;
}
try {
backup_object.resolveSrv(domain, function (err, addresses) {
Expand All @@ -250,9 +336,21 @@ var EnhanceDns = function (conf) {

// override dns.resolveNs method
dns.resolveNs = function (domain, callback) {
cache.get('resolveNs_' + domain, function (error, record) {
cache.get('resolveNs_' + domain, function (error, record, staleCallback) {
if (record) {
return callback(error, deepCopy(record));
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
callback(error, deepCopy(record));
} catch (e) {
if (staleCallback) {
staleCallback();
}
throw e;
}
if (! staleCallback) {
return;
}
callback = staleCallback;
}
try {
backup_object.resolveNs(domain, function (err, addresses) {
Expand All @@ -272,9 +370,21 @@ var EnhanceDns = function (conf) {

// override dns.resolveCname method
dns.resolveCname = function (domain, callback) {
cache.get('resolveCname_' + domain, function (error, record) {
cache.get('resolveCname_' + domain, function (error, record, staleCallback) {
if (record) {
return callback(error, deepCopy(record));
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
callback(error, deepCopy(record));
} catch (e) {
if (staleCallback) {
staleCallback();
}
throw e;
}
if (! staleCallback) {
return;
}
callback = staleCallback;
}
try {
backup_object.resolveCname(domain, function (err, addresses) {
Expand All @@ -294,9 +404,21 @@ var EnhanceDns = function (conf) {

// override dns.reverse method
dns.reverse = function (ip, callback) {
cache.get('reverse_' + ip, function (error, record) {
cache.get('reverse_' + ip, function (error, record, staleCallback) {
if (record) {
return callback(error, deepCopy(record));
/*istanbul ignore next - doesn't throw under normal circumstances*/
try {
callback(error, deepCopy(record));
} catch (e) {
if (staleCallback) {
staleCallback();
}
throw e;
}
if (! staleCallback) {
return;
}
callback = staleCallback;
}
try {
backup_object.reverse(ip, function (err, addresses) {
Expand Down
Loading