:::info
支持。
:::
默认已开启
// see internal/router/router.go...mux, err := core.New(logger, ... core.WithEnableRate(), ...)// 默认设置 core.WithEnableRate() 表示已设置限流。// 如果不写这一行,表示不设置限流。
限流代码片段
// see internal/pkg/core/core.go ...limiter := rate.NewLimiter(rate.Every(time.Second*1), configs.MaxRequestsPerSecond)mux.engine.Use(func(ctx *gin.Context) { context := newContext(ctx) defer releaseContext(context) if !limiter.Allow() { context.AbortWithError(errno.NewError( http.StatusTooManyRequests, code.TooManyRequests, code.Text(code.TooManyRequests)), ) return } ctx.Next()})
限流器