summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cluster-geo/cluster-geo/cluster-geo.js45
1 files changed, 28 insertions, 17 deletions
diff --git a/cluster-geo/cluster-geo/cluster-geo.js b/cluster-geo/cluster-geo/cluster-geo.js
index bbb78e1f..1338b00b 100644
--- a/cluster-geo/cluster-geo/cluster-geo.js
+++ b/cluster-geo/cluster-geo/cluster-geo.js
@@ -19,8 +19,8 @@ if (!config.maxmind) {
console.error('FATAL: only MaxMind GeoIP2 is currently supported and is not configured in config.js');
process.exit(1);
}
-var geo = require('geoip2ws')(config.maxmind);
+var geo = require('geoip2ws')(config.maxmind);
var cache = require('levelup')(__dirname + '/cache.leveldb');
function lookup(ip,callback)
@@ -32,29 +32,40 @@ function lookup(ip,callback)
if (cachedEntry) {
let ts = cachedEntry.ts;
let r = cachedEntry.r;
- if (ts) {
- if ((Date.now() - ts) < CACHE_TTL)
- return callback(null,r);
+ if ((ts)&&((Date.now() - ts) < CACHE_TTL)) {
+ //console.error(ip+': cached!');
+ return callback(null,(r) ? r : null);
}
}
} catch (e) {}
}
- geo(ip,function(err,result) {
- if (err)
- return callback(err,null);
- if (!result)
- result = null;
-
- cache.put(ip,JSON.stringify({
- ts: Date.now(),
- r: result
- }),function(err) {
- if (err)
- console.error('Error saving to cache: '+err);
- return callback(null,result);
+ cache.put(ip,JSON.stringify({
+ ts: Date.now() - (CACHE_TTL - 30000), // set ts to expire in 30 seconds while the query is in progress
+ r: null
+ }),function(err) {
+
+ geo(ip,function(err,result) {
+ if (err) {
+ //console.error(err);
+ return callback(err,null);
+ }
+
+ if (!result)
+ result = null;
+
+ cache.put(ip,JSON.stringify({
+ ts: Date.now(),
+ r: result
+ }),function(err) {
+ if (err)
+ console.error('Error saving to cache: '+err);
+ return callback(null,result);
+ });
});
+
});
+
});
};