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:
@@ -133,7 +133,7 @@ rate limit and kept in `cache/jolpica/`; a throttled or failed fetch is said in
|
||||
|
||||
Everything the service fetches or renders is kept in `cache/` (the module's cache volume): race schedules from
|
||||
Jolpica in `cache/jolpica/` (a season that is over for good, the running one for six hours), map tiles in
|
||||
`cache/tiles/` (30 days), and every poster and thumbnail in `cache/posters/` once it rendered whole. A poster is
|
||||
`cache/tiles/` (30 days), and every poster and thumbnail in `cache/posters/` once its map or photo came. A poster is
|
||||
sent with an `ETag` and a day's `Cache-Control`; one whose map or photo failed is not kept, and is sent with five
|
||||
minutes', so it is rendered again once its source answers.
|
||||
|
||||
|
||||
+1
-1
@@ -108,7 +108,7 @@ def poster_response(request: Request, png: bytes, kept: bool) -> Response:
|
||||
return FastAPIResponse(content=png, media_type="image/png", headers={"Cache-Control": "public, max-age=300"})
|
||||
etag = '"' + hashlib.sha256(png).hexdigest()[:32] + '"'
|
||||
headers = {"Cache-Control": "public, max-age=86400", "ETag": etag}
|
||||
if etag in [tag.strip() for tag in request.headers.get("if-none-match", "").split(",")]:
|
||||
if etag in [tag.strip().removeprefix("W/") for tag in request.headers.get("if-none-match", "").split(",")]:
|
||||
return Response(status_code=304, headers=headers)
|
||||
return FastAPIResponse(content=png, media_type="image/png", headers=headers)
|
||||
|
||||
|
||||
+22
-11
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user