Skip to the content.

RFC 0012: Tunnel Latency Optimization

Summary

Reduce tunnel relay latency from ~500ms to sub-100ms by switching from long-polling to a WebSocket or SSE-based relay mechanism.

Problem

Every visitor request goes through: visitor → ingress → tunnel queues request → client polls (500ms interval) → client fetches localhost → client POSTs relay → tunnel resolves promise → response goes back. Best case 500ms+ per request. Blocked by RFC 0005 (streaming support in ingress).

Files to Modify

Implementation

Do NOT start this until RFC 0005 (streaming/websocket ingress) is complete.

  1. Replace the poll/relay HTTP endpoints with a WebSocket connection between tunnel client and server
  2. The client establishes a persistent WebSocket to the tunnel server on startup
  3. When a visitor request arrives at the tunnel server, it sends the request over the WebSocket to the client
  4. The client processes the request locally and sends the response back over the same WebSocket
  5. The tunnel server relays the response back to the visitor
  6. Add reconnection logic: if the WebSocket drops, the client reconnects with exponential backoff
  7. Add heartbeat: ping/pong every 30s to detect dead connections
  8. Keep the old poll/relay endpoints as a fallback for environments that don’t support WebSocket

Testing & Validation Requirements

Report Requirements