WIP: race conditions and stalling server

This commit is contained in:
Lauren Budorick 2016-03-27 20:55:12 -07:00 committed by Patrick Niklaus
parent 8ac403abb9
commit 8947c789a9
6 changed files with 38 additions and 28 deletions

View File

@ -47,12 +47,12 @@ module.exports = function () {
if (headers.has('route')) { if (headers.has('route')) {
if (json.matchings.length != 1) throw new Error('*** Checking route only supported for matchings with one subtrace'); if (json.matchings.length != 1) throw new Error('*** Checking route only supported for matchings with one subtrace');
route = this.wayList(json.matchings[0].instructions); route = this.wayList(json.matchings[0]);
} }
if (headers.has('duration')) { if (headers.has('duration')) {
if (json.matchings.length != 1) throw new Error('*** Checking duration only supported for matchings with one subtrace'); if (json.matchings.length != 1) throw new Error('*** Checking duration only supported for matchings with one subtrace');
duration = json.matchings[0].route_summary.total_time; duration = json.matchings[0].duration;
} }
} }

View File

@ -66,13 +66,13 @@ module.exports = function () {
r.query = this.query; r.query = this.query;
r.json = JSON.parse(body); r.json = JSON.parse(body);
r.status = r.json.status === 200 ? 'x' : null; r.status = res.statusCode === 200 ? 'x' : null;
if (r.status) { if (r.status) {
r.route = this.wayList(r.json.route_instructions); r.route = this.wayList(r.json.routes[0]);
if (r.route === util.format('w%d', i)) { if (r.route === util.format('w%d', i)) {
r.time = r.json.route_summary.total_time; r.time = r.json.routes[0].duration;
r.distance = r.json.route_summary.total_distance; r.distance = r.json.routes[0].distance;
r.speed = r.time > 0 ? parseInt(3.6 * r.distance / r.time) : null; r.speed = r.time > 0 ? parseInt(3.6 * r.distance / r.time) : null;
} else { } else {
r.status = null; r.status = null;
@ -96,15 +96,23 @@ module.exports = function () {
}); });
result.bothw = {}; result.bothw = {};
['status', 'time', 'distance', 'speed'].forEach((key) => {
var sq = d3.queue();
var parseRes = (key, scb) => {
if (result.forw[key] === result.backw[key]) { if (result.forw[key] === result.backw[key]) {
result.bothw[key] = result.forw[key]; result.bothw[key] = result.forw[key];
} else { } else {
result.bothw[key] = 'diff'; result.bothw[key] = 'diff';
} }
scb();
}
['status', 'time', 'distance', 'speed'].forEach((key) => {
sq.defer(parseRes, key);
}); });
cb(null, result); sq.awaitAll(() => { cb(null, result); });
}); });
}; };
}; };

View File

@ -94,13 +94,13 @@ module.exports = function () {
q.awaitAll(callback); q.awaitAll(callback);
}; };
var ensureDecimal = (i) => { this.ensureDecimal = (i) => {
if (parseInt(i) === i) return i.toFixed(1); if (parseInt(i) === i) return i.toFixed(1);
else return i; else return i;
}; };
this.tableCoordToLonLat = (ci, ri) => { this.tableCoordToLonLat = (ci, ri) => {
return [this.origin[0] + ci * this.zoom, this.origin[1] - ri * this.zoom].map(ensureDecimal); return [this.origin[0] + ci * this.zoom, this.origin[1] - ri * this.zoom].map(this.ensureDecimal);
}; };
this.addOSMNode = (name, lon, lat, id) => { this.addOSMNode = (name, lon, lat, id) => {

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
var util = require('util'); var util = require('util');
var path = require('path');
var fs = require('fs'); var fs = require('fs');
var OSRMError = class extends Error { var OSRMError = class extends Error {
@ -23,8 +24,8 @@ var OSRMError = class extends Error {
}); });
} }
logTail (path, n, callback) { logTail (logPath, n, callback) {
var expanded = path.resolve(this.TEST_FOLDER, path); var expanded = path.resolve(this.TEST_FOLDER, logPath);
fs.exists(expanded, (exists) => { fs.exists(expanded, (exists) => {
if (exists) { if (exists) {
fs.readFile(expanded, (err, data) => { fs.readFile(expanded, (err, data) => {

View File

@ -34,7 +34,7 @@ module.exports = function () {
}; };
var encodeWaypoints = (waypoints) => { var encodeWaypoints = (waypoints) => {
return waypoints.map(w => [w.lon, w.lat].join(',')); return waypoints.map(w => [w.lon, w.lat].map(this.ensureDecimal).join(','));
}; };
this.requestRoute = (waypoints, bearings, userParams, callback) => { this.requestRoute = (waypoints, bearings, userParams, callback) => {
@ -43,7 +43,7 @@ module.exports = function () {
var defaults = { var defaults = {
output: 'json', output: 'json',
steps: 'true', steps: 'true',
alternative: 'false' alternatives: 'false'
}, },
params = this.overwriteParams(defaults, userParams), params = this.overwriteParams(defaults, userParams),
encodedWaypoints = encodeWaypoints(waypoints); encodedWaypoints = encodeWaypoints(waypoints);
@ -108,18 +108,19 @@ module.exports = function () {
return this.requestPath('match', params, callback); return this.requestPath('match', params, callback);
}; };
this.extractInstructionList = (instructions, index, postfix) => { this.extractInstructionList = (instructions, keyFinder, postfix) => {
postfix = postfix || null; postfix = postfix || null;
if (instructions) { if (instructions) {
return instructions.filter(r => r[0].toString() !== this.DESTINATION_REACHED.toString()) return Array.prototype.concat.apply([],
.map(r => r[index]) instructions.legs.map(l => l.steps))
.map(r => (isNaN(parseInt(r)) && (!r || r == '')) ? '""' : '' + r + (postfix || '')) .filter(s => s.maneuver.type !== 'arrive')
.map(keyFinder)
.join(','); .join(',');
} }
}; };
this.wayList = (instructions) => { this.wayList = (instructions) => {
return this.extractInstructionList(instructions, 1); return this.extractInstructionList(instructions, s => s.name);
}; };
this.compassList = (instructions) => { this.compassList = (instructions) => {

View File

@ -39,13 +39,13 @@ module.exports = function () {
var hasRoute = json.status === 200; var hasRoute = json.status === 200;
if (hasRoute) { if (hasRoute) {
instructions = this.wayList(json.route_instructions); instructions = this.wayList(json.routes[0]);
bearings = this.bearingList(json.route_instructions); bearings = this.bearingList(json.routes[0]);
compasses = this.compassList(json.route_instructions); compasses = this.compassList(json.routes[0]);
turns = this.turnList(json.route_instructions); turns = this.turnList(json.routes[0]);
modes = this.modeList(json.route_instructions); modes = this.modeList(json.routes[0]);
times = this.timeList(json.route_instructions); times = this.timeList(json.routes[0]);
distances = this.distanceList(json.route_instructions); distances = this.distanceList(json.routes[0]);
} }
if (headers.has('status')) { if (headers.has('status')) {
@ -81,8 +81,8 @@ module.exports = function () {
this.wayList(json.alternative_instructions[0]) : ''; this.wayList(json.alternative_instructions[0]) : '';
} }
var distance = hasRoute && json.route_summary.total_distance, var distance = hasRoute && json.routes[0].distance,
time = hasRoute && json.route_summary.total_time; time = hasRoute && json.routes[0].duration;
if (headers.has('distance')) { if (headers.has('distance')) {
if (row.distance.length) { if (row.distance.length) {