Documentation
¶
Overview ¶
Package stabilization provides a rate stabilization detector. It detects when the rate of events becomes stable over a configured number of time periods.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
Clock interface for abstracting time operations
var SystemClock Clock = &systemClock{}
Use SystemClock as the default
type Config ¶
type Config struct { // PeriodDuration is the length of each time period for calculating event rates. // Must be greater than zero. Time between measurements. PeriodDuration time.Duration // MinimumPeriods is the number of initial periods to wait *before* // the NumPeriodsForStabilization window is considered for stabilization checks. MinimumPeriods int // NumPeriodsForStabilization is the number of consecutive recent periods to check // for rate stabilization. Must be at least 2. NumPeriodsForStabilization int // Stability threshold: Maximum acceptable deviation (lower = more strict). StabilizationFactor float64 // WarmupTime forces stabilization if not detected naturally within this duration // after monitoring starts. WarmupTime time.Duration // Clock is an optional custom clock for testing. Defaults to SystemClock if nil. Clock Clock }
Config holds the configuration parameters for the rate stabilization detector.
type Detector ¶
type Detector struct { OnMonitoringStart func(t time.Time) OnPeriodComplete func(t time.Time, countInPeriod int, stDev float64) OnStabilized func(t time.Time, totalCount int) // contains filtered or unexported fields }
Detector detects when the rate of events stabilizes over a defined period. It calculates the number of events per period and checks if the rates in the last 'NumPeriodsForStabilization' are similar within a given factor.
func NewDetector ¶
NewDetector creates a new rate stabilization detector.
func (*Detector) Close ¶
func (d *Detector) Close()
Close stops the detector and releases any resources.
func (*Detector) IsStabilized ¶
IsStabilized returns true if the detector is currently in the StateStabilized.
func (*Detector) Record ¶
Record signals that an event has occurred. It updates the internal state and may trigger state transitions or callbacks. Returns the timestamp when the event was recorded. If the state is already Stabilized, this function does nothing and returns zero time.
func (*Detector) Subscribe ¶
func (d *Detector) Subscribe() (c <-chan struct{}, cancel func())
Subscribe returns a channel (c) signaling stabilization and a cancel function. The channel notifies when stabilization is triggered (or immediately if already stable). Calling cancel() when the subscription is no longer needed is recommended to unsubscribe and release associated resources promptly.
type RateState ¶
type RateState int
RateState represents the detected state of the event rate stabilization.
const ( // StateIdle indicates the detector is inactive, waiting for the first event. StateIdle RateState = iota // StateMonitoring indicates events are being recorded and checked for rate stability over periods. StateMonitoring // StateStabilized indicates the event rate has been consistent for the configured // number of periods, or the warmup time has elapsed. The detector will remain in this state. StateStabilized )
type Subscriber ¶
type Subscriber interface { // Subscribe returns a channel that will receive a notification when the // stabilization reaches the Stabilized state. Subscribe() (c <-chan struct{}, cancel func()) // IsStabilized returns true if the detector is in the Stabilized state. IsStabilized() bool }
Subscriber defines the interface for stabilization subscription.