Judge fetched tiles apart from kept ones; share Esri's request slots

Review round 2: with part of a map's tiles on disk, the placeholder
check saw several pictures and let a provider's placeholder through,
drew it and kept it for 30 days. The tiles fetched now are judged on
their own: two or more that are all one picture matching no kept tile
are a placeholder, and the map fails whole (nothing drawn or kept, the
poster served for five minutes). A real uniform tile, such as open sea,
matches one kept before and passes.

From its notes: the request slots are per tile host, so the satellite
and both grey canvases share Esri's six; a weak If-None-Match matches;
the README says a poster is kept once its map or photo came.

Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
This commit is contained in:
2026-11-27 10:42:48 +01:00
parent e6701fcd27
commit 670ecc0162
3 changed files with 24 additions and 13 deletions
+22 -11
View File
@@ -20,6 +20,7 @@ import math
import os
import threading
import time
import urllib.parse
import urllib.request
from concurrent.futures import ThreadPoolExecutor
from enum import Enum
@@ -80,8 +81,14 @@ TILE_PROVIDERS = {
# Tiles are kept on disk under cache/tiles/<style>/<z>/<x>/<y> and used for this long: every poster,
# thumb and locator of a circuit draws on the same tiles (OpenStreetMap's policy asks for at least a week).
TILE_KEEP_SECONDS = 30 * 24 * 3600
# At most this many requests to one provider at once, over every render the service runs side by side.
TILE_SLOTS = {style: threading.BoundedSemaphore(provider["concurrency"]) for style, provider in TILE_PROVIDERS.items()}
# At most this many requests to one tile host at once, over every render the service runs side by side:
# the satellite and both grey canvases are one Esri host, and share its slots.
_TILE_HOST_SLOTS: dict[str, threading.BoundedSemaphore] = {}
for _provider in TILE_PROVIDERS.values():
_TILE_HOST_SLOTS.setdefault(urllib.parse.urlsplit(_provider["url"]).hostname,
threading.BoundedSemaphore(_provider["concurrency"]))
TILE_SLOTS = {style: _TILE_HOST_SLOTS[urllib.parse.urlsplit(provider["url"]).hostname]
for style, provider in TILE_PROVIDERS.items()}
# Part of every race poster's file name in the cache: raised when what a poster looks like changes,
# so posters rendered before (such as over CARTO's "API KEY REQUIRED" tiles) are rendered again.
@@ -268,8 +275,17 @@ class PostersService:
with ThreadPoolExecutor(max_workers=provider["concurrency"]) as pool:
bodies_in_order = list(pool.map(lambda t: tile_body(t[2], t[1]), wanted))
bodies = set()
fetched = 0
# The tiles fetched now are judged on their own, apart from those read from disk: two or more that
# are all one picture, matching no tile kept before (a real uniform tile, such as open sea, does),
# are a placeholder, and the map fails whole: nothing drawn, nothing kept.
on_disk = {hashlib.sha256(body).digest() for body, from_network in bodies_in_order if body and not from_network}
from_net = [hashlib.sha256(body).digest() for body, from_network in bodies_in_order if body and from_network]
if len(from_net) > 1 and len(set(from_net)) == 1 and from_net[0] not in on_disk:
logger.error(f"Every {style.value} tile fetched at z={zoom} is the same picture: the provider answers a "
f"placeholder ({urllib.parse.urlsplit(provider['url']).hostname}), no map drawn")
return None
drawn = 0
new_tiles = []
for (tx, ty, x), (body, from_network) in zip(wanted, bodies_in_order):
if body is None:
@@ -280,15 +296,10 @@ class PostersService:
logger.warning(f"Tile unreadable {style.value} z={zoom} x={x} y={ty}: {e}")
continue
canvas.paste(tile, ((tx - tx_lo) * 256, (ty - ty_lo) * 256))
bodies.add(hashlib.sha256(body).digest())
fetched += 1
drawn += 1
if from_network:
new_tiles.append((x, ty, body))
if not fetched:
return None
if fetched > 1 and len(bodies) == 1:
logger.error(f"Every {style.value} tile at z={zoom} is the same picture: the provider answers a "
f"placeholder ({provider['url'].split('/')[2]}), no map drawn")
if not drawn:
return None
# Kept only now: a placeholder never reaches the disk
for x, y, body in new_tiles: