feat(multiplayer): refresh allocator ready servers

This commit is contained in:
Josh Creek
2026-09-01 10:44:43 +01:00
parent 013eb0778b
commit 9dc1cc2d6f
8 changed files with 111 additions and 5 deletions
+2 -1
View File
@@ -16,7 +16,8 @@ const RegisterReadyServerSQL = `INSERT INTO game_servers
VALUES ($1, $2, $3, $4, $5, 'READY', $6)
ON CONFLICT (server_id) DO UPDATE SET region = EXCLUDED.region,
build = EXCLUDED.build, protocol_version = EXCLUDED.protocol_version,
transport = EXCLUDED.transport, state = 'READY', updated_at = EXCLUDED.updated_at`
transport = EXCLUDED.transport, updated_at = EXCLUDED.updated_at
WHERE game_servers.state = 'READY'`
const ClaimReadyServerSQL = `UPDATE game_servers SET state = 'ALLOCATED', updated_at = $5
WHERE server_id = (
+1 -1
View File
@@ -9,7 +9,7 @@ import (
func TestAllocatorSQLClaimsAndAuditsCompatibleReadyServers(t *testing.T) {
for query, fragments := range map[string][]string{
RegisterReadyServerSQL: {"game_servers", "ON CONFLICT", "state = 'READY'"},
RegisterReadyServerSQL: {"game_servers", "ON CONFLICT", "WHERE game_servers.state = 'READY'"},
ClaimReadyServerSQL: {"state = 'READY'", "region = $1", "protocol_version = $3", "FOR UPDATE SKIP LOCKED", "ORDER BY server_id"},
InsertAllocationSQL: {"allocations", "request_digest", "state", "ALLOCATED"},
ProviderServerClaimSQL: {"state = 'READY'", "region = $2", "protocol_version = $4", "RETURNING"},
@@ -71,6 +71,13 @@ func TestPostgreSQLAllocatorClaimReplayAndCapacityFence(t *testing.T) {
if allocation.ServerID != "allocator-server-a" || allocation.State != domain.ServerAllocated {
t.Fatalf("allocation=%+v", allocation)
}
if err := RegisterReadyServer(ctx, db, servers[1], now.Add(time.Second)); err != nil {
t.Fatalf("stale Ready projection: %v", err)
}
var lifecycle string
if err := db.QueryRowContext(ctx, `SELECT state FROM game_servers WHERE server_id = 'allocator-server-a'`).Scan(&lifecycle); err != nil || lifecycle != "ALLOCATED" {
t.Fatalf("stale Ready projection reopened allocation state=%q err=%v", lifecycle, err)
}
replay, err := ClaimAllocation(ctx, db, request, now.Add(time.Second))
if err != nil || replay.ServerID != allocation.ServerID || !replay.AllocatedAt.Equal(allocation.AllocatedAt) {
t.Fatalf("replay=%+v err=%v", replay, err)