fix(maps): keep a slow Photon from holding back the suggestions

The suggestions waited for Photon beside the index for up to its full
2.5 s deadline on every keystroke, and their misses and spent allowance
paused Photon for the search and the reverse lookup as well.

Once the index has answered, Photon now gets 300 ms more and is then
called off; with nothing from the index it is still waited for. The
keystroke question has a shorter deadline and its own tally of misses and
pauses, a question called off by the list counts as no miss, and a pause
of the search still holds the suggestions back.

Refs #2877
This commit is contained in:
Maurice
2026-10-29 15:11:39 +01:00
parent 64f370fd0e
commit 28a577df5e
6 changed files with 194 additions and 50 deletions
@@ -1,5 +1,5 @@
/**
* MAPS-AUTO-001..024: the suggestion list behind the place search box.
* MAPS-AUTO-001..026: the suggestion list behind the place search box.
*
* This is the path the index was built for. Nominatim's usage policy names
* autocomplete as unacceptable use in its own words, whatever the rate, and the
@@ -11,6 +11,7 @@
*/
import type { AppSettingsRepository } from '../../../src/db/repositories/AppSettings.repository';
import type { UsersRepository } from '../../../src/db/repositories/Users.repository';
import { PHOTON_GRACE_MS } from '../../../src/nest/maps/maps-index.helpers';
import type { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
import { noGoogleQuota } from '../../helpers/google-quota';
import { buildMapsParts, buildMapsService } from '../../helpers/maps-service';
@@ -475,7 +476,7 @@ describe('MapsService.autocompletePlaces', () => {
const result = await svc.autocompletePlaces(1, INPUT, 'de', bias);
expect(photon).toHaveBeenCalledWith(INPUT, 'de', { lat: 54.1, lng: 12.1 });
expect(photon).toHaveBeenCalledWith(INPUT, 'de', { lat: 54.1, lng: 12.1 }, expect.any(AbortSignal));
expect(result.source).toBe('trek-places');
expect(result.suggestions.map((s) => s.placeId)).toEqual([
'gers:abc-123',
@@ -516,4 +517,33 @@ describe('MapsService.autocompletePlaces', () => {
expect(result.source).toBe('google');
vi.unstubAllGlobals();
});
it('MAPS-AUTO-025: once the index has answered, a slow Photon gets a moment more and is then dropped', async () => {
mockSearch.mockResolvedValue([hit()]);
const { svc, photon } = withPhoton(null);
photon.mockImplementation(() => new Promise(() => undefined));
const started = Date.now();
const result = await svc.autocompletePlaces(1, INPUT);
expect(Date.now() - started).toBeGreaterThanOrEqual(PHOTON_GRACE_MS - 50);
expect(result).toEqual({
suggestions: [expect.objectContaining({ placeId: 'gers:abc-123' })],
source: 'trek-places',
});
// The question nobody waits for any more is called off, which is no miss for Photon.
expect((photon.mock.calls[0][3] as AbortSignal).aborted).toBe(true);
});
it('MAPS-AUTO-026: with nothing from the index, Photon is waited for past that moment', async () => {
mockSearch.mockResolvedValue([]);
const { svc, photon } = withPhoton(null);
photon.mockImplementation(
() => new Promise((resolve) => setTimeout(() => resolve([photonRow()]), PHOTON_GRACE_MS + 100)),
);
const result = await svc.autocompletePlaces(1, INPUT);
expect(result).toEqual({ suggestions: [photonRow()], source: 'openstreetmap' });
});
});
+61 -1
View File
@@ -1,5 +1,5 @@
/**
* PHOTON-001..016: when Photon is asked, what it is asked, and every way it
* PHOTON-001..019: when Photon is asked, what it is asked, and every way it
* hands the question back to Nominatim. The network is a stubbed global fetch,
* the installation identity a fake in the slot the service-identity module fills.
*/
@@ -197,3 +197,63 @@ describe('a spent allowance and a short deadline', () => {
timeout.mockRestore();
});
});
describe('the suggestions asked per keystroke', () => {
it('PHOTON-017: their misses and a spent allowance pause the suggestions alone, never the search', async () => {
vi.useFakeTimers({ toFake: ['Date'] });
vi.setSystemTime(new Date('2026-10-29T10:00:00Z'));
const photon = client();
fetchMock.mockImplementation(async () => photonAnswer([], 503));
expect(await oneByOne([1, 2, 3, 4], () => photon.suggest('Ros'))).toEqual([null, null, null, null]);
expect(await photon.suggest('Rost')).toBeNull();
expect(fetchMock).toHaveBeenCalledTimes(4);
fetchMock.mockImplementation(async () => photonAnswer([gateFeature()]));
expect(await photon.search('Rostock')).toHaveLength(1);
expect(fetchMock).toHaveBeenCalledTimes(5);
vi.setSystemTime(new Date('2026-10-29T10:01:01Z'));
fetchMock.mockResolvedValueOnce(
new Response('{}', { status: 429, headers: { 'content-type': 'application/json', 'retry-after': '600' } }),
);
expect(await photon.suggest('Rost')).toBeNull();
expect(await photon.suggest('Rosto')).toBeNull();
expect(await photon.search('Rostock')).toHaveLength(1);
expect(await photon.reverse(52.5, 13.4)).toHaveLength(1);
expect(fetchMock).toHaveBeenCalledTimes(8);
});
it('PHOTON-018: a pause of the search holds them back too', async () => {
vi.useFakeTimers({ toFake: ['Date'] });
vi.setSystemTime(new Date('2026-10-29T10:00:00Z'));
const photon = client();
fetchMock.mockImplementation(async () => photonAnswer([], 503));
await oneByOne([1, 2, 3, 4], () => photon.search('Rostock'));
expect(await photon.suggest('Ros')).toBeNull();
expect(fetchMock).toHaveBeenCalledTimes(4);
});
it('PHOTON-019: they wait less than a search, and one called off by its caller is no miss', async () => {
const timeout = vi.spyOn(AbortSignal, 'timeout');
const photon = client();
expect(await photon.suggest('Ros', { lang: 'de', bias: { lat: 54.1, lng: 12.1 } })).toHaveLength(1);
expect(timeout.mock.calls.map(([ms]) => ms)).toEqual([1500]);
expect(request().url.searchParams.get('lat')).toBe('54.1');
timeout.mockRestore();
fetchMock.mockImplementation(
(_url: URL, init: RequestInit) =>
new Promise((_resolve, reject) => {
init.signal?.addEventListener('abort', () => reject(new DOMException('aborted', 'AbortError')));
}),
);
for (let i = 0; i < 5; i += 1) {
const dropped = new AbortController();
const asked = photon.suggest('Rost', { signal: dropped.signal });
dropped.abort();
expect(await asked).toBeNull();
}
fetchMock.mockImplementation(async () => photonAnswer([gateFeature()]));
expect(await photon.suggest('Rostock')).toHaveLength(1);
});
});