redis_rate

package module
v8.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2019 License: BSD-2-Clause Imports: 3 Imported by: 6

README

Rate limiting for go-redis

Build Status GoDoc

This package is based on rwz/redis-gcra and implements GCRA (aka leaky bucket) for rate limiting based on Redis. The code requires Redis version 3.2 or newer since it relies on replicate_commands feature.

package redis_rate_test

import (
	"fmt"

	"github.com/go-redis/redis/v7"
	"github.com/go-redis/redis_rate/v8"
)

func ExampleNewLimiter() {
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})
	_ = rdb.FlushDB().Err()

	limiter := redis_rate.NewLimiter(rdb)
	res, err := limiter.Allow("project:123", redis_rate.PerSecond(10))
	if err != nil {
		panic(err)
	}
	fmt.Println(res.Allowed, res.Remaining)
	// Output: true 9
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limit

type Limit struct {
	Rate   int
	Period time.Duration
	Burst  int
}

func PerHour

func PerHour(rate int) *Limit

func PerMinute

func PerMinute(rate int) *Limit

func PerSecond

func PerSecond(rate int) *Limit

type Limiter

type Limiter struct {
	// contains filtered or unexported fields
}

Limiter controls how frequently events are allowed to happen.

func NewLimiter

func NewLimiter(rdb rediser) *Limiter

NewLimiter returns a new Limiter.

Example
rdb := redis.NewClient(&redis.Options{
	Addr: "localhost:6379",
})
_ = rdb.FlushDB().Err()

limiter := redis_rate.NewLimiter(rdb)
res, err := limiter.Allow("project:123", redis_rate.PerSecond(10))
if err != nil {
	panic(err)
}
fmt.Println(res.Allowed, res.Remaining)
Output:

true 9

func (*Limiter) Allow

func (l *Limiter) Allow(key string, limit *Limit) (*Result, error)

Allow is shorthand for AllowN(key, 1).

func (*Limiter) AllowN

func (l *Limiter) AllowN(key string, limit *Limit, n int) (*Result, error)

AllowN reports whether n events may happen at time now.

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 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
}

Jump to

Keyboard shortcuts

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