Documentation
¶
Overview ¶
Package redisstore defines a redis-backed storage system for limiting.
Index ¶
- type Config
- type Store
- func (s *Store) Burst(ctx context.Context, key string, tokens uint64) (retErr error)
- func (s *Store) Close(_ context.Context) error
- func (s *Store) Get(ctx context.Context, key string) (limit, remaining uint64, retErr error)
- func (s *Store) Set(ctx context.Context, key string, tokens uint64, interval time.Duration) (retErr error)
- func (s *Store) Take(ctx context.Context, key string) (limit, remaining, next uint64, ok bool, retErr error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
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
}
Config is used as input to New. It defines the behavior of the storage system.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func New ¶
New creates a new limiter backed by the given Redis client.
Example ¶
package main
import (
"context"
"log"
"time"
redisstore "github.com/bobTheBuilder7/go-redisstore"
"github.com/redis/go-redis/v9"
)
func main() {
ctx := context.Background()
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
defer client.Close()
store, err := redisstore.New(&redisstore.Config{
Tokens: 15,
Interval: time.Minute,
}, client)
if err != nil {
log.Fatal(err)
}
defer store.Close(ctx)
limit, remaining, reset, ok, err := store.Take(ctx, "my-key")
if err != nil {
log.Fatal(err)
}
_, _, _, _ = limit, remaining, reset, ok
}
Output:
func (*Store) Get ¶
Get gets the current limit and remaining tokens for the key without consuming any tokens.
func (*Store) Set ¶
func (s *Store) Set(ctx context.Context, key string, tokens uint64, interval time.Duration) (retErr error)
Set sets the key's limit to the provided value and interval.
func (*Store) Take ¶
func (s *Store) Take(ctx context.Context, key string) (limit, remaining, next uint64, ok bool, retErr error)
Take attempts to remove a token from the named key. If the take is successful, it returns true, otherwise false. It also returns the configured limit, remaining tokens, and reset time.
Click to show internal directories.
Click to hide internal directories.