redisstore

package module
v0.0.0-...-30fbb4c Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

Go Rate Limiter

GoDoc GitHub Actions

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

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

func New(c *Config) (limiter.Store, error)

New uses a Redis instance to back a rate limiter that to limit the number of permitted events over an interval.

Example
ctx := context.Background()

store, err := redisstore.New(&redisstore.Config{
	Tokens:   15,
	Interval: time.Minute,
	RedisOptions: &redis.Options{
		Addr: "127.0.0.1:6379",
	},
})
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 NewWithClient

func NewWithClient(c *Config, client *redis.Client) (limiter.Store, error)

NewWithClient creates a new limiter using the given redis pool. Use this to customize lower-level details about the pool.

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

	// Redis client options
	RedisOptions *redis.Options
}

Config is used as input to New. It defines the behavior of the storage system.

Jump to

Keyboard shortcuts

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