Skip to content

Commit

Permalink
serverapi:add rate limit for api ref tikv#4207
Browse files Browse the repository at this point in the history
Signed-off-by: qidi1 <1083369179@qq.com>
  • Loading branch information
qidi1 committed Oct 25, 2021
1 parent fab46b3 commit ffb8257
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/apiutil/serverapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/url"
"strings"

"github.com/juju/ratelimit"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/server"
Expand All @@ -39,6 +40,11 @@ const (
errRedirectToNotLeader = "redirect to not leader"
)

// the api which will be limit by api limit.
var limitAPIPrefix = []string{
"pd/api/v1/hotspot/regions/history",
}

type runtimeServiceValidator struct {
s *server.Server
group server.ServiceGroup
Expand Down Expand Up @@ -123,6 +129,28 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http
NewCustomReverseProxies(client, urls).ServeHTTP(w, r)
}

type apiLimit struct {
bucket *ratelimit.Bucket
}

// NewAPILimit returns apiLimit which limit http access rate.
func NewAPILimit(capacity int64, ratePerSec float64) *apiLimit {
return &apiLimit{
bucket: ratelimit.NewBucketWithRate(ratePerSec, capacity),
}
}

func (a *apiLimit) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
path := r.URL.Path
for _, apiPrefix := range limitAPIPrefix {
if strings.HasPrefix(path, apiPrefix) {
a.bucket.Wait(1)
break
}
}
next(w, r)
}

type customReverseProxies struct {
urls []url.URL
client *http.Client
Expand Down
2 changes: 2 additions & 0 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func NewHandler(ctx context.Context, svr *server.Server) (http.Handler, server.S
router.PathPrefix(apiPrefix).Handler(negroni.New(
serverapi.NewRuntimeServiceValidator(svr, group),
serverapi.NewRedirector(svr),
serverapi.NewAPILimit(svr.GetConfig().PDServerCfg.APIBucketCapacity,
svr.GetConfig().PDServerCfg.APIBucketRate),
negroni.Wrap(r)),
)

Expand Down

0 comments on commit ffb8257

Please sign in to comment.