mirror of
https://github.com/nprasad2077/NBA_Go.git
synced 2026-09-22 14:05:13 +00:00
load test cache miss
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -9,10 +12,34 @@ import (
|
||||
)
|
||||
|
||||
func RateLimiter() fiber.Handler {
|
||||
benchmarkKey := strings.TrimSpace(os.Getenv("BENCHMARK_KEY"))
|
||||
|
||||
maxRequests := 20
|
||||
if envMax := os.Getenv("RATE_LIMIT_MAX"); envMax != "" {
|
||||
if val, err := strconv.Atoi(envMax); err == nil && val > 0 {
|
||||
maxRequests = val
|
||||
}
|
||||
}
|
||||
|
||||
expiration := 1 * time.Minute
|
||||
if envExp := os.Getenv("RATE_LIMIT_EXPIRATION_SECONDS"); envExp != "" {
|
||||
if val, err := strconv.Atoi(envExp); err == nil && val > 0 {
|
||||
expiration = time.Duration(val) * time.Second
|
||||
}
|
||||
}
|
||||
|
||||
return limiter.New(limiter.Config{
|
||||
Max: 20,
|
||||
Expiration: 1 * time.Minute,
|
||||
Max: maxRequests,
|
||||
Expiration: expiration,
|
||||
Next: func(c *fiber.Ctx) bool {
|
||||
// Bypass rate limiting when a valid internal benchmark key is supplied
|
||||
if benchmarkKey != "" {
|
||||
clientKey := c.Get("X-Benchmark-Key")
|
||||
if clientKey != "" && subtle.ConstantTimeCompare([]byte(clientKey), []byte(benchmarkKey)) == 1 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Skip rate limiting for internal services and infra endpoints
|
||||
ip := c.IP()
|
||||
if strings.HasPrefix(ip, "10.") || strings.HasPrefix(ip, "172.") || ip == "127.0.0.1" {
|
||||
|
||||
Reference in New Issue
Block a user