fiber/middleware/limiter
M. Efe Çetin 691d2e6ad5
🗑️ deprecate: go 1.14 & go 1.15 support deprecation (#2172)
* 🗑️ deprecate: go 1.14 & go 1.15 support deprecation
https://github.com/valyala/fasthttp/pull/1379

* fix tests
2022-10-26 11:51:50 +02:00
..
README.md limiter middleware new options: SkipFailedRequests, SkipSuccessfulRequests (#1542) 2021-09-28 10:10:29 +02:00
config.go Fix comment relating to deprecated field (#1790) 2022-02-20 19:01:16 +03:00
limiter.go feat: add sliding window as option for limiter middleware (#1580) 2021-10-18 09:13:53 +02:00
limiter_fixed.go Improve memory storage (#2162) 2022-10-19 16:22:42 +02:00
limiter_sliding.go Improve memory storage (#2162) 2022-10-19 16:22:42 +02:00
limiter_test.go 🗑️ deprecate: go 1.14 & go 1.15 support deprecation (#2172) 2022-10-26 11:51:50 +02:00
manager.go ♻️ Tidy up the codebase (#1613) 2021-11-05 08:00:03 +01:00
manager_msgp.go feat: add sliding window as option for limiter middleware (#1580) 2021-10-18 09:13:53 +02:00

README.md

Limiter Middleware

Limiter middleware for Fiber that is used to limit repeat requests to public APIs and/or endpoints such as password reset. It is also useful for API clients, web crawling, or other tasks that need to be throttled.

NOTE: This middleware uses our Storage package to support various databases through a single interface. The default configuration for this middleware saves data to memory, see the examples below for other databases.

NOTE: this module does not share state with other processes/servers by default.

Table of Contents

Signatures

func New(config ...Config) fiber.Handler

Examples

First import the middleware from Fiber,

import (
  "github.com/gofiber/fiber/v2"
  "github.com/gofiber/fiber/v2/middleware/limiter"
)

Then create a Fiber app with app := fiber.New().

Default Config

// Default middleware config
app.Use(limiter.New())

Custom Config

// Or extend your config for customization
app.Use(limiter.New(limiter.Config{
	Next: func(c *fiber.Ctx) bool {
		return c.IP() == "127.0.0.1"
	},
	Max:          20,
	Expiration:     30 * time.Second,
	KeyGenerator: func(c *fiber.Ctx) string{
  		return "key"
	}
	LimitReached: func(c *fiber.Ctx) error {
		return c.SendFile("./toofast.html")
	},
	Storage: myCustomStore{}
}))

Custom Storage/Database

You can use any storage from our storage package.

storage := sqlite3.New() // From github.com/gofiber/storage/sqlite3
app.Use(limiter.New(limiter.Config{
	Storage: storage,
}))

Config

// Config defines the config for middleware.
type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// Max number of recent connections during `Duration` seconds before sending a 429 response
	//
	// Default: 5
	Max int

	// KeyGenerator allows you to generate custom keys, by default c.IP() is used
	//
	// Default: func(c *fiber.Ctx) string {
	//   return c.IP()
	// }
	KeyGenerator func(*fiber.Ctx) string

	// Expiration is the time on how long to keep records of requests in memory
	//
	// Default: 1 * time.Minute
	Expiration time.Duration

	// LimitReached is called when a request hits the limit
	//
	// Default: func(c *fiber.Ctx) error {
	//   return c.SendStatus(fiber.StatusTooManyRequests)
	// }
	LimitReached fiber.Handler

	// When set to true, requests with StatusCode >= 400 won't be counted.
	//
	// Default: false
	SkipFailedRequests bool

	// When set to true, requests with StatusCode < 400 won't be counted.
	//
	// Default: false
	SkipSuccessfulRequests bool

	// Store is used to store the state of the middleware
	//
	// Default: an in memory store for this process only
	Storage fiber.Storage
}

A custom store can be used if it implements the Storage interface - more details and an example can be found in store.go.

Default Config

var ConfigDefault = Config{
	Max:        5,
	Expiration: 1 * time.Minute,
	KeyGenerator: func(c *fiber.Ctx) string {
		return c.IP()
	},
	LimitReached: func(c *fiber.Ctx) error {
		return c.SendStatus(fiber.StatusTooManyRequests)
	},
	SkipFailedRequests: false,
	SkipSuccessfulRequests: false,
}