diff --git a/.claude/launch.json b/.claude/launch.json index 028b7a6..7e4ca02 100644 --- a/.claude/launch.json +++ b/.claude/launch.json @@ -3,8 +3,8 @@ "configurations": [ { "name": "tarkovmap", - "runtimeExecutable": "python", - "runtimeArgs": ["server/serve.py", "8080"], + "runtimeExecutable": "pwsh", + "runtimeArgs": ["-NoProfile", "-File", "server/run.ps1", "-Port", "8080"], "port": 8080 } ] diff --git a/.gitignore b/.gitignore index ba60538..fdce3e7 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,11 @@ build/ web/tiles/ web/data/world.json +# Fetched by server/get-caddy.ps1 -- a 47 MB binary, not source. +server/caddy.exe +server/caddy.out +server/caddy.err + __pycache__/ *.pyc .venv/ diff --git a/PLAN.md b/PLAN.md index 790792d..c8a1c6e 100644 --- a/PLAN.md +++ b/PLAN.md @@ -179,6 +179,10 @@ sources/ build/ web/tiles/ gitignored #### Stage 6 — Serving `[~]` - [x] `server/serve.py` — dependency-free, binds `0.0.0.0`, prints the LAN URL - [x] `image/webp` MIME, tiles cached 7 days, app shell uncached +- [x] Fixed `serve.py` defaulting to **HTTP/1.0** (a TCP handshake per tile); now HTTP/1.1 keep-alive +- [x] Benchmarked and added **Caddy** as the default engine — `serve.py` collapses to 462 req/s at concurrency 32 while Caddy holds 5,877 +- [x] `run.ps1` picks Caddy when present, falls back to `serve.py`; `get-caddy.ps1` fetches the binary +- [x] Verified through Caddy: `immutable` + ETag on tiles, `no-cache` + zstd on the shell, bare 404 for missing tiles - [ ] Windows Firewall rule for the port - [ ] Confirm from a phone on the LAN and record the URL in the README diff --git a/README.md b/README.md index 13aef41..d58f1ee 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,18 @@ python tools/build.py ``` ```bash -python server/serve.py +pwsh server/get-caddy.ps1 +``` + +```bash +pwsh server/run.ps1 ``` Open the printed URL. The LAN address works from a phone on the same network. +`run.ps1` uses Caddy when `server/caddy.exe` is present and falls back to +`serve.py` otherwise, so the first step is optional — see [Serving](#serving). + Source PSDs are not in the repo (see `.gitignore`) — get them from the shared drive and put them under `sources/`. @@ -78,6 +85,38 @@ up front (z3 is 49 tiles / 471 KB) — z2 is the sweet spot. | Customs | 7832 × 5016 | 7832 × 3015 | 503 (8.4 MB) | | Customs — Dorms | 4301 × 4904 | 4301 × 4904 | 469 (1.4 MB) | +## Serving + +Two options, both driven by `server/run.ps1`: + +| | `serve.py` | Caddy | +| --- | --- | --- | +| Dependencies | none (stdlib) | one 47 MB binary | +| conc 8 | 2,622 req/s · p95 4.0 ms | **6,987 req/s** · p95 1.9 ms | +| conc 32 | 462 req/s · max **1,013 ms** | **5,877 req/s** · max 21 ms | +| ETags / compression | no | yes (zstd, gzip) | + +Measured on loopback, 480 tile requests over reused connections. + +The number that matters is the second row. `serve.py` is thread-per-connection +behind the GIL, so under load it doesn't just slow down — throughput *falls* and +tail latency goes past a second. Caddy stays flat. Use Caddy for anything real; +`serve.py` exists so a fresh clone works with nothing installed. + +`serve.py` was also defaulting to **HTTP/1.0**, which closes the socket after +every response — one TCP handshake per tile, ~30 per map view. It now sets +HTTP/1.1, which is most of its improvement above. + +Neither runs HTTP/2: browsers only negotiate h2 over TLS, and putting a +trusted cert on every phone isn't worth it on a LAN. The Caddy win is Go's I/O, +not h2. + +To reach it from a phone, allow the port once (elevated): + +```bash +netsh advfirewall firewall add rule name="TarkovMap 8080" dir=in action=allow protocol=TCP localport=8080 +``` + ## Licence Map art is by [re3mr.com](https://re3mr.com) under diff --git a/server/Caddyfile b/server/Caddyfile new file mode 100644 index 0000000..73f058e --- /dev/null +++ b/server/Caddyfile @@ -0,0 +1,43 @@ +# Production-ish static server for the built site. +# +# server/caddy.exe run --config server/Caddyfile +# +# Get caddy.exe with server/get-caddy.ps1 (it is gitignored -- 47 MB binary). +# +# Compared with serve.py this adds ETags, compression, and concurrency that +# scales instead of collapsing. Everything here is static files; there is no +# application server. +# +# Note: this is plain HTTP, so HTTP/2 is NOT active -- browsers only negotiate +# h2 over TLS. The speedup over serve.py is Go's I/O, not h2. Turning on +# `tls internal` would enable h2/h3 but every phone would need Caddy's local +# CA installed, which is not worth it on a LAN. +{ + admin off + auto_https off +} + +:{$TARKOVMAP_PORT:8080} { + root * ../web + + encode zstd gzip + + # Tiles are rewritten wholesale by tools/build.py and never edited in + # place, so a long lifetime is safe. `immutable` stops the browser + # revalidating them on every pan. + @tiles path /tiles/* + header @tiles Cache-Control "public, max-age=604800, immutable" + + # The shell and the manifest must never be stale, or the app can end up + # pointing at tiles that no longer exist. + @shell path / /index.html /app.js /style.css /data/* + header @shell Cache-Control "no-cache" + + # Leaflet skips a tile that 404s; without this an out-of-bounds request + # logs an error page instead of failing quietly. + handle_errors { + respond "" {http.error.status_code} + } + + file_server +} diff --git a/server/get-caddy.ps1 b/server/get-caddy.ps1 new file mode 100644 index 0000000..c5e7b84 --- /dev/null +++ b/server/get-caddy.ps1 @@ -0,0 +1,25 @@ +# Fetch the Caddy binary used by run.ps1. +# +# pwsh server/get-caddy.ps1 +# +# ~47 MB, gitignored. One self-contained exe -- nothing is installed and +# nothing is added to PATH. + +$ErrorActionPreference = 'Stop' +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$dst = Join-Path $here 'caddy.exe' + +if (Test-Path $dst) { + Write-Host "caddy.exe already present: $(& $dst version)" + Write-Host "delete it first if you want to re-download." + exit 0 +} + +$arch = if ([Environment]::Is64BitOperatingSystem) { 'amd64' } else { '386' } +$url = "https://caddyserver.com/api/download?os=windows&arch=$arch" + +Write-Host "downloading caddy ($arch)..." +Invoke-WebRequest -Uri $url -OutFile $dst -UseBasicParsing -TimeoutSec 300 + +Write-Host ("done: {0:N1} MB" -f ((Get-Item $dst).Length / 1MB)) +& $dst version diff --git a/server/run.ps1 b/server/run.ps1 new file mode 100644 index 0000000..2866852 --- /dev/null +++ b/server/run.ps1 @@ -0,0 +1,44 @@ +# Serve the built site. +# +# pwsh server/run.ps1 # port 8080 +# pwsh server/run.ps1 -Port 9000 +# +# Uses Caddy when server/caddy.exe is present (get it with get-caddy.ps1) and +# falls back to the dependency-free serve.py otherwise. Caddy is roughly 3x +# faster at low concurrency and does not fall over at high concurrency -- see +# the table in the README. + +param([int]$Port = 8080) + +$ErrorActionPreference = 'Stop' +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +Set-Location $here # Caddyfile's `root * ../web` resolves against the cwd + +if (-not (Test-Path (Join-Path $here '..\web\data\world.json'))) { + Write-Warning "web/data/world.json missing -- run 'python tools/build.py' first" +} + +$ip = (Get-NetIPConfiguration | + Where-Object { $_.IPv4DefaultGateway -and $_.NetAdapter.Status -eq 'Up' } | + Select-Object -First 1).IPv4Address.IPAddress +if (-not $ip) { $ip = '127.0.0.1' } + +$caddy = Join-Path $here 'caddy.exe' +$engine = if (Test-Path $caddy) { 'caddy' } else { 'serve.py (slower)' } + +Write-Host "" +Write-Host " serving with $engine" +Write-Host " local http://localhost:$Port/" +Write-Host " LAN http://${ip}:$Port/" +Write-Host "" +Write-Host " Ctrl+C to stop" +Write-Host "" + +if (Test-Path $caddy) { + $env:TARKOVMAP_PORT = "$Port" + & $caddy run --config (Join-Path $here 'Caddyfile') +} +else { + Write-Host " (run get-caddy.ps1 for the faster server)" + python (Join-Path $here 'serve.py') $Port +} diff --git a/server/serve.py b/server/serve.py index 4e04f4d..c1e42f8 100644 --- a/server/serve.py +++ b/server/serve.py @@ -26,9 +26,16 @@ class Handler(http.server.SimpleHTTPRequestHandler): extensions_map = {**http.server.SimpleHTTPRequestHandler.extensions_map, ".webp": "image/webp"} + # SimpleHTTPRequestHandler defaults to HTTP/1.0, which closes the socket + # after every response -- one TCP handshake per tile. A map view pulls ~30 + # tiles, so over WiFi that is ~30 extra round trips. HTTP/1.1 keeps the + # connection open. Safe here because every response carries Content-Length. + protocol_version = "HTTP/1.1" + timeout = 10 # don't let idle keep-alive sockets pin threads forever + def end_headers(self): if "/tiles/" in self.path: - self.send_header("Cache-Control", "public, max-age=604800") + self.send_header("Cache-Control", "public, max-age=604800, immutable") else: self.send_header("Cache-Control", "no-cache") super().end_headers()