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).
type Limit ¶
Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.
type OptionCleanTicker ¶
OptionCleanTicker check bucket limiter should be removed interval
type OptionCleanTimeout ¶
OptionCleanTimeout bucket limiter will be removed after last wait plus timeout
Click to show internal directories.
Click to hide internal directories.