perf: index routing snapshots and quota usage

This commit is contained in:
kami
2026-07-30 15:36:23 +04:00
parent e8fadfc998
commit fbb13c89d2
8 changed files with 577 additions and 63 deletions
+30 -11
View File
@@ -227,6 +227,20 @@ func (TCPReachability) Reachable(address string, timeout time.Duration) bool {
return true
}
func (r Registry) Candidates(project string, check Reachability, timeout time.Duration) ([]Herdr, error) {
return r.candidates(project, func(h Herdr) bool {
address := r.Endpoint(h)
return check == nil || check.Reachable(address, timeout)
})
}
// CandidatesWithHealth filters candidates using a health snapshot gathered by
// the router. Keeping probe execution outside this method lets a scheduling
// pass probe each herdr once, in parallel, instead of once per queued task.
func (r Registry) CandidatesWithHealth(project string, healthy map[string]bool) ([]Herdr, error) {
return r.candidates(project, func(h Herdr) bool { return healthy[h.ID] })
}
func (r Registry) candidates(project string, include func(Herdr) bool) ([]Herdr, error) {
p, ok := r.projects[project]
if !ok {
return nil, ErrUnknownProject
@@ -237,20 +251,25 @@ func (r Registry) Candidates(project string, check Reachability, timeout time.Du
}
out := []Herdr{}
for _, h := range r.herdrs {
if !allowed[h.MachineID] {
if !allowed[h.MachineID] || !include(h) {
continue
}
addr := h.Address
if addr == "" {
addr = r.machines[h.MachineID].Address
if host, _, err := net.SplitHostPort(addr); err == nil {
addr = net.JoinHostPort(host, defaultHerdrPort)
}
}
if check == nil || check.Reachable(addr, timeout) {
out = append(out, h)
}
out = append(out, h)
}
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
return out, nil
}
// Endpoint resolves the herdr-specific address or its machine's default
// herdr endpoint. It is exposed so health checks can be batched independently
// from project routing.
func (r Registry) Endpoint(h Herdr) string {
if h.Address != "" {
return h.Address
}
address := r.machines[h.MachineID].Address
if host, _, err := net.SplitHostPort(address); err == nil {
return net.JoinHostPort(host, defaultHerdrPort)
}
return address
}