rate

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package rate defines Rate and provides basic interfaces for rate limiter driver implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BypassLimiter

type BypassLimiter struct {
	Limiter
	// contains filtered or unexported fields
}

BypassLimiter uses a TagFilter to selectively apply a Limiter.

func (*BypassLimiter) Take

func (b *BypassLimiter) Take(
	ctx context.Context,
	tag string,
	tokens float64,
) (
	remaining float64,
	ok bool,
	err error,
)

Take consumes tokens, if they are available.

type LeakyBucket

type LeakyBucket struct {
	// contains filtered or unexported fields
}

LeakyBucket keeps track of available tokens according to a given Rate.

func NewLeakyBucket

func NewLeakyBucket(at time.Time, r *Rate, burstLimit float64) *LeakyBucket

NewLeakyBucket returns a full LeakyBucket.

func (*LeakyBucket) Refill

func (l *LeakyBucket) Refill(at time.Time, r *Rate, burstLimit float64)

Refill calculates and returns tokens since last update to given time at a Rate. Restored tokens will not exceed the burst limit.

func (*LeakyBucket) Remaining

func (l *LeakyBucket) Remaining() float64

Remaining returns the number of tokens in the bucket. Use only after running LeakyBucket.Refill.

func (*LeakyBucket) Take

func (l *LeakyBucket) Take(tokens float64) (remaining float64, ok bool)

Take removes tokens from the bucket, if that many are available. Use only after running LeakyBucket.Refill.

func (*LeakyBucket) Touched

func (l *LeakyBucket) Touched() time.Time

Touched returns the last time the bucket was updated.

type Limiter

type Limiter interface {
	Rate() *Rate
	Remaining(
		ctx context.Context,
		tag string,
	) (float64, error)
	Take(
		ctx context.Context,
		tag string,
		tokens float64,
	) (
		remaining float64,
		ok bool,
		err error,
	)
}

Limiter contrains the number of consumed tokens to a certain Rate.

func NewBypassLimiter

func NewBypassLimiter(next Limiter, filter TagFilter) (Limiter, error)

NewBypassLimiter attaches a TagFilter to a Limiter.

func NewListBypassLimiter

func NewListBypassLimiter(next Limiter, skipTags ...string) (Limiter, error)

NewListBypassLimiter creates a BypassLimiter that never limits request tags from a given list.

type Rate

type Rate struct {
	// contains filtered or unexported fields
}

Rate represents replenishment of tokens per interval of time. The original interval is preserved in order to calculate default burst.

func New

func New(limit float64, interval time.Duration) (*Rate, error)

New creates a Rate based on expected limit and a given time interval.

func (*Rate) Burst

func (r *Rate) Burst() float64

Burst returns the number of tokens replenished per original interval.

func (*Rate) FasterThan

func (r *Rate) FasterThan(a *Rate) bool

FasterThan returns true if this Rate replenishes more tokens per nanosecond than the other.

func (*Rate) Interval

func (r *Rate) Interval() time.Duration

Interval returns the duration of expected replenishment time.

func (*Rate) LogValue

func (r *Rate) LogValue() slog.Value

LogValue satisfies slog.Valuer in order to provide more information when logging.

func (*Rate) PerNanosecond

func (r *Rate) PerNanosecond() float64

PerNanosecond returns the amount of tokens replenished per nanosecond.

func (*Rate) ReplenishedTokens

func (r *Rate) ReplenishedTokens(from, to time.Time) float64

ReplenishedTokens returns fractional token amount based on time passed.

func (*Rate) SlowerThan

func (r *Rate) SlowerThan(a *Rate) bool

SlowerThan returns true if this Rate replenishes less tokens per nanosecond than the other.

func (*Rate) String

func (r *Rate) String() string

String expresses the Rate in format convenient for humans.

func (*Rate) Validate

func (r *Rate) Validate() error

Validate checks that the Rate is real and reasonable.

type TagFilter

type TagFilter func(tag string) bool

TagFilter directs a BypassLimiter to drop tags that return false.

Jump to

Keyboard shortcuts

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