load test cache miss

This commit is contained in:
2026-09-11 00:32:01 -05:00
parent 768f44f669
commit de7a2d299f
7 changed files with 247984 additions and 181314 deletions
+29 -2
View File
@@ -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" {