mirror of
https://github.com/smallnest/rpcx.git
synced 2026-04-22 23:27:06 +08:00
44 lines
1018 B
Go
44 lines
1018 B
Go
package serverplugin
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/juju/ratelimit"
|
|
"github.com/smallnest/rpcx/server"
|
|
)
|
|
|
|
// 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 server.ErrReqReachLimit
|
|
}
|