ratelimit

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 18 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractRateLimitData added in v0.2.21

func ExtractRateLimitData(statusCode int, header *http.Header) (*v2.RateLimitDescription, error)

func NewLimiter

NewLimiter configures a RateLimitServer server.

func ObserveWait added in v0.20.1

func ObserveWait(ctx context.Context, ev WaitEvent)

ObserveWait reports a completed wait to the observer carried by ctx, if any. Sleep sites must call this after the sleep completes with the time actually slept — on context cancellation report only the elapsed portion, never the planned duration.

func UnaryInterceptor

func UnaryInterceptor(now func() time.Time, descriptors ...*ratelimitV1.RateLimitDescriptors_Entry) grpc.UnaryClientInterceptor

UnaryInterceptor returns a new unary server interceptors that adds zap.Logger to the context.

func WaitLabelFromContext added in v0.20.1

func WaitLabelFromContext(ctx context.Context) (string, bool)

WaitLabelFromContext returns the wait attribution label, if present.

func WithWaitLabel added in v0.20.1

func WithWaitLabel(ctx context.Context, label string) context.Context

WithWaitLabel attaches a bounded attribution label (e.g. a resource type) to waits reported from this context. Lives here (not pkg/retry) because both the rate-limit gates and the retryer report through this package's observer; pkg/retry re-exports it for compatibility.

func WithWaitObserver added in v0.20.1

func WithWaitObserver(ctx context.Context, fn WaitObserver) context.Context

WithWaitObserver returns a context that carries fn. Any rate-limit gate or retryer that sleeps while handling a request made with this context (or one derived from it) reports the actual slept duration to fn after the sleep completes.

Types

type MemRateLimiter

type MemRateLimiter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewFixedMemoryRateLimiter

func NewFixedMemoryRateLimiter(ctx context.Context, now func() time.Time, rate int64, period time.Duration) *MemRateLimiter

NewFixedMemoryRateLimiter returns an in-memory limiter that allows rate/period requests e.g. 100/second, 1000/minute.

func NewSlidingMemoryRateLimiter

func NewSlidingMemoryRateLimiter(ctx context.Context, now func() time.Time, usePercent float64) *MemRateLimiter

NewSlidingMemoryRateLimiter returns an in-memory limiter that attempts to use rate limiting reports to define a window size and set the limits to a fair amount given the `usePercent` argument.

func (*MemRateLimiter) Do

TODO

func (*MemRateLimiter) Report

Report updates the rate limiter with relevant information.

type NoOpRateLimiter

type NoOpRateLimiter struct{}

func (*NoOpRateLimiter) Do

func (*NoOpRateLimiter) Report

type WaitEvent added in v0.20.1

type WaitEvent struct {
	// Duration is the time actually slept. When the sleep is cut short by
	// context cancellation this is the elapsed portion only, never the
	// planned wait, so cut-short sleeps don't inflate wait stats.
	Duration time.Duration

	// Retry is true for plain retry backoff sleeps (transient errors with no
	// rate-limit evidence). The ZERO VALUE means rate-limited: emitters that
	// predate this field (rate-limit gates, the hosted connector manager)
	// only ever reported rate-limit sleeps, so their events keep landing in
	// the rate_limit_wait bucket without a code change.
	Retry bool
}

WaitEvent describes one completed wait: a rate-limit gate sleep or a retry backoff sleep. It is a struct so new fields can be added later without breaking WaitObserver implementations. Attribution (e.g. resource type) travels on the context via WithWaitLabel rather than in the event.

type WaitObserver added in v0.20.1

type WaitObserver func(ctx context.Context, ev WaitEvent)

WaitObserver is notified each time a rate-limit gate or retryer slept before (re)issuing a request. Callers that account for wait time (e.g. the syncer's sync stats) install one via WithWaitObserver; sleep sites report via ObserveWait.

Jump to

Keyboard shortcuts

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