mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 08:23:45 +00:00
fix(multiplayer): recover recorded allocations
This commit is contained in:
@@ -20,6 +20,10 @@ func (s AllocatingMatchClaims) ClaimAllocatingMatch(ctx context.Context, now tim
|
||||
return item.Request, found, err
|
||||
}
|
||||
|
||||
func (s AllocatingMatchClaims) FindProviderAllocation(ctx context.Context, request domain.AllocationRequest) (domain.Allocation, bool, error) {
|
||||
return FindProviderAllocation(ctx, s.DB, request)
|
||||
}
|
||||
|
||||
func (s AllocatingMatchClaims) BindAllocatedMatch(ctx context.Context, allocation domain.Allocation) error {
|
||||
return BindAllocatedMatch(ctx, s.DB, allocation)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
@@ -47,6 +48,30 @@ const ReleaseAllocatedMatchClaimSQL = `UPDATE matches
|
||||
SET allocation_id = NULL, allocation_claimed_at = NULL
|
||||
WHERE match_id = $1 AND state = 'ALLOCATING' AND allocation_id = $2 AND server_id IS NULL`
|
||||
|
||||
// FindProviderAllocation verifies whether a recovered lease has already
|
||||
// crossed the durable provider boundary. A worker can then bind it without
|
||||
// issuing a second external allocation request after a crash.
|
||||
func FindProviderAllocation(ctx context.Context, db *sql.DB, request domain.AllocationRequest) (domain.Allocation, bool, error) {
|
||||
if db == nil || request.AllocationID == "" || request.MatchID == "" || request.Region == "" || request.Build == "" || request.Protocol <= 0 || (request.Transport != "enet" && request.Transport != "steam_sdr") {
|
||||
return domain.Allocation{}, false, fmt.Errorf("invalid provider allocation lookup")
|
||||
}
|
||||
var allocation domain.Allocation
|
||||
var digest []byte
|
||||
err := db.QueryRowContext(ctx, SelectAllocationSQL, request.AllocationID).Scan(&allocation.AllocationID, &allocation.MatchID, &allocation.ServerID, &allocation.Region, &allocation.Build, &allocation.Protocol, &allocation.Transport, &allocation.AllocatedAt, &digest)
|
||||
if err == sql.ErrNoRows {
|
||||
return domain.Allocation{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return domain.Allocation{}, false, err
|
||||
}
|
||||
want := allocationRequestDigest(request)
|
||||
if !bytes.Equal(digest, want[:]) || allocation.MatchID != request.MatchID || allocation.Region != request.Region || allocation.Build != request.Build || allocation.Protocol != request.Protocol || allocation.Transport != request.Transport {
|
||||
return domain.Allocation{}, false, domain.ErrConflict
|
||||
}
|
||||
allocation.State = domain.ServerAllocated
|
||||
return allocation, true, nil
|
||||
}
|
||||
|
||||
// ClaimAllocatingMatch returns one durable provider work item. The fixed
|
||||
// allocation ID is retained across a lease recovery, allowing every later
|
||||
// reconciliation step to reject a different server for the same match.
|
||||
|
||||
Reference in New Issue
Block a user