Documentation
¶
Overview ¶
Package resource provides system resource monitoring and control.
Index ¶
- type Controller
- func (c *Controller) AcquireSlot(ctx context.Context) bool
- func (c *Controller) AcquireSlotBlocking(ctx context.Context) error
- func (c *Controller) AdaptiveMaxJobs() int
- func (c *Controller) ForceGC()
- func (c *Controller) GetMetrics() *SystemMetrics
- func (c *Controller) GetStatus() *ControllerStatus
- func (c *Controller) IsThrottled() bool
- func (c *Controller) ReleaseSlot()
- func (c *Controller) SetMaxConcurrentJobs(max int)
- func (c *Controller) Start(ctx context.Context) error
- func (c *Controller) Stop()
- type ControllerConfig
- type ControllerStatus
- type SystemMetrics
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Controller ¶
type Controller struct {
// contains filtered or unexported fields
}
Controller monitors system resources and controls work admission.
func NewController ¶
func NewController(config *ControllerConfig) *Controller
NewController creates a new resource controller.
func (*Controller) AcquireSlot ¶
func (c *Controller) AcquireSlot(ctx context.Context) bool
AcquireSlot attempts to acquire a job slot. Returns true if a slot was acquired, false if throttled or at capacity. The caller MUST call ReleaseSlot when done.
func (*Controller) AcquireSlotBlocking ¶
func (c *Controller) AcquireSlotBlocking(ctx context.Context) error
AcquireSlotBlocking blocks until a slot is available or context is canceled.
func (*Controller) AdaptiveMaxJobs ¶
func (c *Controller) AdaptiveMaxJobs() int
AdaptiveMaxJobs calculates recommended max jobs based on current load.
func (*Controller) GetMetrics ¶
func (c *Controller) GetMetrics() *SystemMetrics
GetMetrics returns the current system metrics.
func (*Controller) GetStatus ¶
func (c *Controller) GetStatus() *ControllerStatus
GetStatus returns the current controller status.
func (*Controller) IsThrottled ¶
func (c *Controller) IsThrottled() bool
IsThrottled returns true if resource thresholds are exceeded.
func (*Controller) ReleaseSlot ¶
func (c *Controller) ReleaseSlot()
ReleaseSlot releases a previously acquired job slot.
func (*Controller) SetMaxConcurrentJobs ¶
func (c *Controller) SetMaxConcurrentJobs(max int)
SetMaxConcurrentJobs dynamically adjusts the max concurrent jobs.
type ControllerConfig ¶
type ControllerConfig struct {
// CPUThreshold is the CPU percentage above which new work is paused.
// Default: 85%
CPUThreshold float64
// MemoryThreshold is the memory percentage above which new work is paused.
// Default: 85%
MemoryThreshold float64
// MaxConcurrentJobs limits the number of concurrent jobs.
// Default: runtime.NumCPU()
MaxConcurrentJobs int
// MinConcurrentJobs is the minimum jobs to allow even under load.
// Default: 1
MinConcurrentJobs int
// SampleInterval is how often to sample system metrics.
// Default: 5 seconds
SampleInterval time.Duration
// CooldownDuration is how long to wait after threshold exceeded.
// Default: 30 seconds
CooldownDuration time.Duration
// OnThresholdExceeded is called when resource threshold is exceeded.
OnThresholdExceeded func(metrics *SystemMetrics, reason string)
// OnThresholdCleared is called when resources return to normal.
OnThresholdCleared func(metrics *SystemMetrics)
// Verbose enables debug logging.
Verbose bool
}
ControllerConfig configures the resource controller.
func DefaultControllerConfig ¶
func DefaultControllerConfig() *ControllerConfig
DefaultControllerConfig returns sensible defaults.
type ControllerStatus ¶
type ControllerStatus struct {
Throttled bool `json:"throttled"`
ThrottledAt time.Time `json:"throttled_at,omitempty"`
ThrottleReason string `json:"throttle_reason,omitempty"`
ActiveJobs int `json:"active_jobs"`
PendingJobs int `json:"pending_jobs"`
RejectedJobs int64 `json:"rejected_jobs"`
MaxJobs int `json:"max_jobs"`
Metrics *SystemMetrics `json:"metrics,omitempty"`
}
ControllerStatus represents the current state of the controller.
type SystemMetrics ¶
type SystemMetrics struct {
CPUPercent float64 `json:"cpu_percent"`
MemoryPercent float64 `json:"memory_percent"`
MemoryUsedMB int64 `json:"memory_used_mb"`
MemoryTotalMB int64 `json:"memory_total_mb"`
NumGoroutines int `json:"num_goroutines"`
NumCPU int `json:"num_cpu"`
Timestamp time.Time `json:"timestamp"`
}
SystemMetrics contains current system resource metrics.