mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
routing: a slow route is not a down relay, and the relay takes whole days
A request to the TREK Valhalla that runs out its deadline (a long bike route) is answered 504 and no longer rests the relay for every user or a tab for ten minutes; that one day goes to OSRM. Only an unreachable host or a server error rests it. A day goes to the relay with up to twenty stops per request and no pause between pieces; a request that ends up at the public FOSSGIS Valhalla is cut to ten and paced, as before. The 1.1 s spacing of the server applies to the public hosts only, not to an admin's own OSRM or Valhalla. The relay keeps its connections open and asks for maneuvers only, which the relay contract now accepts.
This commit is contained in:
@@ -173,17 +173,43 @@ async function refusalOf(response: Response): Promise<'no_way' | 'too_long' | nu
|
||||
|
||||
/** The public Valhalla FOSSGIS runs, which TREK ships with. */
|
||||
const FOSSGIS_VALHALLA = 'https://valhalla1.openstreetmap.de';
|
||||
/** The public OSRM hosts FOSSGIS runs, one per profile, which TREK ships with. */
|
||||
const FOSSGIS_OSRM = 'https://routing.openstreetmap.de';
|
||||
/** Locations one request to FOSSGIS may carry; the eleventh is refused with error 150. */
|
||||
const FOSSGIS_MAX_LOCATIONS = 10;
|
||||
|
||||
/** Whether a question goes to one of the public hosts, which allow about one request a second. */
|
||||
function isPublicHost(url: string): boolean {
|
||||
return url.startsWith(`${FOSSGIS_VALHALLA}/`) || url.startsWith(`${FOSSGIS_OSRM}/`);
|
||||
}
|
||||
|
||||
/**
|
||||
* A piece of a day cut for the relay, twenty locations, that has to go to
|
||||
* FOSSGIS instead, which takes ten: the day is cut again for it (`valhallaLegs`).
|
||||
*/
|
||||
class TooManyForFossgis extends Error {
|
||||
constructor() {
|
||||
super('The route carries more locations than FOSSGIS takes');
|
||||
this.name = 'TooManyForFossgis';
|
||||
}
|
||||
}
|
||||
|
||||
/** The locations a route body carries, none for a question of another kind. */
|
||||
function locationCount(body: object): number {
|
||||
const locations = (body as { locations?: unknown }).locations;
|
||||
return Array.isArray(locations) ? locations.length : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* What the relay says when it cannot answer for the TREK services right now:
|
||||
* no key, an allowance spent, a body it does not take, a path the host does
|
||||
* not know, a host that is down or resting. The question then goes to
|
||||
* FOSSGIS, as the planner in the browser sends it.
|
||||
* FOSSGIS, as the planner in the browser sends it. A 504 is not among them:
|
||||
* that one question took too long, FOSSGIS would take as long over it, and
|
||||
* the relay answers the next one as before.
|
||||
*/
|
||||
function relayCannotAnswer(status: number): boolean {
|
||||
return [401, 403, 404, 405, 413, 429].includes(status) || status >= 500;
|
||||
return [401, 403, 404, 405, 413, 429].includes(status) || (status >= 500 && status !== 504);
|
||||
}
|
||||
|
||||
/** The relay's answer read as the queue reads a router's: the payload, a refusal, or a failure. */
|
||||
@@ -342,7 +368,13 @@ export class RoadtripRouterService {
|
||||
return ['driving', 'walking', 'cycling', ...plugin];
|
||||
}
|
||||
|
||||
/**
|
||||
* One question to a router. The public hosts are asked one after another
|
||||
* through a queue that leaves 1.1 s between them, as they ask; an admin's
|
||||
* own OSRM or Valhalla sets its own limits and is asked at once.
|
||||
*/
|
||||
private async request(url: string, body?: unknown, timeoutMs = 20000): Promise<unknown> {
|
||||
if (!isPublicHost(url)) return this.fetchPayload(url, body, timeoutMs);
|
||||
const before = this.queue;
|
||||
let release!: () => void;
|
||||
this.queue = new Promise<void>((resolve) => {
|
||||
@@ -353,34 +385,39 @@ export class RoadtripRouterService {
|
||||
const delay = this.nextRequest - Date.now();
|
||||
if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay));
|
||||
this.nextRequest = Date.now() + 1100;
|
||||
const response = await safeFetchAdminConfigured(url, {
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
headers: { 'User-Agent': 'TREK Roadtrip', 'Content-Type': 'application/json', 'X-Client-Id': 'trek' },
|
||||
...(body ? { method: 'POST', body: JSON.stringify(body) } : {}),
|
||||
});
|
||||
if (!response.ok) {
|
||||
// A 400 from Valhalla says why: a way that does not exist is no failure of the host.
|
||||
if (response.status !== 400) discardBody(response);
|
||||
else {
|
||||
const why = await refusalOf(response);
|
||||
if (why) throw new RouteRefusal(why, 'Routing provider returned 400');
|
||||
}
|
||||
throw new Error(`Routing provider returned ${response.status}`);
|
||||
}
|
||||
const payload = await readCappedJson<unknown>(response, 8 * 1024 * 1024);
|
||||
if (payload === undefined) throw new Error('Routing response is invalid or too large');
|
||||
return payload;
|
||||
return await this.fetchPayload(url, body, timeoutMs);
|
||||
} finally {
|
||||
release();
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchPayload(url: string, body: unknown, timeoutMs: number): Promise<unknown> {
|
||||
const response = await safeFetchAdminConfigured(url, {
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
headers: { 'User-Agent': 'TREK Roadtrip', 'Content-Type': 'application/json', 'X-Client-Id': 'trek' },
|
||||
...(body ? { method: 'POST', body: JSON.stringify(body) } : {}),
|
||||
});
|
||||
if (!response.ok) {
|
||||
// A 400 from Valhalla says why: a way that does not exist is no failure of the host.
|
||||
if (response.status !== 400) discardBody(response);
|
||||
else {
|
||||
const why = await refusalOf(response);
|
||||
if (why) throw new RouteRefusal(why, 'Routing provider returned 400');
|
||||
}
|
||||
throw new Error(`Routing provider returned ${response.status}`);
|
||||
}
|
||||
const payload = await readCappedJson<unknown>(response, 8 * 1024 * 1024);
|
||||
if (payload === undefined) throw new Error('Routing response is invalid or too large');
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* One question to a Valhalla. The TREK services' one is asked through the
|
||||
* relay, under the user's allowance there and with the installation key; a
|
||||
* relay that cannot answer hands the question to FOSSGIS through the paced
|
||||
* queue, unless `publicFallback` is off. Every other Valhalla is asked
|
||||
* through the queue directly.
|
||||
* queue, unless `publicFallback` is off. A question the relay ran out of
|
||||
* time on fails as it is, without FOSSGIS. Every other Valhalla is asked
|
||||
* through `request` directly.
|
||||
*/
|
||||
private async askValhalla(
|
||||
userId: number,
|
||||
@@ -391,22 +428,24 @@ export class RoadtripRouterService {
|
||||
): Promise<unknown> {
|
||||
if (!this.relay.serves(valhalla)) return this.request(`${valhalla}/${action}`, body, timeoutMs);
|
||||
const answer = await this.relay.ask(userId, action, body, timeoutMs).catch((err: unknown) => {
|
||||
if (err instanceof DomainError && err.getStatus() === 504) throw new Error(err.message);
|
||||
if (err instanceof DomainError) return null;
|
||||
throw err;
|
||||
});
|
||||
if (answer && !relayCannotAnswer(answer.status)) return payloadOf(answer);
|
||||
if (!publicFallback) throw new Error('The TREK routing service cannot answer');
|
||||
if (locationCount(body) > FOSSGIS_MAX_LOCATIONS) throw new TooManyForFossgis();
|
||||
return this.request(`${FOSSGIS_VALHALLA}/${action}`, body, timeoutMs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locations one route request carries: ten wherever it may end up at
|
||||
* FOSSGIS, twenty at the TREK Valhalla asked alone and at an own one, as the
|
||||
* planner splits a day (`locationsPerRequest`).
|
||||
* Locations one route request carries: ten at FOSSGIS, twenty at the TREK
|
||||
* Valhalla and at an own one, as the planner splits a day
|
||||
* (`locationsPerRequest`). A piece cut for the relay that ends up at FOSSGIS
|
||||
* has the day cut again (`valhallaLegs`).
|
||||
*/
|
||||
private locationsPerRequest(valhalla: string, publicFallback: boolean): number {
|
||||
const mayReachPublic = valhalla === FOSSGIS_VALHALLA || (this.relay.serves(valhalla) && publicFallback);
|
||||
return mayReachPublic ? FOSSGIS_MAX_LOCATIONS : VALHALLA_RELAY_MAX_LOCATIONS;
|
||||
private locationsPerRequest(valhalla: string): number {
|
||||
return valhalla === FOSSGIS_VALHALLA ? FOSSGIS_MAX_LOCATIONS : VALHALLA_RELAY_MAX_LOCATIONS;
|
||||
}
|
||||
|
||||
async route(
|
||||
@@ -612,9 +651,11 @@ export class RoadtripRouterService {
|
||||
|
||||
/**
|
||||
* Valhalla's legs through the points, as many locations a request as the
|
||||
* host takes (`locationsPerRequest`), one after another through the paced
|
||||
* queue, each piece starting on the point the one before ended at. CW-12: a
|
||||
* walk on foot binds no point to a ferry's edge (`offBoatLines`).
|
||||
* host takes (`locationsPerRequest`), one after another, each piece starting
|
||||
* on the point the one before ended at. Where the relay cannot answer a
|
||||
* piece of twenty and FOSSGIS has to, the whole day is asked again in
|
||||
* pieces of ten. CW-12: a walk on foot binds no point to a ferry's edge
|
||||
* (`offBoatLines`).
|
||||
*/
|
||||
private async valhallaLegs(
|
||||
userId: number,
|
||||
@@ -622,10 +663,27 @@ export class RoadtripRouterService {
|
||||
points: Point[],
|
||||
costing: ValhallaCosting,
|
||||
options: Record<string, number | string>,
|
||||
{ offBoatLines = false, publicFallback = true }: { offBoatLines?: boolean; publicFallback?: boolean } = {},
|
||||
ask: { offBoatLines?: boolean; publicFallback?: boolean } = {},
|
||||
): Promise<ValhallaLegs> {
|
||||
const size = this.locationsPerRequest(valhalla);
|
||||
try {
|
||||
return await this.legsInPieces(userId, valhalla, points, costing, options, ask, size);
|
||||
} catch (err) {
|
||||
if (!(err instanceof TooManyForFossgis)) throw err;
|
||||
return this.legsInPieces(userId, valhalla, points, costing, options, ask, FOSSGIS_MAX_LOCATIONS);
|
||||
}
|
||||
}
|
||||
|
||||
private async legsInPieces(
|
||||
userId: number,
|
||||
valhalla: string,
|
||||
points: Point[],
|
||||
costing: ValhallaCosting,
|
||||
options: Record<string, number | string>,
|
||||
{ offBoatLines = false, publicFallback = true }: { offBoatLines?: boolean; publicFallback?: boolean },
|
||||
size: number,
|
||||
): Promise<ValhallaLegs> {
|
||||
const legs: ValhallaLegs = [];
|
||||
const size = this.locationsPerRequest(valhalla, publicFallback);
|
||||
for (let start = 0; start < points.length - 1; start += size - 1) {
|
||||
const body = {
|
||||
locations: points.slice(start, start + size).map((p) => ({
|
||||
@@ -637,6 +695,8 @@ export class RoadtripRouterService {
|
||||
costing,
|
||||
costing_options: { [costing]: options },
|
||||
directions_options: { units: 'kilometers' },
|
||||
// The maneuvers, where a walk boards a ferry, without the narrative TREK never reads.
|
||||
directions_type: 'maneuvers',
|
||||
};
|
||||
const payload = await this.askValhalla(userId, valhalla, 'route', body, { publicFallback });
|
||||
legs.push(...valhallaSchema.parse(payload).trip.legs);
|
||||
@@ -767,6 +827,7 @@ export class RoadtripRouterService {
|
||||
costing: 'pedestrian',
|
||||
costing_options: { pedestrian: { ...costingOptions } },
|
||||
directions_options: { units: 'kilometers' },
|
||||
directions_type: 'maneuvers',
|
||||
...(count > 0 ? { alternates: count } : {}),
|
||||
});
|
||||
const parsed = alternatesSchema.parse(payload);
|
||||
|
||||
@@ -23,11 +23,16 @@ const QUESTIONS_PER_MINUTE = 60;
|
||||
*/
|
||||
const INSTALLATION_QUESTIONS_PER_MINUTE = 100;
|
||||
const RATE_WINDOW_MS = 60_000;
|
||||
/** Short, because FOSSGIS still has to answer behind it while the planner waits. */
|
||||
/**
|
||||
* Short, because FOSSGIS still has to answer behind it while the planner waits. A
|
||||
* question that runs past it is answered 504 and leaves the host asked: a bike route
|
||||
* of 300 km takes the TREK Valhalla about twelve seconds, and that says nothing about
|
||||
* the next question.
|
||||
*/
|
||||
const TIMEOUT_MS = 10_000;
|
||||
/** Matching a long line takes Valhalla a while, on FOSSGIS as much as here. */
|
||||
const TRACE_TIMEOUT_MS = 30_000;
|
||||
/** How long every question goes to FOSSGIS after the TREK Valhalla failed one. */
|
||||
/** How long every question goes to FOSSGIS after the TREK Valhalla could not be reached or failed one. */
|
||||
const REST_MS = 60_000;
|
||||
/** The longest wait a Retry-After is followed for; the first question after it finds out whether the allowance is back. */
|
||||
const MAX_REST_MS = 60 * 60_000;
|
||||
@@ -53,6 +58,8 @@ export interface ValhallaRelayAnswer {
|
||||
* A host that failed a question, or refused it for a spent allowance, is left
|
||||
* alone for a while: the relay then answers 503 at once and every caller asks
|
||||
* FOSSGIS, instead of each leg of a long day waiting out the deadline again.
|
||||
* A question that only took too long is no such failure: it is answered 504,
|
||||
* and the next one is asked as before.
|
||||
*/
|
||||
@Injectable()
|
||||
export class RoutingRelayService {
|
||||
@@ -130,11 +137,14 @@ export class RoutingRelayService {
|
||||
0,
|
||||
undefined,
|
||||
MAX_ANSWER_BYTES,
|
||||
// One host asked many times in a row: its connection is kept rather than opened per question.
|
||||
true,
|
||||
);
|
||||
this.identity.observe(url, response);
|
||||
const retryAfter = response.headers.get('retry-after');
|
||||
if (response.status === 429) this.rest(waitOf(retryAfter) ?? REST_MS);
|
||||
else if (response.status >= 500) this.rest(REST_MS);
|
||||
// A 504 of the host's own proxy is a slow question as much as the relay's deadline is.
|
||||
else if (response.status >= 500 && response.status !== 504) this.rest(REST_MS);
|
||||
const { bytes, truncated } = await readCapped(response, MAX_ANSWER_BYTES);
|
||||
if (truncated) throw new Error('the answer is larger than the relay passes on');
|
||||
return {
|
||||
@@ -144,9 +154,14 @@ export class RoutingRelayService {
|
||||
...(retryAfter ? { retryAfter } : {}),
|
||||
};
|
||||
} catch (err) {
|
||||
this.rest(REST_MS);
|
||||
// A question that ran out its deadline is a slow question, not a host that is down:
|
||||
// only a host that could not be reached, or sent more than is passed on, rests.
|
||||
const timedOut = err instanceof DOMException && err.name === 'TimeoutError';
|
||||
if (!timedOut) this.rest(REST_MS);
|
||||
logWarn(`[Routing relay] ${url.pathname}: ${err instanceof Error ? err.message : String(err)}`);
|
||||
throw new DomainError(502, 'The TREK routing service did not answer');
|
||||
throw timedOut
|
||||
? new DomainError(504, 'The TREK routing service took too long for this route')
|
||||
: new DomainError(502, 'The TREK routing service did not answer');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -261,6 +261,10 @@ function isLinkLocal(ip: string): boolean {
|
||||
* legitimately wait longer (see safeFetchLlm). Left unset, the caller's own
|
||||
* signal governs, and a call without one waits DEFAULT_RESPONSE_TIMEOUT_MS for
|
||||
* the response headers. `maxBytes` caps the body as in SafeFetchOptions.
|
||||
*
|
||||
* `reuseConnections` keeps the dispatcher for the next call to the same vetted
|
||||
* addresses (see keptDispatcher), so a caller that asks one host many times in
|
||||
* a row, the routing relay, does not pay DNS, TCP and TLS on every question.
|
||||
*/
|
||||
export async function safeFetchAdminConfigured(
|
||||
url: string,
|
||||
@@ -268,6 +272,7 @@ export async function safeFetchAdminConfigured(
|
||||
maxRedirects = 5,
|
||||
responseTimeoutMs?: number,
|
||||
maxBytes: number | null = DEFAULT_MAX_RESPONSE_BYTES,
|
||||
reuseConnections = false,
|
||||
): Promise<Response> {
|
||||
let currentUrl = url;
|
||||
let hopInit = init;
|
||||
@@ -298,7 +303,14 @@ export async function safeFetchAdminConfigured(
|
||||
throw new SsrfBlockedError('Requests to link-local / cloud-metadata addresses are not allowed');
|
||||
}
|
||||
|
||||
const dispatcher = createOutboundDispatcher(currentUrl, usableIps, true, responseTimeoutMs, headersTimeoutMs);
|
||||
const dispatcher = createOutboundDispatcher(
|
||||
currentUrl,
|
||||
usableIps,
|
||||
true,
|
||||
responseTimeoutMs,
|
||||
headersTimeoutMs,
|
||||
reuseConnections,
|
||||
);
|
||||
const response = await fetch(currentUrl, { ...hopInit, redirect: 'manual', dispatcher } as any);
|
||||
|
||||
// Only a 3xx WITH a Location header is a redirect we follow; anything else
|
||||
@@ -683,14 +695,57 @@ export function createOutboundDispatcher(
|
||||
rejectUnauthorized = true,
|
||||
responseTimeoutMs?: number,
|
||||
headersTimeoutMs?: number,
|
||||
reuseConnections = false,
|
||||
): Dispatcher {
|
||||
const proxy = proxyFor(url);
|
||||
if (!proxy) return createPinnedDispatcher(resolved, rejectUnauthorized, responseTimeoutMs, headersTimeoutMs);
|
||||
return new ProxyAgent({
|
||||
uri: proxy,
|
||||
requestTls: { rejectUnauthorized },
|
||||
...dispatcherTimeouts(responseTimeoutMs, headersTimeoutMs),
|
||||
});
|
||||
const keepAliveMs = reuseConnections ? KEPT_ALIVE_MS : undefined;
|
||||
const create = (): Dispatcher =>
|
||||
proxy
|
||||
? new ProxyAgent({
|
||||
uri: proxy,
|
||||
requestTls: { rejectUnauthorized },
|
||||
...dispatcherTimeouts(responseTimeoutMs, headersTimeoutMs),
|
||||
...(keepAliveMs ? { keepAliveTimeout: keepAliveMs } : {}),
|
||||
})
|
||||
: createPinnedDispatcher(resolved, rejectUnauthorized, responseTimeoutMs, headersTimeoutMs, keepAliveMs);
|
||||
if (!reuseConnections) return create();
|
||||
// Through a proxy the proxy connects, so the addresses play no part in what the dispatcher does.
|
||||
const addresses = proxy ? [] : typeof resolved === 'string' ? [resolved] : [...resolved];
|
||||
const key = JSON.stringify([
|
||||
proxy,
|
||||
addresses,
|
||||
rejectUnauthorized,
|
||||
responseTimeoutMs ?? null,
|
||||
headersTimeoutMs ?? null,
|
||||
]);
|
||||
return keptDispatcher(key, create);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatchers kept for the callers that reuse their connections, by everything
|
||||
* that makes one: the proxy, the vetted addresses, the TLS check and the
|
||||
* timeouts. The addresses are part of the key, so the pin is as strict as a
|
||||
* new agent's: a name that now resolves elsewhere gets a dispatcher of its own
|
||||
* and never a connection to the old address. At most MAX_KEPT_DISPATCHERS are
|
||||
* kept; the oldest is closed, after its requests finish, when one more is needed.
|
||||
*/
|
||||
const keptDispatchers = new Map<string, Dispatcher>();
|
||||
const MAX_KEPT_DISPATCHERS = 32;
|
||||
/** How long a kept connection may sit idle, where undici's own default is four seconds. */
|
||||
const KEPT_ALIVE_MS = 10_000;
|
||||
|
||||
function keptDispatcher(key: string, create: () => Dispatcher): Dispatcher {
|
||||
const kept = keptDispatchers.get(key);
|
||||
if (kept) return kept;
|
||||
const dispatcher = create();
|
||||
keptDispatchers.set(key, dispatcher);
|
||||
const [oldestKey] = keptDispatchers.keys();
|
||||
if (keptDispatchers.size > MAX_KEPT_DISPATCHERS && oldestKey !== undefined) {
|
||||
const oldest = keptDispatchers.get(oldestKey);
|
||||
keptDispatchers.delete(oldestKey);
|
||||
void oldest?.close().catch(() => {});
|
||||
}
|
||||
return dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -717,12 +772,15 @@ function dispatcherTimeouts(
|
||||
* `all: true` and walks the list itself, moving on after its attempt timeout
|
||||
* when one family does not answer.
|
||||
* A caller that still names one address gets exactly the old behaviour.
|
||||
* `keepAliveMs` holds an idle connection open longer than undici's default,
|
||||
* for a dispatcher that is kept (keptDispatcher).
|
||||
*/
|
||||
export function createPinnedDispatcher(
|
||||
resolved: string | readonly string[],
|
||||
rejectUnauthorized = true,
|
||||
responseTimeoutMs?: number,
|
||||
headersTimeoutMs?: number,
|
||||
keepAliveMs?: number,
|
||||
): Agent {
|
||||
const addresses = (typeof resolved === 'string' ? [resolved] : [...resolved]).map((address) => ({
|
||||
address,
|
||||
@@ -733,6 +791,7 @@ export function createPinnedDispatcher(
|
||||
// that cap is invisible from the call site: an AbortController set to
|
||||
// fifteen still dies at five.
|
||||
...dispatcherTimeouts(responseTimeoutMs, headersTimeoutMs),
|
||||
...(keepAliveMs ? { keepAliveTimeout: keepAliveMs } : {}),
|
||||
connect: {
|
||||
rejectUnauthorized,
|
||||
lookup: (_hostname: string, opts: Record<string, unknown>, callback: Function) => {
|
||||
|
||||
Reference in New Issue
Block a user