anomaly

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicDetector

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

BasicDetector is a state machine to prevent sending requests that are likely to fail.

func NewBasicDetector

func NewBasicDetector(name string, opts Options) *BasicDetector

NewBasicDetector returns a new BasicDetector configured with the given Options.

func (*BasicDetector) Counter

func (d *BasicDetector) Counter() Counter

Counter returns internal counters

func (*BasicDetector) Mark

func (d *BasicDetector) Mark(normality bool) (State, error)

func (*BasicDetector) Name

func (d *BasicDetector) Name() string

Name returns the name of the BasicDetector.

func (*BasicDetector) Reset

func (d *BasicDetector) Reset()

func (*BasicDetector) State

func (d *BasicDetector) State() State

State returns the current state of the BasicDetector.

type Counter

type Counter struct {
	TotalDetects             uint32
	TotalNormalities         uint32
	TotalAbnormalities       uint32
	ConsecutiveNormalities   uint32
	ConsecutiveAbnormalities uint32
}

type Detector

type Detector interface {
	Name() string
	Mark(normality bool) (State, error)
	State() State
	Reset()
}

type Options

type Options struct {
	// Timeout is the period of the open state,
	// after which the state of the BasicDetector becomes half-open.
	// If Timeout is less than or equal to 0, the timeout value of the BasicDetector is set to 60 seconds.
	Timeout time.Duration
	// NormalConditionFn is called with a copy of Counter whenever a request success in the anomaly state.
	// If NormalConditionFn returns true, the BasicDetector will be placed into the ok state.
	// If NormalConditionFn is nil, default NormalConditionFn is used.
	// Default NormalConditionFn returns true when the number of consecutive normalities is more than 3.
	NormalConditionFn func(counter Counter) bool
	// AnomalyConditionFn is called with a copy of Counter whenever a request fails in the ok state.
	// If AnomalyConditionFn returns true, the BasicDetector will be placed into the anomaly state.
	// If AnomalyConditionFn is nil, default AnomalyConditionFn is used.
	// Default AnomalyConditionFn returns true when the number of consecutive abnormalities is more than 5.
	AnomalyConditionFn func(counter Counter) bool
	// OnStateChange is called whenever the state of the BasicDetector changes.
	OnStateChange func(name string, from State, to State)
}

Options configures BasicDetector

type State

type State int

State is a type that represents a state of Detector.

const (
	StateOK State = iota
	StateAnomaly
)

These constants are states of Detector.

func (State) String

func (s State) String() string

String implements stringer interface.

Jump to

Keyboard shortcuts

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