Documentation
¶
Index ¶
- func ExtractRateLimitData(statusCode int, header *http.Header) (*v2.RateLimitDescription, error)
- func NewLimiter(ctx context.Context, now func() time.Time, cfg *ratelimitV1.RateLimiterConfig) (ratelimitV1.RateLimiterServiceServer, error)
- func ObserveWait(ctx context.Context, ev WaitEvent)
- func UnaryInterceptor(now func() time.Time, descriptors ...*ratelimitV1.RateLimitDescriptors_Entry) grpc.UnaryClientInterceptor
- func WaitLabelFromContext(ctx context.Context) (string, bool)
- func WithWaitLabel(ctx context.Context, label string) context.Context
- func WithWaitObserver(ctx context.Context, fn WaitObserver) context.Context
- type MemRateLimiter
- type NoOpRateLimiter
- type WaitEvent
- type WaitObserver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractRateLimitData ¶ added in v0.2.21
func NewLimiter ¶
func NewLimiter(ctx context.Context, now func() time.Time, cfg *ratelimitV1.RateLimiterConfig) (ratelimitV1.RateLimiterServiceServer, error)
NewLimiter configures a RateLimitServer server.
func ObserveWait ¶ added in v0.20.1
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
WaitLabelFromContext returns the wait attribution label, if present.
func WithWaitLabel ¶ added in v0.20.1
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 ¶
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 ¶
func (m *MemRateLimiter) Do(ctx context.Context, req *ratelimitV1.DoRequest) (*ratelimitV1.DoResponse, error)
TODO
func (*MemRateLimiter) Report ¶
func (m *MemRateLimiter) Report(ctx context.Context, req *ratelimitV1.ReportRequest) (*ratelimitV1.ReportResponse, error)
Report updates the rate limiter with relevant information.
type NoOpRateLimiter ¶
type NoOpRateLimiter struct{}
func (*NoOpRateLimiter) Do ¶
func (r *NoOpRateLimiter) Do(ctx context.Context, req *v1.DoRequest) (*v1.DoResponse, error)
func (*NoOpRateLimiter) Report ¶
func (r *NoOpRateLimiter) Report(ctx context.Context, req *v1.ReportRequest) (*v1.ReportResponse, error)
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
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.