mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
fix(server): rest the routing relay after a failure and keep it under the installation's minute
A hung TREK Valhalla cost every leg, height chunk and trace the full 30 s before FOSSGIS answered. The relay now waits 10 s for a route or heights, rests a minute after an error or timeout and answers 503 at once meanwhile, follows a 429's Retry-After (an hour at most), passes that header on to the browser, and holds the installation to 100 questions a minute below the gate's 120 (60 per user).
This commit is contained in:
@@ -172,11 +172,12 @@ const FOSSGIS_VALHALLA = 'https://valhalla1.openstreetmap.de';
|
||||
|
||||
/**
|
||||
* 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 host that is down.
|
||||
* The question then goes to FOSSGIS, as the planner in the browser sends it.
|
||||
* 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.
|
||||
*/
|
||||
function relayCannotAnswer(status: number): boolean {
|
||||
return status === 401 || status === 403 || status === 413 || status === 429 || status >= 500;
|
||||
return [401, 403, 404, 405, 413, 429].includes(status) || status >= 500;
|
||||
}
|
||||
|
||||
/** The relay's answer read as the queue reads a router's: the payload, a refusal, or a failure. */
|
||||
|
||||
@@ -45,6 +45,8 @@ export class RoutingRelayController {
|
||||
}
|
||||
}
|
||||
|
||||
/** Of the upstream's headers only Retry-After goes on, so the planner knows how long to ask FOSSGIS instead. */
|
||||
function passOn(res: Response, answer: ValhallaRelayAnswer): void {
|
||||
if (answer.retryAfter) res.set('Retry-After', answer.retryAfter);
|
||||
res.status(answer.status).type(answer.contentType).send(answer.body);
|
||||
}
|
||||
|
||||
@@ -10,17 +10,31 @@ import type { ConfigType } from '@nestjs/config';
|
||||
import { VALHALLA_RELAY_REQUEST_SCHEMAS, compactValhallaRequest, type ValhallaRelayAction } from '@trek/shared';
|
||||
|
||||
/** Questions one user may put to the TREK Valhalla a minute, the browser and the server's own routing together. */
|
||||
const QUESTIONS_PER_MINUTE = 120;
|
||||
const QUESTIONS_PER_MINUTE = 60;
|
||||
/**
|
||||
* Questions the whole installation may put a minute. The TREK services allow
|
||||
* a key 120 calls a minute across their services, so routing stays below
|
||||
* that and leaves the address and place search their share.
|
||||
*/
|
||||
const INSTALLATION_QUESTIONS_PER_MINUTE = 100;
|
||||
const RATE_WINDOW_MS = 60_000;
|
||||
const DEFAULT_TIMEOUT_MS = 30_000;
|
||||
/** Short, because FOSSGIS still has to answer behind it while the planner waits. */
|
||||
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. */
|
||||
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;
|
||||
/** A day with alternates, or the description of a long line, stays far below this. */
|
||||
const MAX_ANSWER_BYTES = 8 * 1024 * 1024;
|
||||
|
||||
/** Valhalla's answer as it came: its status, its content type and its body untouched. */
|
||||
/** Valhalla's answer as it came: its status, its content type, its body untouched and the wait a refusal asks for. */
|
||||
export interface ValhallaRelayAnswer {
|
||||
status: number;
|
||||
contentType: string;
|
||||
body: Buffer;
|
||||
retryAfter?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,9 +44,15 @@ export interface ValhallaRelayAnswer {
|
||||
* TREK_SERVICES_HOSTS lists, so a browser never sees the key and a user
|
||||
* cannot send it anywhere else. Every question counts against the user's
|
||||
* allowance here, whichever surface asks it.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
@Injectable()
|
||||
export class RoutingRelayService {
|
||||
private restingUntil = 0;
|
||||
|
||||
constructor(
|
||||
@Inject(servicesConfig.KEY) private readonly config: ConfigType<typeof servicesConfig>,
|
||||
private readonly identity: ServiceIdentityPort,
|
||||
@@ -53,7 +73,7 @@ export class RoutingRelayService {
|
||||
userId: number,
|
||||
action: ValhallaRelayAction,
|
||||
body: object,
|
||||
timeoutMs = DEFAULT_TIMEOUT_MS,
|
||||
timeoutMs = action === 'trace_attributes' ? TRACE_TIMEOUT_MS : TIMEOUT_MS,
|
||||
): Promise<ValhallaRelayAnswer> {
|
||||
const parsed = VALHALLA_RELAY_REQUEST_SCHEMAS[action].safeParse(compactValhallaRequest(action, body));
|
||||
if (!parsed.success) {
|
||||
@@ -62,14 +82,26 @@ export class RoutingRelayService {
|
||||
const url = this.endpoint(action);
|
||||
const headers = this.keyHeaders(url);
|
||||
if (!headers) throw new DomainError(503, 'The TREK routing service is not available on this instance');
|
||||
const now = Date.now();
|
||||
if (now < this.restingUntil) {
|
||||
return refusal(503, 'The TREK routing service is resting', this.restingUntil - now);
|
||||
}
|
||||
const allowed = await this.rateLimit.check(
|
||||
'valhalla-relay',
|
||||
String(userId),
|
||||
QUESTIONS_PER_MINUTE,
|
||||
RATE_WINDOW_MS,
|
||||
Date.now(),
|
||||
now,
|
||||
);
|
||||
if (!allowed) throw new DomainError(429, 'Too many routing requests, try again in a minute');
|
||||
const withinInstallation = await this.rateLimit.check(
|
||||
'valhalla-relay-installation',
|
||||
'all',
|
||||
INSTALLATION_QUESTIONS_PER_MINUTE,
|
||||
RATE_WINDOW_MS,
|
||||
now,
|
||||
);
|
||||
if (!withinInstallation) return refusal(429, 'Too many routing requests on this instance', RATE_WINDOW_MS);
|
||||
return this.forward(url, headers, parsed.data, timeoutMs);
|
||||
}
|
||||
|
||||
@@ -93,19 +125,28 @@ export class RoutingRelayService {
|
||||
MAX_ANSWER_BYTES,
|
||||
);
|
||||
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);
|
||||
const { bytes, truncated } = await readCapped(response, MAX_ANSWER_BYTES);
|
||||
if (truncated) throw new Error('the answer is larger than the relay passes on');
|
||||
return {
|
||||
status: response.status,
|
||||
contentType: response.headers.get('content-type') ?? 'application/json',
|
||||
body: bytes,
|
||||
...(retryAfter ? { retryAfter } : {}),
|
||||
};
|
||||
} catch (err) {
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
private rest(ms: number): void {
|
||||
this.restingUntil = Math.max(this.restingUntil, Date.now() + Math.min(ms, MAX_REST_MS));
|
||||
}
|
||||
|
||||
private endpoint(action: ValhallaRelayAction): URL {
|
||||
return new URL(`${this.config.valhallaUrl}/${action}`);
|
||||
}
|
||||
@@ -117,3 +158,22 @@ export class RoutingRelayService {
|
||||
return headers.Authorization ? headers : null;
|
||||
}
|
||||
}
|
||||
|
||||
/** A refusal of the relay's own, in the body the exception filter writes, with the wait it asks for. */
|
||||
function refusal(status: number, error: string, waitMs: number): ValhallaRelayAnswer {
|
||||
return {
|
||||
status,
|
||||
contentType: 'application/json',
|
||||
body: Buffer.from(JSON.stringify({ error })),
|
||||
retryAfter: String(Math.max(1, Math.ceil(waitMs / 1000))),
|
||||
};
|
||||
}
|
||||
|
||||
/** A Retry-After in milliseconds, given as seconds or as an HTTP date; null when there is none to read. */
|
||||
function waitOf(header: string | null): number | null {
|
||||
const value = header?.trim();
|
||||
if (!value) return null;
|
||||
if (/^\d+$/.test(value)) return Number(value) * 1000;
|
||||
const at = Date.parse(value);
|
||||
return Number.isNaN(at) ? null : Math.max(0, at - Date.now());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user