93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"sync/atomic"
|
|
)
|
|
|
|
// BackpressureManager tracks system load and provides admission control.
|
|
// Level 1 (70%): accept but log warning
|
|
// Level 2 (85%): accept only high priority (P0-P1)
|
|
// Level 3 (95%): reject all new requests
|
|
type BackpressureManager struct {
|
|
maxRunning int64
|
|
maxQueued int64
|
|
level1Threshold float64
|
|
level2Threshold float64
|
|
level3Threshold float64
|
|
|
|
runningCount int64 // atomic
|
|
queuedCount int64 // atomic
|
|
}
|
|
|
|
// NewBackpressureManager creates a new backpressure manager.
|
|
func NewBackpressureManager(maxRunning, maxQueued int, l1, l2, l3 float64) *BackpressureManager {
|
|
return &BackpressureManager{
|
|
maxRunning: int64(maxRunning),
|
|
maxQueued: int64(maxQueued),
|
|
level1Threshold: l1,
|
|
level2Threshold: l2,
|
|
level3Threshold: l3,
|
|
}
|
|
}
|
|
|
|
// Update sets the current running and queued counts.
|
|
func (bp *BackpressureManager) Update(running, queued int) {
|
|
atomic.StoreInt64(&bp.runningCount, int64(running))
|
|
atomic.StoreInt64(&bp.queuedCount, int64(queued))
|
|
}
|
|
|
|
// LoadRatio returns the current system load ratio (0.0 to 1.0).
|
|
func (bp *BackpressureManager) LoadRatio() float64 {
|
|
running := atomic.LoadInt64(&bp.runningCount)
|
|
queued := atomic.LoadInt64(&bp.queuedCount)
|
|
total := running + queued
|
|
capacity := bp.maxRunning + bp.maxQueued
|
|
if capacity == 0 {
|
|
return 0
|
|
}
|
|
return float64(total) / float64(capacity)
|
|
}
|
|
|
|
// Level returns the current backpressure level (0=normal, 1=warning, 2=restricted, 3=reject).
|
|
func (bp *BackpressureManager) Level() int {
|
|
load := bp.LoadRatio()
|
|
if load >= bp.level3Threshold {
|
|
return 3
|
|
}
|
|
if load >= bp.level2Threshold {
|
|
return 2
|
|
}
|
|
if load >= bp.level1Threshold {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// ShouldAccept decides whether to accept a request based on priority and load.
|
|
// priority: 0=P0(highest) to 4=P4(lowest)
|
|
func (bp *BackpressureManager) ShouldAccept(priority int) bool {
|
|
level := bp.Level()
|
|
switch level {
|
|
case 0, 1:
|
|
return true
|
|
case 2:
|
|
// Only accept P0 and P1
|
|
return priority <= 1
|
|
case 3:
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// RejectReason returns a human-readable reason if the request should be rejected.
|
|
func (bp *BackpressureManager) RejectReason(priority int) string {
|
|
level := bp.Level()
|
|
if level == 3 {
|
|
return "system overloaded, please retry later"
|
|
}
|
|
if level == 2 && priority > 1 {
|
|
return "backpressure active, only high-priority requests accepted"
|
|
}
|
|
return ""
|
|
}
|