redisstore

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2020 License: Apache-2.0 Imports: 7 Imported by: 4

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.

For an instrumented client, see the opencensus branch.

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,
	Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", "127.0.0,1:6379",
			redis.DialPassword("my-password"))
	},
})
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 NewWithPool

func NewWithPool(c *Config, pool *redis.Pool) (limiter.Store, error)

NewWithPool 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

	// Dial is the function to use as the dialer. This is ignored when used with
	// NewWithPool.
	Dial func() (redis.Conn, error)
}

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