mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-07-09 16:00:59 +00:00
Tighten v5-to-v6 upgrade safety, release installability, provider MSP mode handling, AI cost accounting, metrics flushing, and frontend guardrails for the v6.0.0 GA candidate.
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
func (m *Monitor) efficientQEMUWorkerCount(total int) int {
|
|
if total <= 0 {
|
|
return 0
|
|
}
|
|
|
|
limit := defaultGuestAgentVMMaxConcurrent
|
|
if m != nil && m.guestAgentWorkSlots != nil && cap(m.guestAgentWorkSlots) > 0 {
|
|
limit = cap(m.guestAgentWorkSlots)
|
|
}
|
|
if limit <= 0 || limit > total {
|
|
return total
|
|
}
|
|
return limit
|
|
}
|
|
|
|
func (m *Monitor) acquireGuestAgentWorkSlot(ctx context.Context) bool {
|
|
if m == nil || m.guestAgentWorkSlots == nil {
|
|
return true
|
|
}
|
|
select {
|
|
case m.guestAgentWorkSlots <- struct{}{}:
|
|
return true
|
|
case <-ctx.Done():
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (m *Monitor) releaseGuestAgentWorkSlot() {
|
|
if m == nil || m.guestAgentWorkSlots == nil {
|
|
return
|
|
}
|
|
select {
|
|
case <-m.guestAgentWorkSlots:
|
|
default:
|
|
}
|
|
}
|
|
|
|
func (m *Monitor) guestAgentVMWorkContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
|
if m == nil || m.guestAgentVMBudget <= 0 {
|
|
return ctx, func() {}
|
|
}
|
|
return context.WithTimeout(ctx, m.guestAgentVMBudget)
|
|
}
|
|
|
|
func (m *Monitor) runGuestAgentVMWork(ctx context.Context, work func(context.Context)) bool {
|
|
if !m.acquireGuestAgentWorkSlot(ctx) {
|
|
return false
|
|
}
|
|
defer m.releaseGuestAgentWorkSlot()
|
|
|
|
workCtx, cancel := m.guestAgentVMWorkContext(ctx)
|
|
defer cancel()
|
|
work(workCtx)
|
|
return true
|
|
}
|
|
|
|
func guestAgentVMBudgetOrZero(m *Monitor) time.Duration {
|
|
if m == nil {
|
|
return 0
|
|
}
|
|
return m.guestAgentVMBudget
|
|
}
|