github.com/gofiber/fiber/v2@v2.47.0/middleware/limiter/config.go (about)

     1  package limiter
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	"github.com/gofiber/fiber/v2"
     8  )
     9  
    10  // Config defines the config for middleware.
    11  type Config struct {
    12  	// Next defines a function to skip this middleware when returned true.
    13  	//
    14  	// Optional. Default: nil
    15  	Next func(c *fiber.Ctx) bool
    16  
    17  	// Max number of recent connections during `Expiration` seconds before sending a 429 response
    18  	//
    19  	// Default: 5
    20  	Max int
    21  
    22  	// KeyGenerator allows you to generate custom keys, by default c.IP() is used
    23  	//
    24  	// Default: func(c *fiber.Ctx) string {
    25  	//   return c.IP()
    26  	// }
    27  	KeyGenerator func(*fiber.Ctx) string
    28  
    29  	// Expiration is the time on how long to keep records of requests in memory
    30  	//
    31  	// Default: 1 * time.Minute
    32  	Expiration time.Duration
    33  
    34  	// LimitReached is called when a request hits the limit
    35  	//
    36  	// Default: func(c *fiber.Ctx) error {
    37  	//   return c.SendStatus(fiber.StatusTooManyRequests)
    38  	// }
    39  	LimitReached fiber.Handler
    40  
    41  	// When set to true, requests with StatusCode >= 400 won't be counted.
    42  	//
    43  	// Default: false
    44  	SkipFailedRequests bool
    45  
    46  	// When set to true, requests with StatusCode < 400 won't be counted.
    47  	//
    48  	// Default: false
    49  	SkipSuccessfulRequests bool
    50  
    51  	// Store is used to store the state of the middleware
    52  	//
    53  	// Default: an in memory store for this process only
    54  	Storage fiber.Storage
    55  
    56  	// LimiterMiddleware is the struct that implements a limiter middleware.
    57  	//
    58  	// Default: a new Fixed Window Rate Limiter
    59  	LimiterMiddleware LimiterHandler
    60  
    61  	// Deprecated: Use Expiration instead
    62  	Duration time.Duration
    63  
    64  	// Deprecated: Use Storage instead
    65  	Store fiber.Storage
    66  
    67  	// Deprecated: Use KeyGenerator instead
    68  	Key func(*fiber.Ctx) string
    69  }
    70  
    71  // ConfigDefault is the default config
    72  var ConfigDefault = Config{
    73  	Max:        5,
    74  	Expiration: 1 * time.Minute,
    75  	KeyGenerator: func(c *fiber.Ctx) string {
    76  		return c.IP()
    77  	},
    78  	LimitReached: func(c *fiber.Ctx) error {
    79  		return c.SendStatus(fiber.StatusTooManyRequests)
    80  	},
    81  	SkipFailedRequests:     false,
    82  	SkipSuccessfulRequests: false,
    83  	LimiterMiddleware:      FixedWindow{},
    84  }
    85  
    86  // Helper function to set default values
    87  func configDefault(config ...Config) Config {
    88  	// Return default config if nothing provided
    89  	if len(config) < 1 {
    90  		return ConfigDefault
    91  	}
    92  
    93  	// Override default config
    94  	cfg := config[0]
    95  
    96  	// Set default values
    97  	if int(cfg.Duration.Seconds()) > 0 {
    98  		log.Printf("[Warning] - [LIMITER] Duration is deprecated, please use Expiration\n")
    99  		cfg.Expiration = cfg.Duration
   100  	}
   101  	if cfg.Key != nil {
   102  		log.Printf("[Warning] - [LIMITER] Key is deprecated, please us KeyGenerator\n")
   103  		cfg.KeyGenerator = cfg.Key
   104  	}
   105  	if cfg.Store != nil {
   106  		log.Printf("[Warning] - [LIMITER] Store is deprecated, please use Storage\n")
   107  		cfg.Storage = cfg.Store
   108  	}
   109  	if cfg.Next == nil {
   110  		cfg.Next = ConfigDefault.Next
   111  	}
   112  	if cfg.Max <= 0 {
   113  		cfg.Max = ConfigDefault.Max
   114  	}
   115  	if int(cfg.Expiration.Seconds()) <= 0 {
   116  		cfg.Expiration = ConfigDefault.Expiration
   117  	}
   118  	if cfg.KeyGenerator == nil {
   119  		cfg.KeyGenerator = ConfigDefault.KeyGenerator
   120  	}
   121  	if cfg.LimitReached == nil {
   122  		cfg.LimitReached = ConfigDefault.LimitReached
   123  	}
   124  	if cfg.LimiterMiddleware == nil {
   125  		cfg.LimiterMiddleware = ConfigDefault.LimiterMiddleware
   126  	}
   127  	return cfg
   128  }