reactivelimiter

package
v0.0.0-...-5506d2e Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExceeded = errors.New("limit exceeded")

ErrExceeded is returned when an execution exceeds the current limit.

Functions

This section is empty.

Types

type Config

type Config struct {
	Enabled                bool          `yaml:"enabled" category:"experimental"`
	ShortWindowMinDuration time.Duration `yaml:"short_window_min_duration" category:"experimental"`
	ShortWindowMaxDuration time.Duration `yaml:"short_window_max_duration" category:"experimental"`
	ShortWindowMinSamples  uint          `yaml:"short_window_min_samples" category:"experimental"`
	LongWindow             uint          `yaml:"long_window" category:"experimental"`
	SampleQuantile         float64       `yaml:"sample_quantile" category:"experimental"`
	MinInflightLimit       uint          `yaml:"min_inflight_limit" category:"experimental"`
	MaxInflightLimit       uint          `yaml:"max_inflight_limit" category:"experimental"`
	InitialInflightLimit   uint          `yaml:"initial_inflight_limit" category:"experimental"`
	MaxLimitFactor         float64       `yaml:"max_limit_factor" category:"experimental"`
	CorrelationWindow      uint          `yaml:"correlation_window" category:"experimental"`
	InitialRejectionFactor float64       `yaml:"initial_rejection_factor" category:"experimental"`
	MaxRejectionFactor     float64       `yaml:"max_rejection_factor" category:"experimental"`
	// contains filtered or unexported fields
}

func (*Config) RegisterFlagsWithPrefix

func (cfg *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet)

type Metrics

type Metrics interface {
	// Limit returns the concurrent execution limit, as calculated by the reactive limiter.
	Limit() int

	// Inflight returns the current number of inflight executions.
	Inflight() int

	// Blocked returns the current number of blocked executions.
	Blocked() int

	// RejectionRate for blocking limiters returns the current rate, from 0 to 1, at which the limiter will reject requests.
	// Returns 0 for limiters that are not blocking.
	RejectionRate() float64
}

Metrics provides info about the reactive limiter.

This type is concurrency safe.

type Permit

type Permit interface {
	// Record records an execution completion and releases a permit back to the limiter. The execution duration will be used
	// to influence the limiter.
	Record()

	// Drop releases an execution permit back to the limiter without recording a completion. This should be used when an
	// execution completes prematurely, such as via a timeout, and we don't want the execution duration to influence the
	// limiter.
	Drop()
}

Permit is a permit to perform an execution that must be completed by calling Record or Drop.

type Prioritizer

type Prioritizer interface {
	// RejectionRate returns the current rate, from 0 to 1, at which the limiter will reject requests, based on recent
	// execution times.
	RejectionRate() float64

	// RejectionThreshold is the priority threshold below which requests will be rejected, based on their priority, from 0 to 499.
	RejectionThreshold() int

	// Calibrate calibrates the RejectionRate based on recent execution times from registered limiters.
	Calibrate()
	// contains filtered or unexported methods
}

Prioritizer computes a rejection rate and priority threshold for one or more priority limiters, which can be used to determine whether to accept or reject an execution.

func NewPrioritizer

func NewPrioritizer(logger log.Logger) Prioritizer

type Priority

type Priority int
const (
	PriorityVeryLow Priority = iota
	PriorityLow
	PriorityMedium
	PriorityHigh
	PriorityVeryHigh
)

type PriorityLimiter

type PriorityLimiter interface {
	Metrics

	// AcquirePermit attempts to acquire a permit, potentially blocking up to maxExecutionTime.
	// The request priority must be greater than the current priority threshold for admission.
	AcquirePermit(ctx context.Context, priority Priority) (Permit, error)

	// CanAcquirePermit returns whether it's currently possible to acquire a permit for the priority.
	CanAcquirePermit(priority Priority) bool
}

PriorityLimiter is a reactive concurrency limiter that can prioritize request rejections via a Prioritizer.

func NewPriorityLimiter

func NewPriorityLimiter(config *Config, prioritizer Prioritizer, logger log.Logger) PriorityLimiter

type RejectionPrioritizerConfig

type RejectionPrioritizerConfig struct {
	CalibrationInterval time.Duration `yaml:"calibration_interval" category:"experimental"`
}

func (*RejectionPrioritizerConfig) RegisterFlagsWithPrefix

func (cfg *RejectionPrioritizerConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet)

Jump to

Keyboard shortcuts

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