用了beego.InsertFilter("/",beego.BeforeRouter,LimitHandler())没用 func LimitHandler() beego.FilterFunc { lmt := tollbooth.NewLimiter(1, &limiter.ExpirableOptions{DefaultExpirationTTL: time.Hour}) lmt.SetMessage("You have reached the maximum number of requests. Please try again later") //Limit only GET and POST requests. lmt.SetMethods([]string{"GET", "POST"}) lmt.SetTokenBucketExpirationTTL(time.Hour) return func(ctx context.Context) { httpError := tollbooth.LimitByRequest(lmt, ctx.ResponseWriter, ctx.Request) if httpError != nil { lmt.ExecOnLimitReached(ctx.ResponseWriter, ctx.Request) ctx.ResponseWriter.Header().Add("Content-Type", lmt.GetMessageContentType()) ctx.ResponseWriter.WriteHeader(httpError.StatusCode) , = ctx.ResponseWriter.Write([]byte("too many request")) return } } }
Q
[beego]beego怎么使用限流插件
4
A
回答
5
试试使用ctx.Output.Body(xxx)
7
NewLimiter要提出来,可以放在init函数里,使限流实例对所有reques请求公用,不然每个请求都会有各自的NewLimiter实例,限流不会生效
9
来自老司机的指引
5
那意思就是不能做到对某些或者某个请求做限流了?