rlimiter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2019 License: MIT Imports: 5 Imported by: 0

README

rlimiter

Simple redis rate limit

example

// applied only to endpoint login and POST
grbinder.BindVerb(group.Group("/login", rlimiter.GinRateLimit(
  rlimiter.NewIPRateLimiter(
    &rlimiter.Rate{Window: 10 * time.Second, Limit: 10},
    "api.login"),
  []string{"POST"})),
  &loginHandler{})

// Applied to any request method
limiter := rlimiter.GinRateLimit(
  rlimiter.NewIPRateLimiter(
    &rlimiter.Rate{Window: 1 * time.Minute, Limit: 10},
    "api.hardwork"),
  []string{})

r := gin.Default()
r.GET("/hardwork", limiter, func(c *gin.Context) {
})
r.POST("/hardwork", limiter, func(c *gin.Context) {
})
r.PUT("/hardwork", limiter, func(c *gin.Context) {
})

Documentation

Overview

Package rlimiter is for rate limit on heavy endpoints

import "github.com/vanng822/rlimiter" grbinder.BindVerb(group.Group("/login", rlimiter.GinRateLimit(

rlimiter.NewIPRateLimiter(
  &rlimiter.Rate{Window: 10 * time.Second, Limit: 10},
  "api.login"),
[]string{"POST"})),
&loginHandler{})

Index

Constants

This section is empty.

Variables

View Source
var (

	// Client for connecting to redis database
	Client = redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		DB:   0,
	})
)

Functions

func GinRateLimit

func GinRateLimit(limiter GinRateLimiter, methods []string) gin.HandlerFunc

Types

type GinRateLimiter

type GinRateLimiter interface {
	RateLimiter
	Key(c *gin.Context) string
}

func NewIPRateLimiter

func NewIPRateLimiter(rate *Rate, prefix string) GinRateLimiter

func NewParamRateLimiter

func NewParamRateLimiter(rate *Rate, prefix, paramName string) GinRateLimiter

func NewPathRateLimiter

func NewPathRateLimiter(rate *Rate, prefix string) GinRateLimiter

type Rate

type Rate struct {
	Window time.Duration
	Limit  int64
}

Rate define window as describe in https://redis.io/commands/incr#pattern-rate-limiter-2

type RateLimiter

type RateLimiter interface {
	IncrementUsage(key string) (bool, error)
}

func NewRateLimiter

func NewRateLimiter(rate *Rate, prefix string) RateLimiter

Jump to

Keyboard shortcuts

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