cartodb-redis
Advanced tools
Comparing version 0.16.0 to 1.0.0
@@ -47,2 +47,3 @@ /** | ||
oauth_metadata_db: 3, | ||
rate_limits_db: 8, | ||
user_key: dot.template("rails:users:{{=it.username}}"), | ||
@@ -58,3 +59,6 @@ oauth_user_key: dot.template("rails:oauth_access_tokens:{{=it.oauth_access_key}}"), | ||
user_tiler_limits: dot.template("limits:tiler:{{=it.username}}"), | ||
user_timeout_limits: dot.template("limits:timeout:{{=it.username}}") // db & render timeouts | ||
user_timeout_limits: dot.template("limits:timeout:{{=it.username}}"), // db & render timeouts | ||
rate_limits_sha_script: null, | ||
rate_limits_store_key: dot.template("limits:rate:store:{{=it.username}}:{{=it.app}}:{{=it.endpointGroup}}"), | ||
rate_limits_status_key: dot.template("limits:rate:status:{{=it.username}}:{{=it.app}}:{{=it.endpointGroup}}") | ||
}; | ||
@@ -525,2 +529,109 @@ | ||
me.rateLimitLuaScript = | ||
'local results = {} ' + | ||
'local resultsCounter = 0 ' + | ||
'local limits = {} ' + | ||
'local limitsArray = redis.call("LRANGE", KEYS[1], 0, -1) ' + | ||
'for i, v in ipairs(limitsArray) do ' + | ||
' local rest = i % 3 ' + | ||
' if rest ~= 0 then ' + | ||
' limits[rest] = v ' + | ||
' else ' + | ||
' resultsCounter = resultsCounter + 1 ' + | ||
' results[resultsCounter] = redis.call("CL.THROTTLE", KEYS[2], limits[1], limits[2], v) ' + | ||
' end ' + | ||
'end ' + | ||
'return results' | ||
; | ||
me.loadRateLimitsScript = function(callback) { | ||
this.redisCmd(me.rate_limits_db, 'SCRIPT', ['LOAD', me.rateLimitLuaScript], function (err, sha) { | ||
if (!err && sha) { | ||
me.rate_limits_sha_script = sha; | ||
} | ||
if(callback) { | ||
callback(err, sha) | ||
} | ||
}); | ||
}; | ||
me.getRateLimit = function(username, app, endpointGroup, callback) { | ||
var params = [ | ||
this.rate_limits_sha_script ? this.rate_limits_sha_script : this.rateLimitLuaScript, | ||
2, | ||
this.rate_limits_store_key({ | ||
username: username, | ||
app: app, | ||
endpointGroup: endpointGroup | ||
}), // KEY[1] | ||
this.rate_limits_status_key({ | ||
username: username, | ||
app: app, | ||
endpointGroup: endpointGroup | ||
}) // KEY[2] | ||
]; | ||
this.redisCmd( | ||
me.rate_limits_db, | ||
me.rate_limits_sha_script ? 'EVALSHA' : 'EVAL', | ||
params, | ||
function (err, rateLimits) { | ||
if (err) { | ||
if (err.name === 'ReplyError' && err.message === 'NOSCRIPT No matching script. Please use EVAL.') { | ||
me.rate_limits_sha_script = null; | ||
me.getRateLimit(username, app, endpointGroup, callback); | ||
} else { | ||
callback(err); | ||
} | ||
} else { | ||
callback(null, me.getLowerRateLimit(rateLimits)); | ||
} | ||
} | ||
); | ||
}; | ||
/** | ||
* Returns the inner rateLimit what is the strictest one or undefined | ||
* @param {Array} rateLimits Each inner array has 5 integers indicating: | ||
* isBloqued, limit, remaining, retry, reset | ||
*/ | ||
me.getLowerRateLimit = function(rateLimits) { | ||
if (!Array.isArray(rateLimits) || !rateLimits.length) { | ||
return; | ||
} | ||
var minIndex; | ||
var minRemainingValue; | ||
for (var currentIndex = 0; currentIndex < rateLimits.length; currentIndex++) { | ||
var rateLimit = rateLimits[currentIndex]; | ||
if (!me.validateRatelimit(rateLimit)) { | ||
continue; | ||
} | ||
var isBlocked = rateLimit[0] | ||
var remaining = rateLimit[2]; | ||
if (isBlocked === 1) { | ||
minIndex = currentIndex; | ||
break; | ||
} | ||
if (minRemainingValue === undefined || remaining < minRemainingValue) { | ||
minIndex = currentIndex; | ||
minRemainingValue = remaining; | ||
} | ||
} | ||
return rateLimits[minIndex]; | ||
}; | ||
me.validateRatelimit = function(rateLimit) { | ||
return rateLimit.length === 5; | ||
}; | ||
/******************************************************************************************************************* | ||
@@ -527,0 +638,0 @@ * END LIMITS |
{ | ||
"name": "cartodb-redis", | ||
"version": "0.16.0", | ||
"version": "1.0.0", | ||
"main": "./lib/carto_metadata.js", | ||
@@ -31,4 +31,4 @@ "description": "A Node.js based lib to interact with cartodb redis databases", | ||
"engine": { | ||
"node": ">= 0.6" | ||
"node": ">= 6.9.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
69725
16
1432
1
0