jitter

package
v0.47.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 6 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JitteredIntervalDuration

func JitteredIntervalDuration(d time.Duration) time.Duration

JitteredIntervalDuration returns a jittered duration based on the given duration.

To use this function, you must first initialize the global jitter with SetGlobalIntervalJitter.

func JitteredRequeueInterval

func JitteredRequeueInterval(res ctrl.Result) ctrl.Result

JitteredRequeueInterval returns a result with a requeue-after interval that has been jittered. It will not modify the result if it is zero or is marked to requeue immediately.

To use this function, you must first initialize the global jitter with SetGlobalIntervalJitter.

func NoJitter

func NoJitter(d time.Duration) time.Duration

NoJitter is a Duration function that will return the given duration without modification.

func SetGlobalIntervalJitter

func SetGlobalIntervalJitter(p float64, rand *rand.Rand)

SetGlobalIntervalJitter sets the global interval jitter. It is safe to call this method multiple times, but only the first call will have an effect.

Types

type Duration

type Duration func(time.Duration) time.Duration

Duration is a function that takes a duration and returns a modified duration with jitter added.

func Percent

func Percent(p float64, r *rand.Rand) Duration

Percent returns a Duration function that will modify the given duration by a random percentage between 0 and p, with the sign chosen randomly.

For example, if percent is 0.1, the returned Duration will modify the duration by a random percentage between -10% and 10%.

When p <= 0 or p >= 1, duration is returned without a modification. If r is nil, a new rand.Rand will be created using the current time as the seed.

type IntervalOptions

type IntervalOptions struct {
	// Percentage of jitter to apply to interval durations. A value of 10
	// will apply a jitter of +/-10% to the interval duration. It can not be negative,
	// and must be less than 100.
	Percentage uint8
}

IntervalOptions is used to configure the interval jitter for a controller using command line flags. To use it, create an IntervalOptions and call BindFlags, then call SetGlobalJitter with a rand.Rand (or nil to use the default).

Applying jitter to the interval duration can be useful to mitigate spikes in memory and CPU usage caused by many resources being configured with the same interval.

When 1000 resources are configured to requeue every 5 minutes with a concurrency setting of 50 and a process time of approximately 1 second per resource.

Without jitter, all 1000 resources will requeue every 5 minutes, resulting in 50 resources requeueing simultaneously every second over a 20-second window.

However, when we apply +/-10% jitter to the interval duration, the requeueing will be spread out over a 1-minute window. As a result, the number of resources requeueing per second will vary between approximately 15 to 18.33.

This smoother workload distribution can result in significant reductions in the impact of CPU and memory spikes. This improvement in workload distribution also translates into benefits for the Go garbage collector. Notably, the garbage collector experiences reduced GC bursts and more frequent collections, leading to improved overall performance.

func (*IntervalOptions) BindFlags

func (o *IntervalOptions) BindFlags(fs *pflag.FlagSet)

BindFlags will parse the given pflag.FlagSet and load the interval jitter with the default value of 5%.

func (*IntervalOptions) BindFlagsWithDefault

func (o *IntervalOptions) BindFlagsWithDefault(fs *pflag.FlagSet, defaultPercentage uint8)

BindFlagsWithDefault will parse the given pflag.FlagSet and load the interval jitter. The defaultPercentage is used to set the default value for the interval jitter percentage.

func (*IntervalOptions) SetGlobalJitter

func (o *IntervalOptions) SetGlobalJitter(rand *rand.Rand) error

SetGlobalJitter sets the global interval jitter. It is safe to call this method multiple times, but only the first call will have an effect.

Jump to

Keyboard shortcuts

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