ratelimite

package
v0.0.0-...-b628505 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrTimeOut = errors.New("restrictor/ratelimit: 超时")

Functions

func NewRateLimit

func NewRateLimit(bucket *ratelimit.Bucket) (restrictor.AllowFunc, restrictor.WaitFunc)
Example
// ratelimit github.com/juju/ratelimit
bucket := ratelimit.NewBucket(time.Second/2, 4)

af, wf := NewRateLimit(bucket)
// af.Allow()bool: 默认取1个token
// af.Allow() == af.AllowN(time.Now(), 1)
af.Allow()

// af.AllowN(ctx,n)bool: 可以取N个token
af.AllowN(time.Now(), 5)

// wf.Wait(ctx) err: 等待ctx超时,默认取1个token
// wf.Wait(ctx) == wf.WaitN(ctx, 1)
_ = wf.Wait(context.TODO())

// wf.WaitN(ctx, n) err: 等待ctx超时,可以取N个token
_ = wf.WaitN(context.TODO(), 5)
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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