ratelimit

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HandleStatsAsync        = "HandleStatsAsync"
	ObserveRegionStatsAsync = "ObserveRegionStatsAsync"
	UpdateSubTree           = "UpdateSubTree"
	HandleOverlaps          = "HandleOverlaps"
	CollectRegionStatsAsync = "CollectRegionStatsAsync"
	SaveRegionToKV          = "SaveRegionToKV"
	SyncRegionToFollower    = "SyncRegionToFollower"
)

RegionHeartbeatStageName is the name of the stage of the region heartbeat.

Variables

View Source
var ErrMaxWaitingTasksExceeded = errors.New("max waiting tasks exceeded")

ErrMaxWaitingTasksExceeded is returned when the number of waiting tasks exceeds the maximum.

Functions

This section is empty.

Types

type ConcurrencyLimiter

type ConcurrencyLimiter struct {
	// contains filtered or unexported fields
}

ConcurrencyLimiter is a limiter that limits the number of concurrent tasks.

func NewConcurrencyLimiter

func NewConcurrencyLimiter(limit uint64) *ConcurrencyLimiter

NewConcurrencyLimiter creates a new ConcurrencyLimiter.

func (*ConcurrencyLimiter) AcquireToken

func (l *ConcurrencyLimiter) AcquireToken(ctx context.Context) (*TaskToken, error)

AcquireToken acquires a token from the limiter. which will block until a token is available or ctx is done, like Timeout.

func (*ConcurrencyLimiter) GetRunningTasksNum

func (l *ConcurrencyLimiter) GetRunningTasksNum() uint64

GetRunningTasksNum returns the number of running tasks.

func (*ConcurrencyLimiter) GetWaitingTasksNum

func (l *ConcurrencyLimiter) GetWaitingTasksNum() uint64

GetWaitingTasksNum returns the number of waiting tasks.

func (*ConcurrencyLimiter) ReleaseToken

func (l *ConcurrencyLimiter) ReleaseToken(token *TaskToken)

ReleaseToken releases the token.

type ConcurrentRunner

type ConcurrentRunner struct {
	// contains filtered or unexported fields
}

ConcurrentRunner is a task runner that limits the number of concurrent tasks.

func NewConcurrentRunner

func NewConcurrentRunner(name string, limiter *ConcurrencyLimiter, maxPendingDuration time.Duration) *ConcurrentRunner

NewConcurrentRunner creates a new ConcurrentRunner.

func (*ConcurrentRunner) RunTask

func (cr *ConcurrentRunner) RunTask(id uint64, name string, f func(context.Context), opts ...TaskOption) error

RunTask runs the task asynchronously.

func (*ConcurrentRunner) Start

func (cr *ConcurrentRunner) Start(ctx context.Context)

Start starts the runner.

func (*ConcurrentRunner) Stop

func (cr *ConcurrentRunner) Stop()

Stop stops the runner.

type Controller

type Controller struct {
	// contains filtered or unexported fields
}

Controller is a controller which holds multiple limiters to manage the request rate of different objects.

func NewController

func NewController(ctx context.Context, typ string, concurrencyGauge *prometheus.GaugeVec) *Controller

NewController returns a global limiter which can be updated in the later.

func (*Controller) Allow

func (l *Controller) Allow(label string) (DoneFunc, error)

Allow is used to check whether it has enough token.

func (*Controller) Close

func (l *Controller) Close()

Close closes the Controller.

func (*Controller) GetConcurrencyLimiterStatus

func (l *Controller) GetConcurrencyLimiterStatus(label string) (limit uint64, current uint64)

GetConcurrencyLimiterStatus returns the status of a given label's concurrency limiter.

func (*Controller) GetQPSLimiterStatus

func (l *Controller) GetQPSLimiterStatus(label string) (limit rate.Limit, burst int)

GetQPSLimiterStatus returns the status of a given label's QPS limiter.

func (*Controller) IsInAllowList

func (l *Controller) IsInAllowList(label string) bool

IsInAllowList returns whether this label is in allow list. If returns true, the given label won't be limited

func (*Controller) Update

func (l *Controller) Update(label string, opts ...Option) UpdateStatus

Update is used to update Ratelimiter with Options

type DimensionConfig

type DimensionConfig struct {
	// qps conifg
	QPS      float64
	QPSBurst int
	// concurrency config
	ConcurrencyLimit uint64
}

DimensionConfig is the limit dimension config of one label

type DoneFunc

type DoneFunc func()

DoneFunc is done function.

type Option

type Option func(string, *Controller) UpdateStatus

Option is used to create a limiter with the optional settings. these setting is used to add a kind of limiter for a service

