Skip to content

Latest commit

 

History

History

demo

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Browser demo

A self-contained FastAPI application for showing the lease in a browser. It uses a synthetic 20-second SSE stream, so it needs Redis but no LLM keys. The demo is shipped in the repository source tree and is not part of the published wheel; run it from a checkout or from the included Docker image.

Both panels share one signed HttpOnly session cookie. The first stream acquires a Redis lease; the second stream for the same session receives 429 from the per-session lease until the first one finishes or the browser disconnects. The per-IP guard is set to 2 so the two-tab demo can reach the session limit; a third session from the same network is rejected by the IP guard.

Run locally

docker compose up --build demo

Open http://localhost:8080. The compose service starts Redis and the demo together, with secure cookies disabled for local HTTP.

To run it without Docker:

uv sync --locked --extra fastapi --group demo
docker run --rm -p 6379:6379 redis:7-alpine
DEMO_COOKIE_SECURE=0 uv run uvicorn demo.app:app --reload

Configuration

Variable Default Purpose
REDIS_URL redis://localhost:6379/0 Redis connection string.
DEMO_LEASE_SECONDS 25 Redis lease TTL while the stream is open.
DEMO_STREAM_SECONDS 20 Synthetic stream duration.
DEMO_TOKEN_INTERVAL 0.5 Delay between synthetic tokens.
DEMO_MAX_PER_USER 1 Concurrent streams per session.
DEMO_MAX_GLOBAL 20 Concurrent streams across the process.
DEMO_MAX_PER_IP 2 Concurrent streams per client IP.
DEMO_RATE_LIMIT_PER_MINUTE 30 Stream attempts per IP per window.
DEMO_RATE_LIMIT_WINDOW_SECONDS 60 Rate-limit window.
DEMO_COOKIE_SECURE 0 Set to 1 if you run the demo behind HTTPS.
DEMO_KEY_PREFIX stream_lease_demo Redis key prefix.
DEMO_SESSION_SECRET random per process HMAC secret for sessions; set a stable value to keep sessions across restarts.

Endpoints

  • GET / — browser demo.
  • GET /demo/stream — protected synthetic SSE stream.
  • GET /demo/stats — active global count and configured limits.
  • GET /healthz — Redis-backed health check; returns 503 when Redis is unavailable.

Test

uv run pytest tests/test_demo.py -q -o addopts=''

The demo is a local reference app, not a production template: it has no user authentication, trusts the session cookie only as a stable demo identity, and keeps its rate limiter and stats cache in process memory.