Skip to content

perf: cache API keys, widen upstream pools, drain before shutdown - #662

Open
guyu233 wants to merge 1 commit into
caigee-cmd:mainfrom
guyu233:perf/api-key-cache-pool-drain
Open

guyu233 wants to merge 1 commit into
caigee-cmd:mainfrom
guyu233:perf/api-key-cache-pool-drain

Conversation

@guyu233

@guyu233 guyu233 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Why

Three hot paths in the gateway were paying a full SQLite write or a fresh TLS handshake per request, and a shutdown could stall behind long-lived streams with no drain window.

What changed

  • API-key lookups cached 30 s (internal/auth/cache.go): configurable via CLI2API_AUTH_CACHE_MS (0 disables). Console key edits/deletions invalidate immediately, and last_used_at is written at most once per minute instead of on every request.
  • Wider upstream pools (internal/netx/transport.go): 100 idle connections per host, five-minute bound on the response-head wait, so bursts reuse TLS connections and a silent upstream fails over instead of pinning a request forever.
  • Draining shutdown (internal/app, internal/server): /ready returns 503 and new work is rejected while active streams finish for up to 30 s; /health exposes the draining state; compose waits 40 s before SIGKILL.
  • Compose log cap: 10 MB × 3 files so a chatty gateway cannot fill the host disk.

All knobs default to the values above, so no behavior change unless the env vars are set.

Tests

  • go build ./...
  • go test ./internal/server/ ./internal/executor/ ./internal/app/ ./internal/gateway/ ./internal/runtime/ — all pass
  • New: internal/auth/cache_test.go (TTL, invalidation, throttled last_used_at), internal/netx/transport_test.go, internal/server/health_test.go (draining state).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant