bucketRateLimiter

package
v0.0.0-...-c6ea6ab Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: LGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BucketRateLimiter

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

BucketRateLimiter main struct

Example
ctx := context.Background()
burst := 2
limiter := New(ctx, Every(100*time.Millisecond), burst)
defer func() {
	traceError(limiter.Close())
}()

start := time.Now()
bucketA := "a"
bucketB := "b"

traceError(limiter.Wait(ctx, bucketA))
fmt.Println("do bucket", bucketA)
traceError(limiter.Wait(ctx, bucketA))
fmt.Println("do bucket", bucketA)
traceError(limiter.Wait(ctx, bucketB))
fmt.Println("do bucket", bucketB)
traceError(limiter.Wait(ctx, bucketB))
fmt.Println("do bucket", bucketB)
traceError(limiter.Wait(ctx, bucketA))
fmt.Println("do bucket", bucketA)
traceError(limiter.Wait(ctx, bucketB))
fmt.Println("do bucket", bucketB)
fmt.Println(time.Now().Sub(start).Truncate(100 * time.Millisecond))
Output:

do bucket a
do bucket a
do bucket b
do bucket b
do bucket a
do bucket b
100ms

func New

func New(
	ctx context.Context,
	limit Limit,
	burst int,
	options ...interface{},
) (limiter *BucketRateLimiter)

New return BucketRateLimiter

func (*BucketRateLimiter) Close

func (t *BucketRateLimiter) Close() (err error)

Close cancel context to stop cleaner loop

func (*BucketRateLimiter) Wait

func (t *BucketRateLimiter) Wait(ctx context.Context, bucket string) (err error)

Wait is shorthand for WaitN(ctx, 1).

func (*BucketRateLimiter) WaitN

func (t *BucketRateLimiter) WaitN(ctx context.Context, bucket string, n int) (err error)

WaitN blocks until lim permits n events to happen. It returns an error if n exceeds the Limiter's burst size, the Context is canceled, or the expected wait time exceeds the Context's Deadline. The burst limit is ignored if the rate limit is Inf.

type Limit

type Limit = rate.Limit

Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.

func Every

func Every(interval time.Duration) Limit

Every converts a minimum time interval between events to a Limit.

type OptionCleanTicker

type OptionCleanTicker time.Duration

OptionCleanTicker check bucket limiter should be removed interval

type OptionCleanTimeout

type OptionCleanTimeout time.Duration

OptionCleanTimeout bucket limiter will be removed after last wait plus timeout

Jump to

Keyboard shortcuts

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