ginlimiter

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2021 License: Apache-2.0 Imports: 6 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

gin-limiter

This is to add a middleware to Gin framework to support rate limiting. It wraps Juju's ratelimit implemetation as a Gin middleware

Usage

lm := limiter.NewRateLimiter(time.Minute, 10, func(ctx *gin.Context) (string, error) {
		key := ctx.Request.Header.Get("X-API-KEY")
		if key != "" {
			return key, nil
		}
		return "", errors.New("API key is missing")
	})

r.GET("/ping", lm.Middleware(), func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

This means the URI "/ping" only allows 10 requests per minutes per X-API-KEY. The key can be not only header but also with your own rules. You can decide what you try to limit with by returning the key.For example, it's also ok to use cookie or client ip as the key.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateKeyFunc

type RateKeyFunc func(ctx *gin.Context) (string, error)

type RateLimiterMiddleware

type RateLimiterMiddleware struct {
	// contains filtered or unexported fields
}

func NewRateLimiter

func NewRateLimiter(interval time.Duration, capacity int64, keyGen RateKeyFunc) *RateLimiterMiddleware

func (*RateLimiterMiddleware) Middleware

func (r *RateLimiterMiddleware) Middleware() gin.HandlerFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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