request

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Outcome

type Outcome int

Outcome represents the result of a request for resource allocation. It is used to adjust throttling probabilities for future requests. Reporting a value other than the constants below changes nothing.

const (
	// OutcomeSuccess means the request managed to obtain the resource.
	OutcomeSuccess Outcome = iota

	// OutcomeFailure means the request failed to obtain the resource.
	// This may not always map to a failure in your business logic. For
	// example - failing to validate a request or failing to reach an
	// upstream service because of a network error would not qualify
	// as a failure here. See ReportOutcome function for when to report.
	OutcomeFailure
)

type RegisterRequestResult

type RegisterRequestResult struct {
	// If true, this request should be throttled
	ShouldThrottle bool
	// Probabilities and other useful debugging information
	ResultStats *ResultStats
}

RegisterRequestResult is returned from RegisterRequest and indicates whether the request should be throttled.

type ReportOutcomeResult

type ReportOutcomeResult struct{}

ReportOutcomeResult is returned from ReportOutcome. It currently carries no fields but exists for future expansion.

type ResultStats

type ResultStats struct {
	// The final probability used to make the throttling decision
	FinalProbability float64
	// The chosen bucket index at every level
	BucketIndexes []int
	// The probabilities of the chosen buckets
	BucketProbabilities []float64
}

ResultStats contains probabilities and other debugging information collected while registering a request.

type Tracker

type Tracker interface {
	// Return the int ID of this structure. Used for implementing moving hashes.
	GetID() uint64

	// Register an incoming request from a client identified by a clientIdentifier
	// The clientIdentifier needs to be unique and consistent for every client as
	// it will be used to hash and locate the corresponding buckets.
	RegisterRequest(ctx context.Context, clientIdentifier []byte) *RegisterRequestResult

	// Report the outcome of a request from the given client so we can update the
	// probabilities of the corresponding buckets.
	// Only report the outcomes on the requests where you could either conclusively
	// get the resource or not. For outcomes such as user errors or network failures
	// or timeout with upstream, do NOT report any outcome, or we may wrongly throttle
	// requests based on things not related to resource contention.
	// You don't have to report an outcome to every registered request.
	ReportOutcome(ctx context.Context, clientIdentifier []byte, outcome Outcome) *ReportOutcomeResult

	// Close releases the structure's resources. The fairness tracker calls it
	// once, after a rotation has retired the structure and no request can
	// still be using it. The pair that is live when the fairness tracker
	// itself is closed is not closed, because requests stay valid after that.
	Close()
}

Tracker defines the operations required by the underlying data structure used to make throttling decisions.

Jump to

Keyboard shortcuts

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