redisstore

package module
v0.0.0-...-155a061 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

Go Rate Limiter

This package provides a rate limiting interface in Go (Golang), using Redis. See sethvargo/go-limiter for more information.

For an instrumented client, see the sethvargo/go-redisstore-opencensus.

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

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

func New(c *Config, pool *redis.Client) (*Store, error)

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
}

func (*Store) Burst

func (s *Store) Burst(ctx context.Context, key string, tokens uint64) (retErr error)

Burst adds the given tokens to the key's current token count.

func (*Store) Close

func (s *Store) Close(_ context.Context) error

Close stops the store and releases any open connections.

func (*Store) Get

func (s *Store) Get(ctx context.Context, key string) (limit, remaining uint64, retErr error)

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.

Jump to

Keyboard shortcuts

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