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.
Click to show internal directories.
Click to hide internal directories.