jitter

package
v0.11.21 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package jitter adds randomized jitter to backoff and retry durations to avoid a thundering herd. It guards against non-positive inputs so callers can pass values like time.Until(resetTime) directly — a rate-limit reset that has already elapsed yields a negative duration, which must not panic.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(d time.Duration) time.Duration

Add adds 0% to +100% random jitter to avoid thundering herd. A non-positive duration is returned unchanged, so callers that pass the result to workqueue.RequeueAfter stay safe when a rate-limit reset time has already elapsed and time.Until returns a negative duration.

Example
package main

import (
	"fmt"
	"time"

	"chainguard.dev/driftlessaf/internal/jitter"
)

func main() {
	// A non-positive duration is returned unchanged.
	fmt.Println(jitter.Add(0))
	fmt.Println(jitter.Add(-time.Second) == -time.Second)

	// A positive duration is returned with 0–100% jitter added.
	result := jitter.Add(time.Second)
	fmt.Println(result >= time.Second && result <= 2*time.Second)
}
Output:
0s
true
true

Types

This section is empty.

Jump to

Keyboard shortcuts

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