mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-15 12:02:03 +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" }
|
||||
Reference in New Issue
Block a user