mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 20:52:33 +02:00
rate limiter check host instead of full remote address
This commit is contained in:
parent
8df28628ec
commit
64584c73b2
1 changed files with 10 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -64,10 +65,17 @@ func NewRateLimiter(optsRaw OptionsRaw) (*Middleware, E.Error) {
|
||||||
func (rl *rateLimiter) limit(next http.HandlerFunc, w ResponseWriter, r *Request) {
|
func (rl *rateLimiter) limit(next http.HandlerFunc, w ResponseWriter, r *Request) {
|
||||||
rl.mu.Lock()
|
rl.mu.Lock()
|
||||||
|
|
||||||
limiter, ok := rl.requestMap[r.RemoteAddr]
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
|
if err != nil {
|
||||||
|
rl.m.Debug().Msgf("unable to parse remote address %s", r.RemoteAddr)
|
||||||
|
http.Error(w, "Internal error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
limiter, ok := rl.requestMap[host]
|
||||||
if !ok {
|
if !ok {
|
||||||
limiter = rl.newLimiter()
|
limiter = rl.newLimiter()
|
||||||
rl.requestMap[r.RemoteAddr] = limiter
|
rl.requestMap[host] = limiter
|
||||||
}
|
}
|
||||||
|
|
||||||
rl.mu.Unlock()
|
rl.mu.Unlock()
|
||||||
|
|
Loading…
Add table
Reference in a new issue