Documentation ¶
Index ¶
- type Bucket
- type KeyPrefix
- type Limit
- type Limiter
- func (l Limiter) Allow(ctx context.Context, key string, limit Limit) (*Result, error)
- func (l Limiter) AllowAtMost(ctx context.Context, key string, limit Limit, n int) (*Result, error)
- func (l Limiter) AllowN(ctx context.Context, key string, limit Limit, n int) (*Result, error)
- func (l *Limiter) Reset(ctx context.Context, key string) error
- type Optional
- type RateLimiter
- type Result
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bucket ¶
type Bucket[T any] struct { // contains filtered or unexported fields }
Example ¶
package main import ( "fmt" "time" "github.com/holdno/go-instrumentation/ratelimit" ) func main() { b := ratelimit.NewLeakyBucket[int](1, time.Second, nil) length := 10 go func() { for i := 0; i < length; i++ { b.Set(i) // do your logic } b.Done() }() for { taskIndex, ok := b.Get() if !ok { fmt.Println("done") break } fmt.Println(taskIndex) } }
Output: 0 1 2 3 4 5 6 7 8 9 done
func NewLeakyBucket ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter controls how frequently events are allowed to happen.
func NewLimiter ¶
NewLimiter returns a new Limiter.
Example ¶
ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", }) _ = rdb.FlushDB(ctx).Err() limiter := ratelimit.NewLimiter(rdb, ratelimit.KeyPrefix("yourprefix_")) res, err := limiter.Allow(ctx, "project:123", ratelimit.Limit{ Rate: 1, Burst: 1, Period: time.Second * 10, }) if err != nil { panic(err) } time.Sleep(time.Second * 10) res, err = limiter.Allow(ctx, "project:123", ratelimit.Limit{ Rate: 10, Burst: 10, Period: time.Second * 10, }) if err != nil { panic(err) } fmt.Println("allowed", res.Allowed, "remaining", res.Remaining)
Output: allowed 1 remaining 9
func (Limiter) AllowAtMost ¶
func (l Limiter) AllowAtMost( ctx context.Context, key string, limit Limit, n int, ) (*Result, error)
AllowAtMost reports whether at most n events may happen at time now. It returns number of allowed events that is less than or equal to n.
type RateLimiter ¶
func New ¶
Example ¶
ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", }) _ = rdb.FlushDB(ctx).Err() limit := ratelimit.New("project:123", 10, time.Second*10, ratelimit.NewLimiter(rdb, ratelimit.KeyPrefix("yourprefix_"))) fmt.Println(limit.AllowN(2)) rdb.Close() fmt.Println(limit.AllowN(10)) fmt.Println(limit.AllowN(8))
Output: true redis: client is closed downgrade false redis: client is closed downgrade true
type Result ¶
type Result struct { // Limit is the limit that was used to obtain this result. Limit Limit // Allowed is the number of events that may happen at time now. Allowed int // Remaining is the maximum number of requests that could be // permitted instantaneously for this key given the current // state. For example, if a rate limiter allows 10 requests per // second and has already received 6 requests for this key this // second, Remaining would be 4. Remaining int // RetryAfter is the time until the next request will be permitted. // It should be -1 unless the rate limit has been exceeded. RetryAfter time.Duration // ResetAfter is the time until the RateLimiter returns to its // initial state for a given key. For example, if a rate limiter // manages requests per second and received one request 200ms ago, // Reset would return 800ms. You can also think of this as the time // until Limit and Remaining will be equal. ResetAfter time.Duration }
Click to show internal directories.
Click to hide internal directories.