resource

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package resource implements per-process and system-wide resource sampling and enforcement.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	ProcessID    string
	Kind         string // "rss" | "cpu"
	Current      float64
	Limit        float64
	ExceedAction cliwrap.ExceedAction
}

Action describes an enforcement decision.

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator tracks per-process limit breaches and fires actions.

func NewEvaluator

func NewEvaluator(opts EvaluatorOptions) *Evaluator

NewEvaluator returns an Evaluator.

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(id string, sample Sample)

Evaluate checks sample against the limits of id and may fire actions.

func (*Evaluator) RegisterLimits

func (e *Evaluator) RegisterLimits(id string, l cliwrap.ResourceLimits)

RegisterLimits associates limits with a process ID.

func (*Evaluator) Unregister

func (e *Evaluator) Unregister(id string)

Unregister removes a process ID.

type EvaluatorOptions

type EvaluatorOptions struct {
	// OnAction is invoked (synchronously) when a sustained breach is detected.
	OnAction func(Action)

	// RSSBreachThreshold sets the consecutive breach count for memory.
	// Default 3.
	RSSBreachThreshold int

	// CPUBreachThreshold sets the consecutive breach count for CPU.
	// Default 10 (CPU samples are noisier).
	CPUBreachThreshold int
}

EvaluatorOptions parameterises NewEvaluator.

type Monitor

type Monitor struct {
	// contains filtered or unexported fields
}

Monitor periodically polls every child process in its Source.

func NewMonitor

func NewMonitor(opts MonitorOptions) *Monitor

NewMonitor returns a Monitor.

func (*Monitor) Start

func (m *Monitor) Start()

Start launches the ticker goroutine. Calling Start more than once is a no-op.

func (*Monitor) Stop

func (m *Monitor) Stop()

Stop halts the ticker and waits for the goroutine to exit.

type MonitorOptions

type MonitorOptions struct {
	Interval         time.Duration
	Source           PIDSource
	ProcessCollector ProcessCollector
	SystemCollector  SystemCollector
	OnSample         func(id string, s Sample)
	OnSystem         func(s SystemSample)
}

MonitorOptions configures a Monitor.

type PIDSource

type PIDSource interface {
	ChildPIDs() map[string]int
}

PIDSource is any object that can report the current set of (id, PID) pairs that should be sampled.

type ProcessCollector

type ProcessCollector interface {
	Collect(pid int) (Sample, error)
}

ProcessCollector returns a Sample for the given PID.

func DefaultProcessCollector

func DefaultProcessCollector() ProcessCollector

DefaultProcessCollector returns the platform's default process collector.

type Sample

type Sample struct {
	PID        int
	RSS        uint64  // bytes
	VMS        uint64  // bytes
	CPUPercent float64 // 0-100 normalized per core
	NumThreads int
	NumFDs     int
	SampledAt  time.Time
}

Sample is one point-in-time reading for a single process.

type SystemCollector

type SystemCollector interface {
	Collect() (SystemSample, error)
}

SystemCollector returns a system-wide sample.

func DefaultSystemCollector

func DefaultSystemCollector() SystemCollector

DefaultSystemCollector returns the platform's default system collector.

type SystemSample

type SystemSample struct {
	TotalMemory     uint64
	AvailableMemory uint64
	LoadAvg1        float64
	LoadAvg5        float64
	NumCPU          int
	SampledAt       time.Time
}

SystemSample is one host-wide reading.

func (SystemSample) AvailableRatio

func (s SystemSample) AvailableRatio() float64

AvailableRatio returns AvailableMemory / TotalMemory, or 0 if TotalMemory is 0.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL