Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { Interval time.Duration // the interval between each addition of one token // the capacity of the bucket Capacity int64 }
Config is the bucket configuration.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements the Token Bucket Algorithm. See https://en.wikipedia.org/wiki/Token_bucket.
func NewRateLimiter ¶
func NewRateLimiter(redis Redis, key string, config *Config) *RateLimiter
NewRateLimiter returns a new token-bucket rate limiter special for key in redis with the specified bucket configuration.
func (*RateLimiter) Config ¶
func (b *RateLimiter) Config() Config
Config returns the bucket configuration in a concurrency-safe way.
func (*RateLimiter) SetConfig ¶
func (b *RateLimiter) SetConfig(config *Config)
SetConfig updates the bucket configuration in a concurrency-safe way.
func (*RateLimiter) Take ¶
func (b *RateLimiter) Take(amount int64) (bool, error)
Take takes amount tokens from the bucket.
Example ¶
package main import ( "fmt" "strings" "time" "github.com/akhiljames/golimit" "github.com/go-redis/redis" ) type Redis struct { client *redis.Client } func (r *Redis) Eval(script string, keys []string, args ...interface{}) (interface{}, error) { return r.client.Eval(script, keys, args...).Result() } // EvalSha Optimize Lua script execution func (r *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) (interface{}, error, bool) { result, err := r.client.EvalSha(sha1, keys, args...).Result() noScript := err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") return result, err, noScript } func main() { tb := golimit.NewRateLimiter( &Redis{redis.NewClient(&redis.Options{ Addr: "localhost:6379", })}, "golimit:example", &golimit.Config{ Interval: 1 * time.Second / 2, Capacity: 5, }, ) if ok, err := tb.Take(1); ok { fmt.Println("PASS") } else { if err != nil { fmt.Println(err.Error()) } fmt.Println("DROP") } }
Output: PASS
type Redis ¶
type Redis interface { Eval(script string, keys []string, args ...interface{}) (interface{}, error) EvalSha(sha1 string, keys []string, args ...interface{}) (interface{}, error, bool) }
Redis interface
Click to show internal directories.
Click to hide internal directories.