feat(multiplayer): run leased allocator worker

This commit is contained in:
Josh Creek
2026-09-01 10:38:45 +01:00
parent 6f7d61eafb
commit 55c46f56ec
6 changed files with 255 additions and 6 deletions
+60
View File
@@ -0,0 +1,60 @@
package allocator
import (
"context"
"fmt"
"strconv"
"time"
"github.com/cosmic-clash/cosmic-clash/server/domain"
)
// MatchClaimSource is the durable allocator work queue. Implementations must
// lease a match before returning it and fence binding by allocation ID.
type MatchClaimSource interface {
ClaimAllocatingMatch(context.Context, time.Time) (domain.AllocationRequest, bool, error)
BindAllocatedMatch(context.Context, domain.Allocation) error
}
// Worker consumes one leased match at a time. Provider failures deliberately
// retain the lease: an HTTP/provider failure can be ambiguous after an external
// allocation, so releasing it could allocate two GameServers for one match.
type Worker struct {
Claims MatchClaimSource
Service Service
Now func() time.Time
}
// RunOnce returns whether it found a claimed match. It never exposes an
// endpoint itself; Service first records the provider allocation durably and
// BindAllocatedMatch then attaches that already-recorded allocation to the
// fenced match claim.
func (w Worker) RunOnce(ctx context.Context) (bool, error) {
if w.Claims == nil || w.Now == nil {
return false, errNotConfigured
}
request, found, err := w.Claims.ClaimAllocatingMatch(ctx, w.Now())
if err != nil || !found {
return found, err
}
result, err := w.Service.Allocate(ctx, request, AllocationLabels(request))
if err != nil {
return true, fmt.Errorf("allocate claimed match %s: %w", request.MatchID, err)
}
if err := w.Claims.BindAllocatedMatch(ctx, result.Allocation); err != nil {
return true, fmt.Errorf("bind allocated match %s: %w", request.MatchID, err)
}
return true, nil
}
// AllocationLabels are the compatibility selectors shared with the Fleet
// template. They are derived only from the durable match plan, never client
// input or mutable worker configuration.
func AllocationLabels(request domain.AllocationRequest) map[string]string {
return map[string]string{
"cosmic-clash.io/region": request.Region,
"cosmic-clash.io/build": request.Build,
"cosmic-clash.io/protocol": strconv.Itoa(request.Protocol),
"cosmic-clash.io/transport": request.Transport,
}
}
+69
View File
@@ -0,0 +1,69 @@
package allocator
import (
"context"
"errors"
"reflect"
"testing"
"time"
"github.com/cosmic-clash/cosmic-clash/server/agones"
"github.com/cosmic-clash/cosmic-clash/server/domain"
)
type matchClaimSpy struct {
request domain.AllocationRequest
found bool
err error
bound domain.Allocation
bindErr error
}
func (s *matchClaimSpy) ClaimAllocatingMatch(_ context.Context, _ time.Time) (domain.AllocationRequest, bool, error) {
return s.request, s.found, s.err
}
func (s *matchClaimSpy) BindAllocatedMatch(_ context.Context, allocation domain.Allocation) error {
s.bound = allocation
return s.bindErr
}
func TestWorkerClaimsAllocatesAndBindsDurably(t *testing.T) {
request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}
claims := &matchClaimSpy{request: request, found: true}
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: request.AllocationID, MatchID: request.MatchID, ServerID: "server-1", Region: request.Region, Build: request.Build, Protocol: request.Protocol, Transport: request.Transport, State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}}
durable := &durableSpy{}
worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1_000, 0) }}, Now: func() time.Time { return time.Unix(1_000, 0) }}
processed, err := worker.RunOnce(context.Background())
if err != nil || !processed || provider.calls != 1 || durable.calls != 1 || claims.bound.ServerID != "server-1" {
t.Fatalf("processed=%t err=%v provider=%d durable=%d bound=%+v", processed, err, provider.calls, durable.calls, claims.bound)
}
}
func TestWorkerRetainsClaimWhenProviderOutcomeIsAmbiguous(t *testing.T) {
request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}
claims := &matchClaimSpy{request: request, found: true}
provider := &providerSpy{err: errors.New("provider timeout")}
worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: &durableSpy{}, Now: func() time.Time { return time.Unix(1_000, 0) }}, Now: func() time.Time { return time.Unix(1_000, 0) }}
processed, err := worker.RunOnce(context.Background())
if err == nil || !processed || claims.bound != (domain.Allocation{}) {
t.Fatalf("processed=%t err=%v bound=%+v", processed, err, claims.bound)
}
}
func TestWorkerDoesNothingWhenNoDurableMatchIsAvailable(t *testing.T) {
claims := &matchClaimSpy{}
worker := Worker{Claims: claims, Now: func() time.Time { return time.Unix(1_000, 0) }}
processed, err := worker.RunOnce(context.Background())
if err != nil || processed {
t.Fatalf("processed=%t err=%v", processed, err)
}
}
func TestAllocationLabelsMirrorFleetCompatibilityTuple(t *testing.T) {
got := AllocationLabels(domain.AllocationRequest{Region: "NA", Build: "build-4", Protocol: 12, Transport: "steam_sdr"})
want := map[string]string{"cosmic-clash.io/region": "NA", "cosmic-clash.io/build": "build-4", "cosmic-clash.io/protocol": "12", "cosmic-clash.io/transport": "steam_sdr"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("labels=%v want=%v", got, want)
}
}