Rate Limit Middleware
The RateLimit function in the middlewares package provides rate limiting functionality for your Twix router. This middleware helps control the number of requests a client can make to your server within a specified time window.
RateLimitConfig
The RateLimitConfig struct holds the configuration for the rate limit middleware.
type RateLimitConfig struct {
RequestLimit int
WindowSize time.Duration
}
RequestLimit: The maximum number of requests allowed within theWindowSize.WindowSize: The duration within which theRequestLimitapplies.
RateLimit
The RateLimit function creates a middleware handler that enforces rate limiting based on the specified configuration.
func RateLimit(config RateLimitConfig) func(http.Handler) http.Handler
Example
package main
import (
"net/http"
"time"
"github.com/farhanmobashir/twix"
"github.com/farhanmobashir/twix/middlewares"
)
func main() {
router := twix.New()
// Define rate limit configuration
rateLimitConfig := middlewares.RateLimitConfig{
RequestLimit: 100,
WindowSize: 1 * time.Minute,
}
// Apply rate limit middleware to the router
router.Use(middlewares.RateLimit(rateLimitConfig))
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})
http.ListenAndServe(":8080", router)
}
In this example, the RateLimit middleware is configured to allow up to 100 requests per minute per IP address. Requests exceeding this limit will receive a 429 Too Many Requests response.
Rate Limiting Behavior
The rate limiting middleware works as follows:
- Extract IP Address: It extracts the client's IP address from the request.
- Check Rate Limit Data: It checks if the IP address has existing rate limit data.
- Update or Set Data:
- If the data is within the specified
WindowSizeand the request count exceeds the limit, it responds with429 Too Many Requests. - If the data is within the
WindowSizebut the request count is below the limit, it increments the count. - If the
WindowSizehas passed, it resets the count and updates the timestamp.
- If the data is within the specified
- Store Data: It stores the updated rate limit data for the IP address.
Usage
You can apply the RateLimit middleware to your Twix router by calling the Use method with the rate limit configuration:
router.Use(middlewares.RateLimit(rateLimitConfig))
This will enforce the rate limit rules for incoming requests based on the client's IP address.