Files
bssapp/svc/middlewares/rate_limit.go
2026-02-11 17:46:22 +03:00

60 lines
1.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package middlewares
import (
"net"
"net/http"
"sync"
"time"
)
type rateEntry struct {
Count int
ExpiresAt time.Time
}
var (
rateMu sync.Mutex
rateDB = make(map[string]*rateEntry)
)
func RateLimit(keyFn func(*http.Request) string, limit int, window time.Duration) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
key := keyFn(r)
now := time.Now()
rateMu.Lock()
e, ok := rateDB[key]
if !ok || now.After(e.ExpiresAt) {
e = &rateEntry{
Count: 0,
ExpiresAt: now.Add(window),
}
rateDB[key] = e
}
e.Count++
if e.Count > limit {
rateMu.Unlock()
http.Error(w, "Too many requests", http.StatusTooManyRequests)
return
}
rateMu.Unlock()
next.ServeHTTP(w, r)
})
}
}
// helpers
func RateByIP(r *http.Request) string {
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
return "ip:" + ip
}
func RateByUser(r *http.Request) string {
return "user:" + r.URL.Path // id pathten okunabilir
}