mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 08:16:39 +00:00
add rate limit
This commit is contained in:
@@ -3,7 +3,6 @@ package server
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand/v2"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
@@ -179,7 +178,6 @@ func (s *GRPCServer) Sync(req *proto.EncryptedMessage, srv proto.ManagementServi
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !s.syncLimiter.Allow() {
|
if !s.syncLimiter.Allow() {
|
||||||
time.Sleep(time.Second + (time.Millisecond * time.Duration(rand.IntN(20)*100)))
|
|
||||||
log.Warnf("sync rate limit exceeded for peer %s", req.WgPubKey)
|
log.Warnf("sync rate limit exceeded for peer %s", req.WgPubKey)
|
||||||
return status.Errorf(codes.Internal, "temp rate limit reached")
|
return status.Errorf(codes.Internal, "temp rate limit reached")
|
||||||
}
|
}
|
||||||
@@ -492,7 +490,7 @@ func (s *GRPCServer) Login(ctx context.Context, req *proto.EncryptedMessage) (*p
|
|||||||
s.loginLimiterStore.Store(req.WgPubKey, newLimiter)
|
s.loginLimiterStore.Store(req.WgPubKey, newLimiter)
|
||||||
|
|
||||||
if !newLimiter.Allow() {
|
if !newLimiter.Allow() {
|
||||||
time.Sleep(time.Second + (time.Millisecond * time.Duration(rand.IntN(20)*100)))
|
//time.Sleep(time.Second + (time.Millisecond * time.Duration(rand.IntN(20)*100)))
|
||||||
log.WithContext(ctx).Warnf("rate limit exceeded for peer %s", req.WgPubKey)
|
log.WithContext(ctx).Warnf("rate limit exceeded for peer %s", req.WgPubKey)
|
||||||
return nil, fmt.Errorf("temp rate limit reached (new peer limit)")
|
return nil, fmt.Errorf("temp rate limit reached (new peer limit)")
|
||||||
}
|
}
|
||||||
@@ -500,7 +498,7 @@ func (s *GRPCServer) Login(ctx context.Context, req *proto.EncryptedMessage) (*p
|
|||||||
// Use existing limiter for this peer
|
// Use existing limiter for this peer
|
||||||
limiter := limiterIface.(*rate.Limiter)
|
limiter := limiterIface.(*rate.Limiter)
|
||||||
if !limiter.Allow() {
|
if !limiter.Allow() {
|
||||||
time.Sleep(time.Second + (time.Millisecond * time.Duration(rand.IntN(20)*100)))
|
//time.Sleep(time.Second + (time.Millisecond * time.Duration(rand.IntN(20)*100)))
|
||||||
log.WithContext(ctx).Warnf("rate limit exceeded for peer %s", req.WgPubKey)
|
log.WithContext(ctx).Warnf("rate limit exceeded for peer %s", req.WgPubKey)
|
||||||
return nil, fmt.Errorf("temp rate limit reached (peer limit)")
|
return nil, fmt.Errorf("temp rate limit reached (peer limit)")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user