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
|
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
|
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
|
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.
|
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"})
|
return FastAPIResponse(content=png, media_type="image/png", headers={"Cache-Control": "public, max-age=300"})
|
||||||
etag = '"' + hashlib.sha256(png).hexdigest()[:32] + '"'
|
etag = '"' + hashlib.sha256(png).hexdigest()[:32] + '"'
|
||||||
headers = {"Cache-Control": "public, max-age=86400", "ETag": etag}
|
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 Response(status_code=304, headers=headers)
|
||||||
return FastAPIResponse(content=png, media_type="image/png", headers=headers)
|
return FastAPIResponse(content=png, media_type="image/png", headers=headers)
|
||||||
|
|
||||||
|
|||||||
+22
-11
@@ -20,6 +20,7 @@ import math
|
|||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
import urllib.parse
|
||||||
import urllib.request
|
import urllib.request
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from enum import Enum
|
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,
|
# 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).
|
# 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
|
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.
|
# At most this many requests to one tile host 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()}
|
# 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,
|
# 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.
|
# 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:
|
with ThreadPoolExecutor(max_workers=provider["concurrency"]) as pool:
|
||||||
bodies_in_order = list(pool.map(lambda t: tile_body(t[2], t[1]), wanted))
|
bodies_in_order = list(pool.map(lambda t: tile_body(t[2], t[1]), wanted))
|
||||||
|
|
||||||
bodies = set()
|
# The tiles fetched now are judged on their own, apart from those read from disk: two or more that
|
||||||
fetched = 0
|
# 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 = []
|
new_tiles = []
|
||||||
for (tx, ty, x), (body, from_network) in zip(wanted, bodies_in_order):
|
for (tx, ty, x), (body, from_network) in zip(wanted, bodies_in_order):
|
||||||
if body is None:
|
if body is None:
|
||||||
@@ -280,15 +296,10 @@ class PostersService:
|
|||||||
logger.warning(f"Tile unreadable {style.value} z={zoom} x={x} y={ty}: {e}")
|
logger.warning(f"Tile unreadable {style.value} z={zoom} x={x} y={ty}: {e}")
|
||||||
continue
|
continue
|
||||||
canvas.paste(tile, ((tx - tx_lo) * 256, (ty - ty_lo) * 256))
|
canvas.paste(tile, ((tx - tx_lo) * 256, (ty - ty_lo) * 256))
|
||||||
bodies.add(hashlib.sha256(body).digest())
|
drawn += 1
|
||||||
fetched += 1
|
|
||||||
if from_network:
|
if from_network:
|
||||||
new_tiles.append((x, ty, body))
|
new_tiles.append((x, ty, body))
|
||||||
if not fetched:
|
if not drawn:
|
||||||
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")
|
|
||||||
return None
|
return None
|
||||||
# Kept only now: a placeholder never reaches the disk
|
# Kept only now: a placeholder never reaches the disk
|
||||||
for x, y, body in new_tiles:
|
for x, y, body in new_tiles:
|
||||||
|
|||||||
Reference in New Issue
Block a user