mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-13 12:32:14 +00:00
feat: reconcile Agones allocations durably
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
// Package allocator coordinates provider allocation with durable control-plane
|
||||
// state. It does not expose an endpoint until both boundaries succeed.
|
||||
package allocator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/cosmic-clash/cosmic-clash/server/agones"
|
||||
"github.com/cosmic-clash/cosmic-clash/server/domain"
|
||||
)
|
||||
|
||||
type Provider interface {
|
||||
Allocate(context.Context, domain.AllocationRequest, map[string]string, time.Time) (agones.AllocatedServer, error)
|
||||
}
|
||||
|
||||
type Durable interface {
|
||||
RecordProviderAllocation(context.Context, domain.Allocation, time.Time) (domain.Allocation, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Provider Provider
|
||||
Durable Durable
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
func (s Service) Allocate(ctx context.Context, request domain.AllocationRequest, labels map[string]string) (agones.AllocatedServer, error) {
|
||||
if s.Provider == nil || s.Durable == nil || s.Now == nil {
|
||||
return agones.AllocatedServer{}, errNotConfigured
|
||||
}
|
||||
now := s.Now()
|
||||
result, err := s.Provider.Allocate(ctx, request, labels, now)
|
||||
if err != nil {
|
||||
return agones.AllocatedServer{}, err
|
||||
}
|
||||
if _, err := s.Durable.RecordProviderAllocation(ctx, result.Allocation, now); err != nil {
|
||||
return agones.AllocatedServer{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var errNotConfigured = &configurationError{}
|
||||
|
||||
type configurationError struct{}
|
||||
|
||||
func (*configurationError) Error() string { return "allocator service is not configured" }
|
||||
@@ -0,0 +1,54 @@
|
||||
package allocator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cosmic-clash/cosmic-clash/server/agones"
|
||||
"github.com/cosmic-clash/cosmic-clash/server/domain"
|
||||
)
|
||||
|
||||
type providerSpy struct {
|
||||
calls int
|
||||
result agones.AllocatedServer
|
||||
err error
|
||||
}
|
||||
|
||||
func (p *providerSpy) Allocate(_ context.Context, _ domain.AllocationRequest, _ map[string]string, _ time.Time) (agones.AllocatedServer, error) {
|
||||
p.calls++
|
||||
return p.result, p.err
|
||||
}
|
||||
|
||||
type durableSpy struct {
|
||||
calls int
|
||||
allocation domain.Allocation
|
||||
err error
|
||||
}
|
||||
|
||||
func (d *durableSpy) RecordProviderAllocation(_ context.Context, allocation domain.Allocation, _ time.Time) (domain.Allocation, error) {
|
||||
d.calls++
|
||||
d.allocation = allocation
|
||||
return allocation, d.err
|
||||
}
|
||||
|
||||
func TestServiceDurablyRecordsProviderAllocationBeforeReturning(t *testing.T) {
|
||||
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: "a", MatchID: "m", ServerID: "gs", State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}}
|
||||
durable := &durableSpy{}
|
||||
service := Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1000, 0) }}
|
||||
result, err := service.Allocate(context.Background(), domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}, map[string]string{"region": "EU"})
|
||||
if err != nil || result.Endpoint == "" || durable.calls != 1 || durable.allocation.ServerID != "gs" {
|
||||
t.Fatalf("result=%+v err=%v durable=%+v", result, err, durable)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceDoesNotReturnProviderResultAfterDurableFailure(t *testing.T) {
|
||||
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: "a", State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}}
|
||||
durable := &durableSpy{err: errors.New("database unavailable")}
|
||||
service := Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1000, 0) }}
|
||||
result, err := service.Allocate(context.Background(), domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}, map[string]string{"region": "EU"})
|
||||
if err == nil || result.Endpoint != "" || durable.calls != 1 {
|
||||
t.Fatalf("result=%+v err=%v calls=%d", result, err, durable.calls)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user