From 0be51ab902258ab85f48884203af29f06f2bdac0 Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Mon, 31 Aug 2026 23:08:24 +0100 Subject: [PATCH] fix: reconnect matchmaking event stream --- Game/scripts/control_plane_client.gd | 23 +++++++++++++++++++++++ multiplayer-todo.md | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Game/scripts/control_plane_client.gd b/Game/scripts/control_plane_client.gd index 0b099c74..9b2b9fe8 100644 --- a/Game/scripts/control_plane_client.gd +++ b/Game/scripts/control_plane_client.gd @@ -28,6 +28,8 @@ var _operation := "" var _last_queue_create: Dictionary = {} var _websocket: WebSocketPeer var _websocket_status := "DISCONNECTED" +var _websocket_retry_seconds := 0.0 +var _websocket_backoff := 1.0 func _ready() -> void: @@ -50,6 +52,8 @@ func _process(_delta: float) -> void: _websocket.poll() var ready_state := _websocket.get_ready_state() if ready_state == WebSocketPeer.STATE_OPEN: + _websocket_retry_seconds = 0.0 + _websocket_backoff = 1.0 _set_websocket_status("CONNECTED") while _websocket.get_available_packet_count() > 0: _handle_websocket_packet(_websocket.get_packet()) @@ -57,6 +61,12 @@ func _process(_delta: float) -> void: _set_websocket_status("CONNECTING") elif ready_state == WebSocketPeer.STATE_CLOSED: _set_websocket_status("DISCONNECTED") + if not auth_expired and is_valid_access_token(access_token): + _websocket_retry_seconds -= _delta + if _websocket_retry_seconds <= 0.0: + _websocket_retry_seconds = _websocket_backoff + _websocket_backoff = minf(_websocket_backoff * 2.0, 30.0) + connect_event_stream() func configure(url: String, token: String) -> bool: @@ -67,6 +77,8 @@ func configure(url: String, token: String) -> bool: base_url = normalized access_token = normalized_token auth_expired = false + if _websocket != null: + connect_event_stream() return true @@ -79,6 +91,7 @@ func connect_event_stream() -> Error: if err != OK: _set_websocket_status("DISCONNECTED") return err + _websocket_retry_seconds = 0.0 _set_websocket_status("CONNECTING") return OK @@ -86,6 +99,8 @@ func connect_event_stream() -> Error: func disconnect_event_stream() -> void: if _websocket != null: _websocket.close() + _websocket_retry_seconds = 0.0 + _websocket_backoff = 1.0 _set_websocket_status("DISCONNECTED") @@ -244,6 +259,7 @@ func _on_request_completed(result: HTTPRequest.Result, response_code: int, _head if response_code == HTTPClient.RESPONSE_UNAUTHORIZED: access_token = "" auth_expired = true + disconnect_event_stream() state.fail("Session expired; sign in again") ranked_profile.set_error("Session expired; sign in again") session_expired.emit() @@ -272,6 +288,7 @@ func _on_request_completed(result: HTTPRequest.Result, response_code: int, _head access_token = returned_token auth_expired = false session_expires_at = String(payload.get("expires_at", "")) + connect_event_stream() session_changed.emit(player_id) elif operation == "queue_create": state.begin_queue(String(payload.get("ticket_id", "")), String(payload.get("playlist", ""))) @@ -334,6 +351,12 @@ func _set_websocket_status(status: String) -> void: return _websocket_status = status websocket_status_changed.emit(status) + if status == "CONNECTED": + if not state.ticket_id.is_empty() and state.phase not in [MatchmakingState.CANCELLED, MatchmakingState.EXPIRED, MatchmakingState.FAILED, MatchmakingState.LIVE]: + if not state.proposal_id.is_empty(): + recover_proposal(state.proposal_id) + else: + recover_queue(state.ticket_id) func _idempotency_key(prefix: String) -> String: diff --git a/multiplayer-todo.md b/multiplayer-todo.md index 82802edd..517bcfc9 100644 --- a/multiplayer-todo.md +++ b/multiplayer-todo.md @@ -1227,7 +1227,7 @@ the local/CI/community transport, not a silent production fallback. | # | Task | Acceptance | |---|---|---| | 8.39 `[D:8.3,8.14,8.17]` | **IN PROGRESS.** `MatchmakingState` now projects queue → proposal → allocation/process-ready/assignment-ready/connect/live plus terminal failure states; autoload `ControlPlaneClient` provides authenticated queue create/recovery/heartbeat/cancel and proposal response requests with idempotency/revision headers; `matchmaking.tscn`/`matchmaking.gd` expose the state and authoritative actions from the main menu; authenticated proposal recovery now reconciles missed proposal events and expires them at read time; the Go API now provides targeted authenticated revisioned event publication and the Godot client consumes state/proposal events | `test_matchmaking_state.gd`, `test_control_plane_client.gd`, `test_matchmaking_ui.gd`, `TestProposalRecoveryIsParticipantScopedAndExpiresAtReadBoundary` and `TestAuthenticatedWebSocketDeliversOnlyTargetedRevisionedEvents` reject stale/gapped/conflicting updates, validate endpoint/token/payload normalization, preserve idempotent duplicates, and guarantee visible phase/terminal copy; wiring durable allocation events into the outbox, wait/latency explanations and Godot runtime verification remain | -| 8.40 `[D:8.3,8.14]` | **IN PROGRESS.** Pure Go revisioned replica reducer rejects gaps for REST resync, makes duplicate/out-of-order events idempotent, and resumes from the authoritative snapshot revision; authenticated queue-ticket recovery now has an owner-checked REST read; Godot client projection persists non-secret ticket/proposal state, forces authoritative recovery after restart, and can replay a lost queue-create response with the original ticket/idempotency key; the Go API now exposes an authenticated `/v1/events` WebSocket with bounded per-player queues, strict upgrade validation and REST-resync-safe slow-client failure; Godot `ControlPlaneClient` can connect to the stream, consume validated events, and trigger REST recovery on projection gaps | `server/domain/sync.go`, `server/api/events.go`, `server/api/service.go`, `service_test.go`, `matchmaking_state.gd` and `control_plane_client.gd` cover gap, snapshot, replay, same-revision conflict, owner-only ticket recovery, expired-ticket terminal handling, malformed restart snapshots, API-level duplicate-create replay/conflict, authenticated handshake/key rejection and targeted event delivery; durable outbox fan-out, reconnect/resync orchestration and live Godot verification remain | +| 8.40 `[D:8.3,8.14]` | **IN PROGRESS.** Pure Go revisioned replica reducer rejects gaps for REST resync, makes duplicate/out-of-order events idempotent, and resumes from the authoritative snapshot revision; authenticated queue-ticket recovery now has an owner-checked REST read; Godot client projection persists non-secret ticket/proposal state, forces authoritative recovery after restart, and can replay a lost queue-create response with the original ticket/idempotency key; the Go API now exposes an authenticated `/v1/events` WebSocket with bounded per-player queues, strict upgrade validation and REST-resync-safe slow-client failure; Godot `ControlPlaneClient` can connect to the stream, consume validated events, automatically reconnect with bounded backoff, and trigger REST recovery on projection gaps and stream return | `server/domain/sync.go`, `server/api/events.go`, `server/api/service.go`, `service_test.go`, `matchmaking_state.gd` and `control_plane_client.gd` cover gap, snapshot, replay, same-revision conflict, owner-only ticket recovery, expired-ticket terminal handling, malformed restart snapshots, API-level duplicate-create replay/conflict, authenticated handshake/key rejection and targeted event delivery; durable outbox fan-out and live Godot verification remain | | 8.41 `[D:7.8,8.9,8.31,8.40]` | **IN PROGRESS.** Authenticated `GET /v1/assignments/{matchId}` now exposes only a validated, player-scoped assignment view; Godot `AssignmentState`/`ControlPlaneClient.fetch_assignment()` bind the response to the authenticated player, recheck expiry, preserve explicit transport/slot/join authorisation and do not connect before assignment-ready | `server/api/service.go`, `service_test.go`, `assignment_state.gd` and `test_assignment_state.gd` cover participant/identity/expiry/shape/transport boundaries and assignment recovery; signed manifest-to-player persistence, SDR relay-ticket installation, `hello` join-authorisation wiring, fencing integration and live Godot verification remain | | 8.42 `[D:8.22,8.23,8.24,8.40]` | **IN PROGRESS.** `RankedProfileState` and `ControlPlaneClient.fetch_ranked_profile()` expose the backend-authoritative rating/RD/volatility/games/tier/provisional/season view; matchmaking UI displays provisional/tier status without client-side rating math | `test_control_plane_client.gd` validates profile shape, numeric safety and provisional display; ranked profile fetch/display, committed revision after reconnect, abandon status and season countdown remain dependent on live auth/backend events and Godot runtime verification | | 8.43 `[D:8.39,8.40,8.41]` | **IN PROGRESS.** Matchmaking client now distinguishes expired queue recovery, session expiry, missing records and retryable control-plane outages; a 401 clears the in-memory token, emits `session_expired` and disables retry until a new session is configured; terminal messages remain visible and active searches are not falsely failed on transient errors | `MatchmakingState` and `ControlPlaneClient` tests cover explicit expiry and the existing terminal/retry-safe state paths; decline, version mismatch, regional outage retry UI, failed reconnect, duplicate-action recovery and live Godot verification remain |