refactor(game): lock-free storage, remove /command, flatten engine wrapper
Three-stage refactor of the game-engine plumbing (game logic untouched): Stage 1 — lock-free persistence + admin serialisation. Remove the file lock from repo/fs (the .lock file, the Read/Write-vs-*Safe duality and the dead ReadSafe polling) and replace the two-step rename with a single atomic rename so concurrent reads are torn-free without a lock. Serialise the state-mutating admin writers (init/turn/banish) with one shared router LimitMiddleware, rewritten to block on the request context instead of a racy shared 100ms timer. Stage 2 — remove the obsolete immediate-command path end to end. Players submit through PUT /api/v1/order; the legacy PUT /api/v1/command path is deleted across game (route, handler, 24 command factories, Ctrl), backend (Commands handler/route, engineclient.ExecuteCommands), gateway (dispatch + executeUserGamesCommand + routing entry), the FlatBuffers/model contract (UserGamesCommand[Response]) and transcoder, plus every affected OpenAPI/README/FUNCTIONAL/ARCHITECTURE doc. The integration proxy test is converted to the order path. Stage 3 — flatten the REST->engine wrapper. Replace the executor adapter, the controller package functions and RepoController with one concrete controller.Service; drop the single-implementation Repo and Storage interfaces (repo.Repo / fs.FS are now concrete). Handlers depend on a thin handler.Engine seam and own the domain->REST projection; storage is resolved once at startup instead of per request. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -10,12 +10,11 @@ import (
|
||||
"galaxy/integration/testenv"
|
||||
)
|
||||
|
||||
// TestEngineCommandProxy spins up a running game (10 enrolled
|
||||
// pilots so engine init succeeds) and verifies that backend's
|
||||
// user-side `/api/v1/user/games/{id}/commands` proxy reaches the
|
||||
// engine and returns its passthrough body without an internal-error
|
||||
// response.
|
||||
func TestEngineCommandProxy(t *testing.T) {
|
||||
// TestEngineOrderProxy spins up a running game (10 enrolled pilots so
|
||||
// engine init succeeds) and verifies that backend's user-side
|
||||
// `/api/v1/user/games/{id}/orders` proxy reaches the engine and returns
|
||||
// its passthrough body without an internal-error response.
|
||||
func TestEngineOrderProxy(t *testing.T) {
|
||||
plat := testenv.Bootstrap(t, testenv.BootstrapOptions{})
|
||||
testenv.EnsureGameImage(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
@@ -28,7 +27,7 @@ func TestEngineCommandProxy(t *testing.T) {
|
||||
t.Fatalf("seed engine_version: err=%v resp=%v", err, resp)
|
||||
}
|
||||
|
||||
owner := testenv.RegisterSession(t, plat, "owner+cmd@example.com")
|
||||
owner := testenv.RegisterSession(t, plat, "owner+order@example.com")
|
||||
testenv.PromoteToPaid(t, ctx, admin, plat, owner)
|
||||
ownerID, err := owner.LookupUserID(ctx, plat)
|
||||
if err != nil {
|
||||
@@ -37,7 +36,7 @@ func TestEngineCommandProxy(t *testing.T) {
|
||||
ownerHTTP := testenv.NewBackendUserClient(plat.Backend.HTTPURL, ownerID)
|
||||
|
||||
gameBody := map[string]any{
|
||||
"game_name": "Engine Command Proxy",
|
||||
"game_name": "Engine Order Proxy",
|
||||
"visibility": "private",
|
||||
"min_players": 10,
|
||||
"max_players": 10,
|
||||
@@ -59,7 +58,7 @@ func TestEngineCommandProxy(t *testing.T) {
|
||||
if _, resp, err := ownerHTTP.Do(ctx, http.MethodPost, "/api/v1/user/lobby/games/"+game.GameID+"/open-enrollment", nil); err != nil || resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("open enrollment: %v %d", err, resp.StatusCode)
|
||||
}
|
||||
pilots := testenv.EnrollPilots(t, plat, ownerHTTP, game.GameID, 10, "cmd")
|
||||
pilots := testenv.EnrollPilots(t, plat, ownerHTTP, game.GameID, 10, "order")
|
||||
|
||||
if _, resp, err := admin.Do(ctx, http.MethodPost, "/api/v1/admin/games/"+game.GameID+"/force-start", nil); err != nil || resp.StatusCode/100 != 2 {
|
||||
t.Fatalf("force-start: %v %d", err, resp.StatusCode)
|
||||
@@ -81,17 +80,17 @@ func TestEngineCommandProxy(t *testing.T) {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
|
||||
// Pilot 1 sends a command. Backend forwards to the engine; the
|
||||
// pass-through body comes back unchanged. We accept any status
|
||||
// the engine produces (200, 4xx) — what matters is that backend
|
||||
// did not surface an internal error of its own.
|
||||
cmdBody := map[string]any{"actions": []map[string]any{}}
|
||||
raw, resp, err = pilots[0].HTTP.Do(ctx, http.MethodPost, "/api/v1/user/games/"+game.GameID+"/commands", cmdBody)
|
||||
// Pilot 1 submits an (empty) order. Backend forwards to the engine;
|
||||
// the pass-through body comes back unchanged. We accept any status
|
||||
// the engine produces (200, 4xx) — what matters is that backend did
|
||||
// not surface an internal error of its own.
|
||||
orderBody := map[string]any{"cmd": []map[string]any{}}
|
||||
raw, resp, err = pilots[0].HTTP.Do(ctx, http.MethodPost, "/api/v1/user/games/"+game.GameID+"/orders", orderBody)
|
||||
if err != nil {
|
||||
t.Fatalf("commands proxy: %v", err)
|
||||
t.Fatalf("orders proxy: %v", err)
|
||||
}
|
||||
if resp.StatusCode == http.StatusInternalServerError || resp.StatusCode == http.StatusBadGateway {
|
||||
t.Fatalf("commands proxy: backend internal-error %d body=%s", resp.StatusCode, string(raw))
|
||||
t.Fatalf("orders proxy: backend internal-error %d body=%s", resp.StatusCode, string(raw))
|
||||
}
|
||||
|
||||
// Cleanup: stop the container so the test does not leak it.
|
||||
Reference in New Issue
Block a user