Skip to content

Commit

Permalink
close tikv#4500: add QPS rate limiter
Browse files Browse the repository at this point in the history
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
  • Loading branch information
CabinfeverB committed Dec 27, 2021
1 parent 8334bbe commit a1c1ecc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
7 changes: 4 additions & 3 deletions server/self_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"golang.org/x/time/rate"
)

type APIRateLimiter struct {
// serviceRateLimiter is used to rimit service request rate
type serviceRateLimiter struct {
mu sync.RWMutex

enableQPSLimit bool
Expand All @@ -33,7 +34,7 @@ type APIRateLimiter struct {

// QPSAllow firstly check component token bucket and then check total token bucket
// if component rate limiter doesn't allow, it won't ask total limiter
func (rl *APIRateLimiter) QPSAllow(componentName string) bool {
func (rl *serviceRateLimiter) QPSAllow(componentName string) bool {
if !rl.enableQPSLimit {
return true
}
Expand All @@ -53,7 +54,7 @@ func (rl *APIRateLimiter) QPSAllow(componentName string) bool {
}

// Allow currentlt only supports QPS rate limit
func (rl *APIRateLimiter) Allow(componentName string) bool {
func (rl *serviceRateLimiter) Allow(componentName string) bool {
rl.mu.RLock()
defer rl.mu.RUnlock()
return rl.QPSAllow(componentName)
Expand Down
22 changes: 11 additions & 11 deletions server/self_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ import (
var _ = Suite(&testSelfProtectHandler{})

type testSelfProtectHandler struct {
rateLimiterOnlyTotal *APIRateLimiter
rateLimiterDisabled *APIRateLimiter
rateLimiterZeroBucket *APIRateLimiter
rateLimiterComopnent *APIRateLimiter
rateLimiterNoComopnentConfig *APIRateLimiter
rateLimiterOnlyTotal *serviceRateLimiter
rateLimiterDisabled *serviceRateLimiter
rateLimiterZeroBucket *serviceRateLimiter
rateLimiterComopnent *serviceRateLimiter
rateLimiterNoComopnentConfig *serviceRateLimiter
}

func (s *testSelfProtectHandler) SetUpSuite(c *C) {
s.rateLimiterOnlyTotal = &APIRateLimiter{
s.rateLimiterOnlyTotal = &serviceRateLimiter{
enableQPSLimit: true,
totalQPSRateLimiter: rate.NewLimiter(100, 100),
enableComponentQPSLimit: false,
}
s.rateLimiterDisabled = &APIRateLimiter{
s.rateLimiterDisabled = &serviceRateLimiter{
enableQPSLimit: false,
}
s.rateLimiterZeroBucket = &APIRateLimiter{
s.rateLimiterZeroBucket = &serviceRateLimiter{
enableQPSLimit: true,
totalQPSRateLimiter: rate.NewLimiter(0, 0),
}
s.rateLimiterComopnent = &APIRateLimiter{
s.rateLimiterComopnent = &serviceRateLimiter{
enableQPSLimit: true,
totalQPSRateLimiter: rate.NewLimiter(100, 100),
enableComponentQPSLimit: true,
Expand All @@ -54,7 +54,7 @@ func (s *testSelfProtectHandler) SetUpSuite(c *C) {
s.rateLimiterComopnent.componentQPSRateLimiter["pdctl"] = rate.NewLimiter(100, 100)
s.rateLimiterComopnent.componentQPSRateLimiter["anonymous"] = rate.NewLimiter(100, 100)

s.rateLimiterNoComopnentConfig = &APIRateLimiter{
s.rateLimiterNoComopnentConfig = &serviceRateLimiter{
enableQPSLimit: true,
totalQPSRateLimiter: rate.NewLimiter(200, 200),
enableComponentQPSLimit: true,
Expand All @@ -63,7 +63,7 @@ func (s *testSelfProtectHandler) SetUpSuite(c *C) {
s.rateLimiterNoComopnentConfig.componentQPSRateLimiter["pdctl"] = rate.NewLimiter(10, 10)
}

func CountRateLimiterHandleResult(handler *APIRateLimiter, component string, successCount *int,
func CountRateLimiterHandleResult(handler *serviceRateLimiter, component string, successCount *int,
failedCount *int, lock *sync.Mutex, wg *sync.WaitGroup) {
result := handler.Allow(component)
lock.Lock()
Expand Down

0 comments on commit a1c1ecc

Please sign in to comment.