Documentation ¶
Index ¶
- type Limit
- type Limiter
- func (l *Limiter) Close()
- func (l *Limiter) Decr(ctx context.Context, r *Result) error
- func (l *Limiter) Inc(ctx context.Context, key string, limit *Limit) (*Result, error)
- func (l *Limiter) IncDynamic(ctx context.Context, key string, limit *Limit, maxlimit int64) (*Result, error)
- func (l *Limiter) IncN(ctx context.Context, key string, limit *Limit, n int) (*Result, error)
- func (l *Limiter) IncNDynamic(ctx context.Context, key string, limit *Limit, n int, maxlimit int64) (*Result, error)
- type Result
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limit ¶
type Limit struct { // InFlight is the max number of InFlight before rate // limit hits and the Limiter return not Allowed in the Result. InFlight int64 // Timeout should be above the expected max runtime for a request in flight. Timeout time.Duration }
Limit instructions.
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 ¶
// Miniredis server mr, _ := miniredis.Run() // Setup Redis ring := redis.NewRing(&redis.RingOptions{ Addrs: map[string]string{"server0": mr.Addr()}, }) // Control max allowed time for flightlimit to block ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) defer cancel() // Create a new flighlimiter l := flightlimit.NewLimiter(ring, true) defer l.Close() // Allow a maximum of 10 in-flight, with expected processing time of <= 30 Seconds limit := flightlimit.NewLimit(10, 30*time.Second) // Mark as in-flight r, _ := l.Inc(ctx, "foo:123", limit) if !r.Allowed { // // Here a 429 could be returned // return } // Processing fmt.Println(r.Remaining) // Land l.Decr(ctx, r)
Output: 9
func (*Limiter) IncDynamic ¶
func (l *Limiter) IncDynamic(ctx context.Context, key string, limit *Limit, maxlimit int64) (*Result, error)
IncDynamic is shorthand for IncNDynamic(ctx, key, limit, 1, maxlimit).
type Result ¶
type Result struct { // Limit is the limit that was used to obtain this result. Limit *Limit // Allowed reports whether event may happen at time now. Allowed bool // 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 in // flight and has 6 mid air, Remaining would be 4. Remaining int64 // contains filtered or unexported fields }
Result is the obj we containing the result.
Click to show internal directories.
Click to hide internal directories.