mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 18:13:42 +00:00
feat(multiplayer): expose allocator quota metrics
This commit is contained in:
@@ -34,12 +34,20 @@ type SharedAllocationQuota interface {
|
||||
Consume(context.Context, string, time.Time) error
|
||||
}
|
||||
|
||||
type AllocationMetrics interface {
|
||||
ObserveAttempt(string)
|
||||
ObserveSuccess(string)
|
||||
ObserveFailure(string)
|
||||
ObserveDenied(string)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Provider Provider
|
||||
Durable Durable
|
||||
Roster RosterPublisher
|
||||
Budget AllocationBudget
|
||||
Quota SharedAllocationQuota
|
||||
Metrics AllocationMetrics
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
@@ -88,23 +96,41 @@ func (s Service) Allocate(ctx context.Context, request domain.AllocationRequest,
|
||||
return agones.AllocatedServer{}, errNotConfigured
|
||||
}
|
||||
now := s.Now()
|
||||
if s.Metrics != nil {
|
||||
s.Metrics.ObserveAttempt(request.Region)
|
||||
}
|
||||
if s.Budget != nil {
|
||||
if err := s.Budget.Allow(request.Region, now); err != nil {
|
||||
if s.Metrics != nil {
|
||||
s.Metrics.ObserveDenied(request.Region)
|
||||
}
|
||||
return agones.AllocatedServer{}, err
|
||||
}
|
||||
}
|
||||
if s.Quota != nil {
|
||||
if err := s.Quota.Consume(ctx, request.Region, now); err != nil {
|
||||
if s.Metrics != nil {
|
||||
s.Metrics.ObserveDenied(request.Region)
|
||||
}
|
||||
return agones.AllocatedServer{}, err
|
||||
}
|
||||
}
|
||||
result, err := s.Provider.Allocate(ctx, request, labels, now)
|
||||
if err != nil {
|
||||
if s.Metrics != nil {
|
||||
s.Metrics.ObserveFailure(request.Region)
|
||||
}
|
||||
return agones.AllocatedServer{}, err
|
||||
}
|
||||
if _, err := s.Durable.RecordProviderAllocation(ctx, result.Allocation, now); err != nil {
|
||||
if s.Metrics != nil {
|
||||
s.Metrics.ObserveFailure(request.Region)
|
||||
}
|
||||
return agones.AllocatedServer{}, err
|
||||
}
|
||||
if s.Metrics != nil {
|
||||
s.Metrics.ObserveSuccess(request.Region)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -114,10 +140,21 @@ func (s Service) RecordProviderAllocation(ctx context.Context, result agones.All
|
||||
}
|
||||
if s.Quota != nil {
|
||||
if err := s.Quota.Consume(ctx, result.Allocation.Region, now); err != nil {
|
||||
if s.Metrics != nil {
|
||||
s.Metrics.ObserveDenied(result.Allocation.Region)
|
||||
}
|
||||
return domain.Allocation{}, err
|
||||
}
|
||||
}
|
||||
return s.Durable.RecordProviderAllocation(ctx, result.Allocation, now)
|
||||
allocation, err := s.Durable.RecordProviderAllocation(ctx, result.Allocation, now)
|
||||
if s.Metrics != nil {
|
||||
if err != nil {
|
||||
s.Metrics.ObserveFailure(result.Allocation.Region)
|
||||
} else {
|
||||
s.Metrics.ObserveSuccess(result.Allocation.Region)
|
||||
}
|
||||
}
|
||||
return allocation, err
|
||||
}
|
||||
|
||||
var errNotConfigured = &configurationError{}
|
||||
|
||||
Reference in New Issue
Block a user