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: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
package main

import (
	"context"
	"log"
	"time"

	"github.com/go-redis/redis/v8"
	"github.com/zavitax/go-redisstore"
)

func main() {
	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
}

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
⌂ 换网址