File size: 1,212 Bytes
6a7089a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package instance
import (
"fmt"
"github.com/pinchtab/pinchtab/internal/allocation"
"github.com/pinchtab/pinchtab/internal/bridge"
)
// Allocator selects an instance using the configured AllocationPolicy.
// It reads candidates from the Repository and delegates selection to the policy.
type Allocator struct {
repo *Repository
policy allocation.Policy
}
// NewAllocator creates an Allocator with the given policy.
func NewAllocator(repo *Repository, policy allocation.Policy) *Allocator {
return &Allocator{repo: repo, policy: policy}
}
// Allocate selects a running instance using the configured policy.
func (a *Allocator) Allocate() (*bridge.Instance, error) {
candidates := a.repo.Running()
if len(candidates) == 0 {
return nil, fmt.Errorf("no running instances available")
}
selected, err := a.policy.Select(candidates)
if err != nil {
return nil, fmt.Errorf("allocation policy %q failed: %w", a.policy.Name(), err)
}
return &selected, nil
}
// Policy returns the current allocation policy.
func (a *Allocator) Policy() allocation.Policy {
return a.policy
}
// SetPolicy swaps the allocation policy at runtime.
func (a *Allocator) SetPolicy(p allocation.Policy) {
a.policy = p
}
|