ratelimit

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ratelimit 提供了一个灵活的令牌桶实现,用于速率限制。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

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

Bucket 表示一个灵活的速率限制令牌桶

func NewBucket

func NewBucket(ctx context.Context, fillInterval time.Duration, capacity int64, opts ...BucketOption) (*Bucket, error)

NewBucket 创建并返回一个新的令牌桶

func (*Bucket) Available

func (b *Bucket) Available() int64

Available 返回当前可用的令牌数量

func (*Bucket) Close

func (b *Bucket) Close()

Close 关闭令牌桶,停止填充

func (*Bucket) Take

func (b *Bucket) Take(ctx context.Context, count int64) error

Take 尝试从桶中获取指定数量的令牌

type BucketOption

type BucketOption func(*Bucket)

BucketOption 定义了创建Bucket时的选项

func WithStrictMode

func WithStrictMode() BucketOption

WithStrictMode 设置严格模式的选项

type LimitMode

type LimitMode int

LimitMode 定义速率限制的模式

const (
	// ModeRelaxed 允许在高负载下稍微超过速率限制
	ModeRelaxed LimitMode = iota
	// ModeStrict 严格执行速率限制
	ModeStrict
)

type RateLimiter

type RateLimiter interface {
	Take(ctx context.Context, count int64) error
	Close()
	Available() int64
}

RateLimiter 接口定义了速率限制器的基本操作

Jump to

Keyboard shortcuts

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