feat(gateway): unsigned gateway.heartbeat keeps Safari push streams alive
Tests · UI / test (push) Successful in 2m35s
Tests · Go / test (push) Successful in 1m56s
Tests · UI / test (pull_request) Has been cancelled
Tests · Integration / integration (pull_request) Successful in 1m42s
Tests · Go / test (pull_request) Successful in 2m0s
Tests · UI / test (push) Successful in 2m35s
Tests · Go / test (push) Successful in 1m56s
Tests · UI / test (pull_request) Has been cancelled
Tests · Integration / integration (pull_request) Successful in 1m42s
Tests · Go / test (pull_request) Successful in 2m0s
Browser fetch-streaming layers close response bodies they consider
idle after roughly 15-30 s without incoming bytes. Safari is the
most aggressive, but the symptom matters everywhere: a quiet
SubscribeEvents stream (lobby, between turns, mailbox empty) gets
torn down by the browser, the EventStream singleton reconnects with
backoff, and any push event that fires inside the reconnect window
is lost because `push.Hub` queues are not persisted across
subscription closes. The user-visible failure mode is the
intermittent "Fetch API cannot load … due to access control checks"
console error (a misleading WebKit symptom — CORS headers are
actually present) plus missed turn-ready / mail-received toasts.
Server-side fix: a silence-based heartbeat at the
`authenticatedPushStreamService` wrapper layer. After the signed
`gateway.server_time` bootstrap event, gateway wraps the bound
stream with `heartbeatingStream`. Every tail Send (fan-out, future
variants) resets the silence timer; when the timer elapses, a
goroutine emits `gateway.heartbeat` with only `EventType` set —
everything else stays at proto3 defaults, so the wire frame is
~45 bytes amortised. A `sendMu` serialises the heartbeat goroutine
with tail Sends because grpc.ServerStream.Send is not goroutine-safe.
The heartbeat is intentionally UNSIGNED: heartbeats carry no
payload, dispatch to no handler on the client, and an injected
heartbeat trivially causes no user-visible state change. TLS still
protects the wire and real events keep the signed envelope
unchanged. Documented in `docs/ARCHITECTURE.md` § 15 alongside the
per-scale bandwidth projection (100…100 000 clients × 15…60 s).
Config: new `GATEWAY_PUSH_HEARTBEAT_INTERVAL` (default `15s`,
`0s` disables). Telemetry: new
`gateway.push.heartbeats_sent{outcome}` counter so operators can
budget bandwidth and spot a sudden `outcome=error` bump as an
upstream-failing-before-flush signal.
Client (`ui/frontend/src/api/events.svelte.ts`): early `continue`
on `event.eventType === "gateway.heartbeat"` before `verifyEvent`,
`verifyPayloadHash`, or dispatch — empty signature would otherwise
trip SignatureError and reconnect. A leading heartbeat still flips
`connectionStatus` to `connected` and resets backoff, because
receiving one is proof the stream is healthy.
Tests:
- `push_heartbeat_test.go`: unit tests for the wrapper — zero
interval returns nil, heartbeat fires after silence, real Send
resets the timer, Stop / context-cancel halt the goroutine,
Send errors propagate.
- `server_test.go`: integration tests through the full gateway
pipeline — heartbeat fires after the configured silence window,
zero interval keeps the stream silent.
- `config_test.go`: default applied, env-override parsed,
negative value rejected.
- `events.test.ts`: heartbeat skipped before verification + not
dispatched to handlers; leading heartbeat still flips
`connectionStatus` to `connected`.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -125,6 +125,14 @@ const (
|
||||
// gRPC requests.
|
||||
authenticatedGRPCFreshnessWindowEnvVar = "GATEWAY_AUTHENTICATED_GRPC_FRESHNESS_WINDOW"
|
||||
|
||||
// pushHeartbeatIntervalEnvVar names the environment variable that
|
||||
// configures the silence-based heartbeat cadence for authenticated
|
||||
// push streams. The heartbeat keeps idle SubscribeEvents responses
|
||||
// alive across browser fetch-streaming idle timeouts (Safari is
|
||||
// notably aggressive) so push events do not disappear into the
|
||||
// reconnect window. A value of `0s` disables heartbeats entirely.
|
||||
pushHeartbeatIntervalEnvVar = "GATEWAY_PUSH_HEARTBEAT_INTERVAL"
|
||||
|
||||
// authenticatedGRPCIPRateLimitRequestsEnvVar names the environment
|
||||
// variable that configures the authenticated gRPC per-IP request budget per
|
||||
// window.
|
||||
@@ -321,6 +329,13 @@ const (
|
||||
defaultAuthenticatedGRPCDownstreamTimeout = 5 * time.Second
|
||||
defaultAuthenticatedGRPCFreshnessWindow = 5 * time.Minute
|
||||
|
||||
// defaultPushHeartbeatInterval is the silence window the push stream
|
||||
// keeps open before emitting `gateway.heartbeat`. 15s is comfortably
|
||||
// below the empirical Safari fetch-streaming idle threshold
|
||||
// (~15-30s) and well above any realistic per-event rate, so the
|
||||
// timer is almost always reset by a real event in active games.
|
||||
defaultPushHeartbeatInterval = 15 * time.Second
|
||||
|
||||
defaultAuthenticatedGRPCIPRateLimitRequests = 120
|
||||
defaultAuthenticatedGRPCIPRateLimitBurst = 40
|
||||
|
||||
@@ -549,6 +564,16 @@ type AuthenticatedGRPCConfig struct {
|
||||
// used for client request timestamps.
|
||||
FreshnessWindow time.Duration
|
||||
|
||||
// PushHeartbeatInterval is the silence window after which an open
|
||||
// authenticated SubscribeEvents stream sends an unsigned
|
||||
// `gateway.heartbeat` event. Every real Send resets the window, so
|
||||
// in busy streams the heartbeat fires rarely. A zero or negative
|
||||
// value disables the heartbeat — the stream then relies on
|
||||
// transport-level keepalives only, which Safari's fetch-streaming
|
||||
// layer ignores. See `docs/ARCHITECTURE.md` for the security
|
||||
// rationale of leaving the heartbeat unsigned.
|
||||
PushHeartbeatInterval time.Duration
|
||||
|
||||
// AntiAbuse configures the authenticated gRPC rate limits enforced after
|
||||
// the request passes the transport authenticity checks.
|
||||
AntiAbuse AuthenticatedGRPCAntiAbuseConfig
|
||||
@@ -719,6 +744,7 @@ func DefaultAuthenticatedGRPCConfig() AuthenticatedGRPCConfig {
|
||||
ConnectionTimeout: defaultAuthenticatedGRPCConnectionTimeout,
|
||||
DownstreamTimeout: defaultAuthenticatedGRPCDownstreamTimeout,
|
||||
FreshnessWindow: defaultAuthenticatedGRPCFreshnessWindow,
|
||||
PushHeartbeatInterval: defaultPushHeartbeatInterval,
|
||||
AntiAbuse: AuthenticatedGRPCAntiAbuseConfig{
|
||||
IP: AuthenticatedRateLimitConfig{
|
||||
Requests: defaultAuthenticatedGRPCIPRateLimitRequests,
|
||||
@@ -928,6 +954,12 @@ func LoadFromEnv() (Config, error) {
|
||||
}
|
||||
cfg.AuthenticatedGRPC.DownstreamTimeout = authenticatedGRPCDownstreamTimeout
|
||||
|
||||
pushHeartbeatInterval, err := loadDurationEnvWithDefault(pushHeartbeatIntervalEnvVar, cfg.AuthenticatedGRPC.PushHeartbeatInterval)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
cfg.AuthenticatedGRPC.PushHeartbeatInterval = pushHeartbeatInterval
|
||||
|
||||
authenticatedGRPCFreshnessWindow, err := loadDurationEnvWithDefault(authenticatedGRPCFreshnessWindowEnvVar, cfg.AuthenticatedGRPC.FreshnessWindow)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
@@ -1156,6 +1188,9 @@ func LoadFromEnv() (Config, error) {
|
||||
if cfg.AuthenticatedGRPC.FreshnessWindow <= 0 {
|
||||
return Config{}, fmt.Errorf("load gateway config: %s must be positive", authenticatedGRPCFreshnessWindowEnvVar)
|
||||
}
|
||||
if cfg.AuthenticatedGRPC.PushHeartbeatInterval < 0 {
|
||||
return Config{}, fmt.Errorf("load gateway config: %s must not be negative", pushHeartbeatIntervalEnvVar)
|
||||
}
|
||||
if err := validateRateLimitConfig(
|
||||
cfg.AuthenticatedGRPC.AntiAbuse.IP,
|
||||
authenticatedGRPCIPRateLimitRequestsEnvVar,
|
||||
|
||||
Reference in New Issue
Block a user