stabilization

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: BSD-3-Clause Imports: 5 Imported by: 0

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

type Clock interface {
	Now() time.Time
}

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

func NewDetector(cfg Config) (*Detector, error)

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

func (d *Detector) IsStabilized() bool

IsStabilized returns true if the detector is currently in the StateStabilized.

func (*Detector) Record

func (d *Detector) Record() time.Time

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) State

func (d *Detector) State() RateState

State returns the current detected rate state.

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
)

func (RateState) String

func (rs RateState) String() string

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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