Documentation ¶
Overview ¶
Package redisstore defines a redis-backed storage system for limiting.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New uses a Redis instance to back a rate limiter that to limit the number of permitted events over an interval.
Example ¶
package main import ( "log" "net" "time" "github.com/sethvargo/go-limiter/redisstore" ) func main() { store, err := redisstore.New(&redisstore.Config{ Tokens: 15, Interval: time.Minute, InitialPoolSize: 32, MaxPoolSize: 128, AuthPassword: "my-password", DialFunc: func() (net.Conn, error) { conn, err := net.Dial("tcp", "127.0.0.1:6379") if err != nil { return nil, err } return conn, nil }, }) if err != nil { log.Fatal(err) } defer store.Close() limit, remaining, reset, ok := store.Take("my-key") _, _, _, _ = limit, remaining, reset, ok }
Output:
Types ¶
type Config ¶
type Config struct { // Tokens is the number of tokens to allow per interval. The default value is // 1. Tokens uint64 // Interval is the time interval upon which to enforce rate limiting. The // default value is 1 second. Interval time.Duration // TTL is the amount of time a key should exist without changes before // purging. The default is 10 x interval. TTL uint64 // InitialPoolSize and MaxPoolSize determine the initial and maximum number of // pool connections. The default values are 5 and 100 respectively. InitialPoolSize uint64 MaxPoolSize uint64 // DialFunc is a function that creates a connection to the Redis // server. DialFunc func() (net.Conn, error) // AuthUsername and AuthPassword are optional authentication information. AuthUsername string AuthPassword string // FailureMode indicates how the system should fail if it cannot connect to // the redis backend. FailureMode FailureMode }
Config is used as input to New. It defines the behavior of the storage system.
type FailureMode ¶ added in v0.3.0
type FailureMode int
FailureMode specifies the failure mode.
const ( FailClosed FailureMode FailOpen )
Click to show internal directories.
Click to hide internal directories.