mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
fix(routes): report an upstream routing outage as unavailable
When the OSRM routing service failed (a 5xx, a 403 block, a 404), answered
with a body that was not JSON, or did not answer at all (timeout, network
error), /api/route returned HTTP 200 with 'no route found' or 'route proxy
error'. plan_route turned that into invalid_arguments ("No walk route from
...: no route found"), blaming the caller for an outage, and the directions
layer reported "No route found between A and B".
The proxy now answers all of these with 502 {ok:false, error:'routing
service unavailable'}: a rejected upstream fetch, any failure status but
400, a non-JSON content type, and a body read that fails or does not parse.
An exception in the proxy's own code after that point is still a 200 'route
proxy error'. createRouteService throws for a 5xx before reading the body's
ok:false, so plan_route reports unavailable, and the directions source
treats any 5xx as "Routing unavailable (HTTP n)", also when the failure
page in front of the proxy is not JSON. OSRM's own refusal (a 400 such as
NoRoute, or a 200 without a route) still reads "no route found".
The 12 s upstream timeout now starts when the request leaves rather than
before it waits for an outbound slot, so a wait in the proxy's own queue
(up to about 8 s at the 1 s spacing) is not reported as an outage.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
ebd8439868
commit
f8795ff426
7 files changed
+223
-34
No files matched your search
@@ -236,23 +236,107 @@ test('OSRM routing preserves aliases, cache, span guards and upstream failure be
|
||||
assert.equal((await request(route, url)).body.error, error);
|
||||
assert.equal(calls, 1);
|
||||
now += 600001;
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async () => new Response('offline', { status: 503 }),
|
||||
// OSRM's own "no route" is a 400 with a JSON code, and stays one.
|
||||
t.mock.method(globalThis, 'fetch', async () =>
|
||||
Response.json(
|
||||
{ code: 'NoRoute', message: 'Impossible route between points' },
|
||||
{ status: 400 },
|
||||
),
|
||||
);
|
||||
assert.deepEqual((await request(route, query)).body, {
|
||||
ok: false,
|
||||
error: 'no route found',
|
||||
});
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async () => new Response('x', { headers: { 'content-type': 'text/html' } }),
|
||||
const noRoute = await request(route, query);
|
||||
assert.equal(noRoute.statusCode, 200);
|
||||
assert.deepEqual(noRoute.body, { ok: false, error: 'no route found' });
|
||||
t.mock.method(globalThis, 'fetch', async () =>
|
||||
Response.json({ code: 'NoRoute', routes: [] }),
|
||||
);
|
||||
assert.equal((await request(route, query)).body.error, 'no route found');
|
||||
});
|
||||
|
||||
test('a routing outage is reported as one, not as "no route found"', async (t) => {
|
||||
const route = '/api/route';
|
||||
const query = '?profile=car&coords=-97,30;-97.01,30.01';
|
||||
for (const response of [
|
||||
() => new Response('offline', { status: 503 }),
|
||||
() => new Response('blocked', { status: 403 }),
|
||||
() => new Response('x', { headers: { 'content-type': 'text/html' } }),
|
||||
() =>
|
||||
new Response('<html>', {
|
||||
headers: { 'content-type': 'application/json' },
|
||||
}),
|
||||
() => Promise.reject(new TypeError('fetch failed')),
|
||||
() => Promise.reject(new DOMException('aborted', 'AbortError')),
|
||||
]) {
|
||||
_resetRouteUpstreamForTest();
|
||||
const request = install(installRouteMiddleware);
|
||||
t.mock.method(globalThis, 'fetch', async () => response());
|
||||
const result = await request(route, query);
|
||||
assert.equal(result.statusCode, 502);
|
||||
assert.deepEqual(result.body, {
|
||||
ok: false,
|
||||
error: 'routing service unavailable',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('callers sharing one failed upstream call all hear the outage, and it is not cached', async (t) => {
|
||||
_resetRouteUpstreamForTest();
|
||||
_setRouteUpstreamIntervalForTest(0);
|
||||
t.after(() => _resetRouteUpstreamForTest());
|
||||
const request = install(installRouteMiddleware);
|
||||
let calls = 0;
|
||||
let release;
|
||||
const gate = new Promise((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
t.mock.method(globalThis, 'fetch', async () => {
|
||||
calls += 1;
|
||||
await gate;
|
||||
return new Response('offline', { status: 503 });
|
||||
});
|
||||
const route = '/api/route';
|
||||
const query = '?profile=car&coords=-97,30;-97.01,30.01';
|
||||
const all = [
|
||||
request(route, `${query}&steps=1`),
|
||||
request(route, `${query}&steps=1`),
|
||||
request(route, query),
|
||||
];
|
||||
await Promise.resolve();
|
||||
release();
|
||||
for (const result of await Promise.all(all)) {
|
||||
assert.equal(result.statusCode, 502);
|
||||
assert.equal(result.body.error, 'routing service unavailable');
|
||||
}
|
||||
assert.equal(calls, 1);
|
||||
assert.equal((await request(route, query)).statusCode, 502);
|
||||
assert.equal(calls, 2, 'the next request asks the routing service again');
|
||||
});
|
||||
|
||||
test('a wait for an outbound slot does not count toward the upstream timeout', async (t) => {
|
||||
_resetRouteUpstreamForTest();
|
||||
// The second request waits 13 s for its slot, longer than the 12 s budget.
|
||||
_setRouteUpstreamIntervalForTest(13_000);
|
||||
t.after(() => _resetRouteUpstreamForTest());
|
||||
t.mock.timers.enable({ apis: ['setTimeout', 'Date'], now: Date.now() });
|
||||
const request = install(installRouteMiddleware);
|
||||
t.mock.method(globalThis, 'fetch', async (_url, { signal }) => {
|
||||
signal.throwIfAborted();
|
||||
return Response.json(osrmRouteWithSteps(2));
|
||||
});
|
||||
const first = request(
|
||||
'/api/route',
|
||||
'?profile=car&coords=-97,30;-97.01,30.01',
|
||||
);
|
||||
const second = request(
|
||||
'/api/route',
|
||||
'?profile=foot&coords=-97,30;-97.01,30.01',
|
||||
);
|
||||
assert.equal((await first).body.ok, true);
|
||||
t.mock.timers.tick(13_000);
|
||||
const result = await second;
|
||||
assert.equal(result.statusCode, 200);
|
||||
assert.equal(result.body.ok, true);
|
||||
});
|
||||
|
||||
/** One OSRM route with `count` maneuvers, for the step tests. */
|
||||
function osrmRouteWithSteps(count) {
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user