Merge branch 'master' into middlewarify-analyses-controller

This commit is contained in:
Daniel García Aubert 2017-11-23 11:47:57 +01:00
commit 1a78b8a75a
10 changed files with 101 additions and 31 deletions

Binary file not shown.

View File

@ -4,6 +4,7 @@ var step = require('step');
var cors = require('../middleware/cors');
var userMiddleware = require('../middleware/user');
var allowQueryParams = require('../middleware/allow-query-params');
var vectorError = require('../middleware/vector-error');
var DataviewBackend = require('../backends/dataview');
var AnalysisStatusBackend = require('../backends/analysis-status');
@ -50,7 +51,8 @@ LayergroupController.prototype.register = function(app) {
cors(),
userMiddleware,
this.prepareContext,
this.tile.bind(this)
this.tile.bind(this),
vectorError()
);
app.get(
@ -58,7 +60,8 @@ LayergroupController.prototype.register = function(app) {
cors(),
userMiddleware,
this.prepareContext,
this.tile.bind(this)
this.tile.bind(this),
vectorError()
);
app.get(
@ -67,7 +70,8 @@ LayergroupController.prototype.register = function(app) {
userMiddleware,
validateLayerRouteMiddleware,
this.prepareContext,
this.layer.bind(this)
this.layer.bind(this),
vectorError()
);
app.get(

View File

@ -6,6 +6,7 @@ var NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
var cors = require('../middleware/cors');
var userMiddleware = require('../middleware/user');
var allowQueryParams = require('../middleware/allow-query-params');
var vectorError = require('../middleware/vector-error');
function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, previewBackend,
surrogateKeysCache, tablesExtentApi, metadataBackend) {
@ -26,7 +27,8 @@ NamedMapsController.prototype.register = function(app) {
cors(),
userMiddleware,
this.prepareContext,
this.tile.bind(this)
this.tile.bind(this),
vectorError()
);
app.get(
@ -102,6 +104,7 @@ NamedMapsController.prototype.tile = function(req, res, next) {
},
function handleImage(err, tile, headers, stats) {
req.profiler.add(stats);
if (err) {
err.label = 'NAMED_MAP_TILE';
next(err);

View File

@ -1,8 +1,6 @@
const _ = require('underscore');
module.exports = function localsMiddleware(req, res, next) {
_.extend(res.locals, req.params);
// save req.params in res.locals
res.locals = Object.assign(req.params || {}, res.locals);
next();
};

View File

@ -4,11 +4,5 @@ var cdbRequest = new CdbRequest();
module.exports = function userMiddleware(req, res, next) {
res.locals.user = cdbRequest.userByReq(req);
// avoid a req.params.user equals to undefined
// overwrites res.locals.user
if(!req.params.user) {
delete req.params.user;
}
next();
};

View File

@ -0,0 +1,30 @@
const fs = require('fs');
const timeoutErrorVectorTile = fs.readFileSync(__dirname + '/../../../assets/render-timeout-fallback.mvt');
module.exports = function vectorError() {
return function vectorErrorMiddleware(err, req, res, next) {
if(req.params.format === 'mvt') {
if (isTimeoutError(err)) {
res.set('Content-Type', 'application/x-protobuf');
return res.status(429).send(timeoutErrorVectorTile);
}
}
next(err);
};
};
function isRenderTimeoutError (err) {
return err.message === 'Render timed out';
}
function isDatasourceTimeoutError (err) {
return err.message && err.message.match(/canceling statement due to statement timeout/i);
}
function isTimeoutError (err) {
return isRenderTimeoutError(err) || isDatasourceTimeoutError(err);
}

37
scripts/mvt-timeout-error.py Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python
import mapbox_vector_tile
lines_list = []
# main diagonal line
lines_list.append({ "geometry":"LINESTRING (0 0, 4096 4096)"})
# diagonal lines
for i in range(4096/32, 4096, 4096/32):
start = i
end = 4096 - i
lines_list.append({ "geometry":"LINESTRING (0 " + str(start) + ", " + str(end) + " 4096)" })
lines_list.append({ "geometry":"LINESTRING (" + str(start) + " 0, 4096 " + str(end) + ")" })
# box lines
lines_list.append({ "geometry":"LINESTRING (0 0, 0 4096)"})
lines_list.append({ "geometry":"LINESTRING (0 4096, 4096 4096)"})
lines_list.append({ "geometry":"LINESTRING (4096 4096, 4096 0)"})
lines_list.append({ "geometry":"LINESTRING (4096 0, 0 0)"})
tile = mapbox_vector_tile.encode([
{
"name": "errorTileSquareLayer",
"features": [{ "geometry":"POLYGON ((0 0, 0 4096, 4096 4096, 4096 0, 0 0))" }]
},
{
"name": "errorTileStripesLayer",
"features": lines_list
}
])
with open('./assets/render-timeout-fallback.mvt', 'w+') as f:
f.write(tile)

View File

@ -419,18 +419,23 @@ describe('user database timeout limit', function () {
response: {
status: 429,
headers: {
'Content-Type': 'application/json; charset=utf-8'
'Content-Type': 'application/x-protobuf'
}
}
};
this.testClient.getTile(0, 0, 0, params, (err, res, timeoutError) => {
this.testClient.getTile(0, 0, 0, params, (err, res, tile) => {
assert.ifError(err);
assert.deepEqual(timeoutError, DATASOURCE_TIMEOUT_ERROR);
var tileJSON = tile.toJSON();
assert.equal(Array.isArray(tileJSON), true);
assert.equal(tileJSON.length, 2);
assert.equal(tileJSON[0].name, 'errorTileSquareLayer');
assert.equal(tileJSON[1].name, 'errorTileStripesLayer');
done();
});
});
});
});

View File

@ -232,7 +232,7 @@ describe('user render timeout limit', function () {
response: {
status: 429,
headers: {
'Content-Type': 'application/json; charset=utf-8'
'Content-Type': 'application/x-protobuf'
}
}
};
@ -240,15 +240,12 @@ describe('user render timeout limit', function () {
this.testClient.getTile(0, 0, 0, params, (err, res, tile) => {
assert.ifError(err);
assert.deepEqual(tile, {
errors: ['You are over platform\'s limits. Please contact us to know more details'],
errors_with_context: [{
type: 'limit',
subtype: 'datasource',
message: 'You are over platform\'s limits. Please contact us to know more details'
}]
});
var tileJSON = tile.toJSON();
assert.equal(Array.isArray(tileJSON), true);
assert.equal(tileJSON.length, 2);
assert.equal(tileJSON[0].name, 'errorTileSquareLayer');
assert.equal(tileJSON[1].name, 'errorTileStripesLayer');
done();
});
});

View File

@ -95,8 +95,10 @@ if test x"$PREPARE_PGSQL" = xyes; then
do
# Strip PARALLEL labels for PostgreSQL releases before 9.6
if [ $PG_PARALLEL -eq 0 ]; then
TMPFILE=$(mktemp /tmp/$(basename $0).XXXXXXXX)
sed -e 's/PARALLEL \= [A-Z]*,/''/g' \
-e 's/PARALLEL [A-Z]*/''/g' -i sql/$i.sql
-e 's/PARALLEL [A-Z]*/''/g' sql/$i.sql > $TMPFILE
mv $TMPFILE sql/$i.sql
fi
cat sql/${i}.sql |
sed -e 's/cartodb\./public./g' -e "s/''cartodb''/''public''/g" |