feat(timeout): prevent Cloudflare 524 timeouts on long generations via SSE heartbeats and chunked keep-alives
This commit is contained in:
parent
ba57e3e394
commit
0e83233715
7 changed files with 385 additions and 25 deletions
17
README.md
17
README.md
|
|
@ -16,6 +16,7 @@ An OpenAI v1 compatible API endpoint that rotates Gemini API keys to avoid rate
|
|||
- 🧪 **Live Playground & Key Verifier** - Test individual provider keys and inspect step-by-step trace metadata directly from the Admin console
|
||||
- 🐳 **Docker support** - Containerized deployment with Docker and Docker Compose
|
||||
- 🌐 **Cloudflare Tunnel integration** - Securely expose your API via Cloudflare tunnel
|
||||
- ⏱️ **Cloudflare 524 Anti-Timeout Engine** - Prevents Cloudflare Free/Pro/Biz 100s proxy timeouts on long generations via immediate SSE header flushing, periodic `: keep-alive` comment heartbeats, and RFC 8259 chunked whitespace keep-alives for non-streaming requests
|
||||
|
||||
## Setup
|
||||
|
||||
|
|
@ -126,6 +127,22 @@ With Cloudflare tunnel running, your API will be accessible at:
|
|||
|
||||
Use the same authentication method with your configured `API_PASSWORD`.
|
||||
|
||||
### Cloudflare 524 Timeout Prevention (Free Plan Compatible)
|
||||
|
||||
Cloudflare Free, Pro, and Business plans enforce a non-configurable **100-second idle proxy timeout** (Error 524). Long LLM generations (thinking models, large prompts, or slow outputs) can exceed this limit. GeminiRevolver solves this at the application level:
|
||||
|
||||
1. **Streaming Requests (`stream: true`)**:
|
||||
- HTTP response headers and `X-Accel-Buffering: no` are flushed immediately upon initiation.
|
||||
- Periodic SSE comment pings (`: keep-alive\n\n`) are transmitted every 15 seconds. These reset Cloudflare's 100-second timer continuously while being silently ignored by SSE client parsers per the W3C spec.
|
||||
2. **Non-Streaming Requests (`stream: false`)**:
|
||||
- If generation takes longer than 20 seconds, chunked transfer encoding (`Transfer-Encoding: chunked`) is activated, and periodic whitespace keep-alives (` `) are emitted.
|
||||
- Standard JSON parsers (RFC 8259) automatically ignore leading whitespace when parsing the final JSON payload.
|
||||
3. **Configurable Settings (`.env`)**:
|
||||
- `KEEPALIVE_INTERVAL_MS` (default: `15000`): Interval between SSE comment heartbeats.
|
||||
- `ENABLE_NON_STREAMING_KEEPALIVE` (default: `true`): Enables chunked whitespace keep-alives for non-streaming calls.
|
||||
- `NON_STREAMING_KEEPALIVE_DELAY_MS` (default: `20000`): Wait time before activating non-streaming keep-alives.
|
||||
- `CANDIDATE_TIMEOUT_MS` (default: `60000`): Timeout per individual key/provider before triggering automatic failover.
|
||||
|
||||
## Admin Console & Model Revolver
|
||||
|
||||
Navigate to `http://localhost:3000/admin` in your web browser.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue