代码拉取完成,页面将自动刷新
package serverplugin
import (
"context"
"time"
"github.com/juju/ratelimit"
"github.com/syndtr/goleveldb/leveldb/errors"
)
var ErrReqReachLimit = errors.New("request reached rate limit")
// ReqRateLimitingPlugin can limit requests per unit time
type ReqRateLimitingPlugin struct {
FillInterval time.Duration
Capacity int64
bucket *ratelimit.Bucket
block bool // blocks or return error if reach the limit
}
// NewReqRateLimitingPlugin creates a new RateLimitingPlugin
func NewReqRateLimitingPlugin(fillInterval time.Duration, capacity int64, block bool) *ReqRateLimitingPlugin {
tb := ratelimit.NewBucket(fillInterval, capacity)
return &ReqRateLimitingPlugin{
FillInterval: fillInterval,
Capacity: capacity,
bucket: tb,
block: block,
}
}
// PreReadRequest can limit request processing.
func (plugin *ReqRateLimitingPlugin) PreReadRequest(ctx context.Context) error {
if plugin.block {
plugin.bucket.Wait(1)
return nil
}
count := plugin.bucket.TakeAvailable(1)
if count == 1 {
return nil
}
return ErrReqReachLimit
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。