Merge branch 'master' into limits-error-refactor
This commit is contained in:
commit
b75334f1e7
1
.gitignore
vendored
1
.gitignore
vendored
@ -12,4 +12,3 @@ test/test.log
|
|||||||
test/acceptance/oauth/venv/*
|
test/acceptance/oauth/venv/*
|
||||||
coverage/
|
coverage/
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
libredis_cell.so
|
|
||||||
|
@ -29,12 +29,16 @@ function rateLimit(userLimits, endpointGroup = null) {
|
|||||||
res.set({
|
res.set({
|
||||||
'Carto-Rate-Limit-Limit': limit,
|
'Carto-Rate-Limit-Limit': limit,
|
||||||
'Carto-Rate-Limit-Remaining': remaining,
|
'Carto-Rate-Limit-Remaining': remaining,
|
||||||
'Retry-After': retry,
|
|
||||||
'Carto-Rate-Limit-Reset': reset
|
'Carto-Rate-Limit-Reset': reset
|
||||||
});
|
});
|
||||||
|
|
||||||
if (isBlocked) {
|
if (isBlocked) {
|
||||||
const rateLimitError = new Error('You are over the limits.');
|
// retry is floor rounded in seconds by redis-cell
|
||||||
|
res.set('Retry-After', retry + 1);
|
||||||
|
|
||||||
|
const rateLimitError = new Error(
|
||||||
|
'You are over platform\'s limits. Please contact us to know more details'
|
||||||
|
);
|
||||||
rateLimitError.http_status = 429;
|
rateLimitError.http_status = 429;
|
||||||
rateLimitError.context = 'limit';
|
rateLimitError.context = 'limit';
|
||||||
rateLimitError.detail = 'rate-limit';
|
rateLimitError.detail = 'rate-limit';
|
||||||
|
@ -48,7 +48,10 @@ function assertRequest (status, limit, remaining, reset, retry, done = null) {
|
|||||||
assert.equal(res.headers['carto-rate-limit-limit'], limit);
|
assert.equal(res.headers['carto-rate-limit-limit'], limit);
|
||||||
assert.equal(res.headers['carto-rate-limit-remaining'], remaining);
|
assert.equal(res.headers['carto-rate-limit-remaining'], remaining);
|
||||||
assert.equal(res.headers['carto-rate-limit-reset'], reset);
|
assert.equal(res.headers['carto-rate-limit-reset'], reset);
|
||||||
assert.equal(res.headers['retry-after'], retry);
|
|
||||||
|
if (retry) {
|
||||||
|
assert.equal(res.headers['retry-after'], retry);
|
||||||
|
}
|
||||||
|
|
||||||
if(status === 429) {
|
if(status === 429) {
|
||||||
const expectedResponse = {
|
const expectedResponse = {
|
||||||
@ -91,12 +94,12 @@ describe('rate limit', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("1 req/sec: 2 req/seg should be limited", function(done) {
|
it("1 req/sec: 2 req/seg should be limited", function(done) {
|
||||||
assertRequest(200, 2, 1, 1, -1);
|
assertRequest(200, 2, 1, 1);
|
||||||
setTimeout( () => assertRequest(200, 2, 0, 1, -1), 250 );
|
setTimeout( () => assertRequest(200, 2, 0, 1, null), 250 );
|
||||||
setTimeout( () => assertRequest(429, 2, 0, 1, 0), 500 );
|
setTimeout( () => assertRequest(429, 2, 0, 1, 1), 500 );
|
||||||
setTimeout( () => assertRequest(429, 2, 0, 1, 0), 750 );
|
setTimeout( () => assertRequest(429, 2, 0, 1, 1), 750 );
|
||||||
setTimeout( () => assertRequest(429, 2, 0, 1, 0), 950 );
|
setTimeout( () => assertRequest(429, 2, 0, 1, 1), 950 );
|
||||||
setTimeout( () => assertRequest(200, 2, 0, 1, -1, done), 1050 );
|
setTimeout( () => assertRequest(200, 2, 0, 1, null, done), 1050 );
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -18,7 +18,6 @@ OPT_DROP_PGSQL=yes # drop the postgreql test environment
|
|||||||
OPT_DROP_REDIS=yes # drop the redis test environment
|
OPT_DROP_REDIS=yes # drop the redis test environment
|
||||||
OPT_COVERAGE=no # run tests with coverage
|
OPT_COVERAGE=no # run tests with coverage
|
||||||
OPT_OFFLINE=no # do not donwload scripts
|
OPT_OFFLINE=no # do not donwload scripts
|
||||||
OPT_REDIS_CELL=yes # download redis cell
|
|
||||||
|
|
||||||
|
|
||||||
cd $(dirname $0)
|
cd $(dirname $0)
|
||||||
@ -55,17 +54,6 @@ die() {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
get_redis_cell() {
|
|
||||||
if test x"$OPT_REDIS_CELL" = xyes; then
|
|
||||||
echo "Downloading redis-cell"
|
|
||||||
curl -L https://github.com/brandur/redis-cell/releases/download/v0.2.2/redis-cell-v0.2.2-x86_64-unknown-linux-gnu.tar.gz --output redis-cell.tar.gz > /dev/null 2>&1
|
|
||||||
tar xvzf redis-cell.tar.gz > /dev/null 2>&1
|
|
||||||
mv libredis_cell.so ${BASEDIR}/support/libredis_cell.so
|
|
||||||
rm redis-cell.tar.gz
|
|
||||||
rm libredis_cell.d
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
trap 'cleanup_and_exit' 1 2 3 5 9 13
|
trap 'cleanup_and_exit' 1 2 3 5 9 13
|
||||||
|
|
||||||
while [ -n "$1" ]; do
|
while [ -n "$1" ]; do
|
||||||
@ -103,10 +91,6 @@ while [ -n "$1" ]; do
|
|||||||
OPT_OFFLINE=yes
|
OPT_OFFLINE=yes
|
||||||
shift
|
shift
|
||||||
continue
|
continue
|
||||||
elif test "$1" = "--norediscell"; then
|
|
||||||
OPT_REDIS_CELL=no
|
|
||||||
shift
|
|
||||||
continue
|
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
@ -128,9 +112,12 @@ fi
|
|||||||
TESTS=$@
|
TESTS=$@
|
||||||
|
|
||||||
if test x"$OPT_CREATE_REDIS" = xyes; then
|
if test x"$OPT_CREATE_REDIS" = xyes; then
|
||||||
get_redis_cell
|
|
||||||
echo "Starting redis on port ${REDIS_PORT}"
|
echo "Starting redis on port ${REDIS_PORT}"
|
||||||
echo "port ${REDIS_PORT}" | redis-server - --loadmodule ${BASEDIR}/support/libredis_cell.so > ${BASEDIR}/test.log &
|
REDIS_CELL_PATH="${BASEDIR}/support/libredis_cell.so"
|
||||||
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||||
|
REDIS_CELL_PATH="${BASEDIR}/support/libredis_cell.dylib"
|
||||||
|
fi
|
||||||
|
echo "port ${REDIS_PORT}" | redis-server - --loadmodule ${REDIS_CELL_PATH} > ${BASEDIR}/test.log &
|
||||||
PID_REDIS=$!
|
PID_REDIS=$!
|
||||||
echo ${PID_REDIS} > ${BASEDIR}/redis.pid
|
echo ${PID_REDIS} > ${BASEDIR}/redis.pid
|
||||||
fi
|
fi
|
||||||
|
BIN
test/support/libredis_cell.dylib
Executable file
BIN
test/support/libredis_cell.dylib
Executable file
Binary file not shown.
BIN
test/support/libredis_cell.so
Executable file
BIN
test/support/libredis_cell.so
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user