mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
fix(tours): name the Overpass kinds of a search along a tour at once while every mirror is down (PLN-02, CW-05)
While the public Overpass mirrors do not answer, every search along a hike with viewpoints, huts or drinking water waited out the whole twelve second deadline only to name those kinds as not searchable. Once every mirror has failed the search along a line, and no other search along a line was answered in the meantime, the next searches for three minutes leave Overpass alone and name its kinds at once, next to the places the index found; a later stretch of a long line that was already being searched does not wait either. A stretch answered shortly before keeps its places from the cache during the pause. After the three minutes the next search asks again for real, and any answer ends the pause. The log holds one line per pause with the time it ends, next to the line of the search that failed. The explore buttons of the map and the other Overpass lookups are unchanged.
This commit is contained in:
@@ -10,7 +10,9 @@
|
||||
* Overpass at the same time, one query near each stretch of the whole line,
|
||||
* with a short deadline. A source that fails is logged and its kinds named in
|
||||
* `failed`, so the caller can say what could not be searched instead of
|
||||
* reporting nothing found.
|
||||
* reporting nothing found. Once every Overpass endpoint failed, the next
|
||||
* searches for a few minutes name its kinds at once instead of waiting out the
|
||||
* deadline each time, while a stretch still cached keeps its places.
|
||||
*
|
||||
* MapsService.poisAlong delegates here and hands in whether the index is
|
||||
* switched on, which is a deployment property it owns.
|
||||
@@ -25,7 +27,7 @@ import {
|
||||
ownedByAsked,
|
||||
} from './maps-index.helpers';
|
||||
import type { OverpassPoi } from './maps.helpers';
|
||||
import { OsmClient } from './providers/osm.client';
|
||||
import { OsmClient, OverpassCoolingDown } from './providers/osm.client';
|
||||
import { trekPlacesNearby } from './trek-places.client';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import type { LatLng } from '@trek/shared/roadtrip';
|
||||
@@ -89,7 +91,9 @@ function oncePerPlace(pois: OverpassPoi[]): OverpassPoi[] {
|
||||
|
||||
/**
|
||||
* Requests a few at a time, each batch after the last, so no more than a
|
||||
* batch is ever in flight; a failed one leaves the others.
|
||||
* batch is ever in flight; a failed one leaves the others. A stretch refused
|
||||
* while Overpass cools down is missed like any other, but makes no error of
|
||||
* its own: the client logs the cooldown once, not with every search.
|
||||
*/
|
||||
async function inBatches(requests: Array<() => Promise<Page>>, parallel: number): Promise<Batched> {
|
||||
const batches = Array.from({ length: Math.ceil(requests.length / parallel) }, (_, index) =>
|
||||
@@ -100,13 +104,14 @@ async function inBatches(requests: Array<() => Promise<Page>>, parallel: number)
|
||||
Promise.resolve([]),
|
||||
);
|
||||
const pages = settled.flatMap((entry) => (entry.status === 'fulfilled' ? [entry.value] : []));
|
||||
const misses = settled.flatMap((entry) => (entry.status === 'rejected' ? [reason(entry.reason)] : []));
|
||||
const misses = settled.flatMap((entry): unknown[] => (entry.status === 'rejected' ? [entry.reason] : []));
|
||||
const reported = misses.filter((miss) => !(miss instanceof OverpassCoolingDown));
|
||||
return {
|
||||
pois: pages.flatMap((page) => page.pois),
|
||||
truncated: pages.some((page) => page.truncated),
|
||||
answered: pages.length,
|
||||
missed: misses.length,
|
||||
error: misses.length ? `${misses.length} of ${settled.length} requests failed: ${misses[0]}` : null,
|
||||
error: reported.length ? `${misses.length} of ${settled.length} requests failed: ${reason(reported[0])}` : null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -167,7 +172,9 @@ export class PoisAlongResolver {
|
||||
|
||||
/**
|
||||
* The other kinds near each stretch of the whole line, one Overpass query
|
||||
* per stretch; nothing to ask is nothing found.
|
||||
* per stretch; nothing to ask is nothing found. While the client cools down
|
||||
* after every Overpass endpoint failed, a stretch it holds no cached answer
|
||||
* for is refused at once, so its kinds are unavailable without the wait.
|
||||
*/
|
||||
private async askOverpass(kinds: string[], query: PoisAlongQuery): Promise<SourceAnswer> {
|
||||
if (!kinds.length) return NOTHING;
|
||||
|
||||
@@ -11,11 +11,14 @@
|
||||
*
|
||||
* The POI cache and the mirror list are module state on purpose, frozen at load
|
||||
* the way they were in the service ("frozen on purpose", see
|
||||
* src/app-config/README.md), so every instance shares one cache.
|
||||
* src/app-config/README.md), so every instance shares one cache. The pause the
|
||||
* search along a line takes after every Overpass endpoint failed it is the
|
||||
* instance's own: MapsModule provides one client per process.
|
||||
*
|
||||
* Never imports MapsService: providers sit below the orchestrator
|
||||
* (lint:boundaries holds that).
|
||||
*/
|
||||
import { logWarn } from '../../audit/audit-log.logger';
|
||||
import { nominatimFetch, type GeoLane } from '../../geo/nominatim.client';
|
||||
import { OVERPASS_ALONG_QUERY_TIMEOUT_S, aroundPath, overpassAlongQuery } from '../maps-along.helpers';
|
||||
import { nearbyOverpassQuery, overpassNearbyRecords } from '../maps-nearby.helpers';
|
||||
@@ -317,8 +320,34 @@ function readPois(
|
||||
*/
|
||||
const OVERPASS_ALONG_TIMEOUT_MS = (OVERPASS_ALONG_QUERY_TIMEOUT_S + 2) * 1000;
|
||||
|
||||
/**
|
||||
* How long the search along a line leaves Overpass alone once every endpoint
|
||||
* failed it. While the public mirrors do not answer, each search would wait
|
||||
* out the whole deadline only to name the same kinds unavailable; after the
|
||||
* pause the next search asks again for real.
|
||||
*/
|
||||
export const OVERPASS_ALONG_COOLDOWN_MS = 3 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* A stretch of a search along a line refused during the cooldown: nothing was
|
||||
* asked, and the cooldown is logged once rather than with every search.
|
||||
*/
|
||||
export class OverpassCoolingDown extends Error {
|
||||
constructor() {
|
||||
super('Overpass unreachable, not retrying yet');
|
||||
this.name = 'OverpassCoolingDown';
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class OsmClient {
|
||||
/** Until when the search along a line leaves Overpass alone after every endpoint failed it; 0 while it asks. */
|
||||
private overpassAlongCooldownUntil = 0;
|
||||
/** When an along query was last answered, so a failure that settles after an answer starts no cooldown. */
|
||||
private overpassAlongAnsweredAt = 0;
|
||||
/** Whether this cooldown's first skip was logged, so the log holds one line per cooldown. */
|
||||
private overpassAlongSkipLogged = false;
|
||||
|
||||
// ── Nominatim search ───────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
@@ -721,7 +750,9 @@ export class OsmClient {
|
||||
* the cap is spent on the corridor rather than on the box. Unnamed taps,
|
||||
* huts and the like stay, because they are what a hike looks for, while an
|
||||
* unnamed garden or cafe is dropped as on the explore pill. A short deadline
|
||||
* of its own; failing throws as the explore pill's search does.
|
||||
* of its own; failing throws as the explore pill's search does. During the
|
||||
* cooldown after every endpoint failed, a stretch still cached is answered
|
||||
* from the cache and any other throws OverpassCoolingDown at once.
|
||||
*/
|
||||
async searchOverpassPoisAlong(
|
||||
category: string,
|
||||
@@ -739,8 +770,10 @@ export class OsmClient {
|
||||
const cacheKey = `along|${kinds}|${osmLang}|${path.radiusM}|${points}|${cap}`;
|
||||
const cached = cachedPoiAnswer(cacheKey);
|
||||
if (cached) return cached;
|
||||
// Neither a new search nor a later stretch of one under way waits for mirrors that just failed.
|
||||
if (this.skipsOverpassAlong()) throw new OverpassCoolingDown();
|
||||
|
||||
const elements = await overpassFetch(overpassAlongQuery(match.filters, path, cap), OVERPASS_ALONG_TIMEOUT_MS);
|
||||
const elements = await this.fetchOverpassAlong(overpassAlongQuery(match.filters, path, cap));
|
||||
const pois = readPois(elements, match, osmLang, KEPT_UNNAMED_ALONG);
|
||||
const value: PoiSearchResult = {
|
||||
pois: pois.slice(0, cap),
|
||||
@@ -751,4 +784,42 @@ export class OsmClient {
|
||||
cachePoiAnswer(cacheKey, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the search along a line leaves Overpass alone right now, because
|
||||
* every endpoint failed it within the last OVERPASS_ALONG_COOLDOWN_MS. Its
|
||||
* kinds are then unavailable at once instead of after the whole deadline.
|
||||
* The first skip of each cooldown is logged.
|
||||
*/
|
||||
skipsOverpassAlong(): boolean {
|
||||
if (Date.now() >= this.overpassAlongCooldownUntil) return false;
|
||||
if (!this.overpassAlongSkipLogged) {
|
||||
this.overpassAlongSkipLogged = true;
|
||||
const until = new Date(this.overpassAlongCooldownUntil).toISOString();
|
||||
logWarn(`[Maps] OpenStreetMap along a line: every Overpass endpoint failed, so it is not asked until ${until}`);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The along query on the mirrors. An answer ends the cooldown; every
|
||||
* endpoint failing starts it, unless another along query was answered while
|
||||
* this one was in flight: Overpass is up then, and only this query failed.
|
||||
*/
|
||||
private async fetchOverpassAlong(query: string): Promise<OverpassPoiElement[]> {
|
||||
const askedAt = Date.now();
|
||||
try {
|
||||
const elements = await overpassFetch(query, OVERPASS_ALONG_TIMEOUT_MS);
|
||||
this.overpassAlongAnsweredAt = Date.now();
|
||||
this.overpassAlongCooldownUntil = 0;
|
||||
return elements;
|
||||
} catch (err) {
|
||||
// overpassFetch throws only once every mirror has failed.
|
||||
if (this.overpassAlongAnsweredAt < askedAt) {
|
||||
this.overpassAlongCooldownUntil = Date.now() + OVERPASS_ALONG_COOLDOWN_MS;
|
||||
this.overpassAlongSkipLogged = false;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user