func AddLabelAllowList

func AddLabelAllowList() Option

AddLabelAllowList adds a label into allow list. It means the given label will not be limited

func InitLimiter

func InitLimiter() Option

InitLimiter creates empty concurrency limiter for a given label by config if it doesn't exist.

func UpdateConcurrencyLimiter

func UpdateConcurrencyLimiter(limit uint64) Option

UpdateConcurrencyLimiter creates a concurrency limiter for a given label if it doesn't exist.

func UpdateDimensionConfig

func UpdateDimensionConfig(cfg *DimensionConfig) Option

UpdateDimensionConfig creates QPS limiter and concurrency limiter for a given label by config if it doesn't exist.

func UpdateQPSLimiter

func UpdateQPSLimiter(limit float64, burst int) Option

UpdateQPSLimiter creates a QPS limiter for a given label if it doesn't exist.

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter is a rate limiter based on `golang.org/x/time/rate`. It implements `Available` function which is not included in `golang.org/x/time/rate`. Note: AvailableN will increase the wait time of WaitN.

func NewRateLimiter

func NewRateLimiter(r float64, b int) *RateLimiter

NewRateLimiter returns a new Limiter that allows events up to rate r (it means limiter refill r token per second) and permits bursts of at most b tokens.

func (*RateLimiter) Allow

func (l *RateLimiter) Allow() bool

Allow is same as `rate.Limiter.Allow`.

func (*RateLimiter) AllowN

func (l *RateLimiter) AllowN(n int) bool

AllowN is same as `rate.Limiter.AllowN`.

func (*RateLimiter) Available

func (l *RateLimiter) Available(n int) bool

Available returns whether limiter has enough tokens. Note: Available will increase the wait time of WaitN.

func (*RateLimiter) Burst

func (l *RateLimiter) Burst() int

Burst returns the maximum burst size. Burst is the maximum number of tokens that can be consumed in a single call to Allow, Reserve, or Wait, so higher Burst values allow more events to happen at once. A zero Burst allows no events, unless limit == Inf.

func (*RateLimiter) Limit

func (l *RateLimiter) Limit() rate.Limit

Limit returns the maximum overall event rate.

func (*RateLimiter) SetBurst

func (l *RateLimiter) SetBurst(burst int)

SetBurst is shorthand for SetBurstAt(time.Now(), newBurst).

func (*RateLimiter) SetLimit

func (l *RateLimiter) SetLimit(limit rate.Limit)

SetLimit is shorthand for SetLimitAt(time.Now(), newLimit).

func (*RateLimiter) WaitN

func (l *RateLimiter) WaitN(ctx context.Context, n int) error

WaitN blocks until lim permits n events to happen. It returns an error if n exceeds the Limiter's burst size, the Context is canceled, or the expected wait time exceeds the Context's Deadline. The burst limit is ignored if the rate limit is Inf.

type Runner

type Runner interface {
	RunTask(id uint64, name string, f func(context.Context), opts ...TaskOption) error
	Start(ctx context.Context)
	Stop()
}

Runner is the interface for running tasks.

type SyncRunner

type SyncRunner struct{}

SyncRunner is a simple task runner that limits the number of concurrent tasks.

func NewSyncRunner

func NewSyncRunner() *SyncRunner

NewSyncRunner creates a new SyncRunner.

func (*SyncRunner) RunTask

func (*SyncRunner) RunTask(_ uint64, _ string, f func(context.Context), _ ...TaskOption) error

RunTask runs the task synchronously.

func (*SyncRunner) Start

func (*SyncRunner) Start(context.Context)

Start starts the runner.

func (*SyncRunner) Stop

func (*SyncRunner) Stop()

Stop stops the runner.

type Task

type Task struct {
	// contains filtered or unexported fields
}

Task is a task to be run.

type TaskOption

type TaskOption func(opts *Task)

TaskOption configures TaskOp

func WithRetained

func WithRetained(retained bool) TaskOption

WithRetained sets whether the task should be retained.

type TaskToken

type TaskToken struct {
	// contains filtered or unexported fields
}

TaskToken is a token that must be released after the task is done.

type UpdateStatus

type UpdateStatus uint32

UpdateStatus is flags for updating limiter config.

const (
	LimiterNotChanged UpdateStatus = 1 << iota
	// LimiterUpdated shows that limiter's config is updated.
	LimiterUpdated
	// LimiterDeleted shows that limiter's config is deleted.
	LimiterDeleted
	// InAllowList shows that limiter's config isn't changed because it is in in allow list.
	InAllowList
)

Flags for limiter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL