bbr

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	LimitKey = "LimitKey"
	LimitOp  = "LimitLoad"
)

Variables

View Source
var LimitExceed = errors.New("509:过载保护")

Functions

func NewLimiter

func NewLimiter(options ...options.Option) middleware.MiddleWare
Example
// 建立Group 中间件
middle := NewLimiter()

// 在middleware中
// ctx中携带这两个可配置的有效数据
// 可以通过 ctx.Set

// 配置获取限制器类型,可以根据不同api获取不同的限制器
ctx := context.WithValue(context.TODO(), LimitKey, "key")

// 可配置成功是否上报
// 必须是 overload.Op 类型
ctx = context.WithValue(ctx, LimitOp, overload.Success)

_ = middle
Output:

func SetCPUThreshold

func SetCPUThreshold(cPUThreshold int64) options.Option

func SetDebug

func SetDebug(debug bool) options.Option

func SetEnabled

func SetEnabled(enabled bool) options.Option

func SetRule

func SetRule(rule string) options.Option

func SetWinBucket

func SetWinBucket(winBucket int) options.Option

func SetWindow

func SetWindow(window time.Duration) options.Option

Types

type BBR

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

BBR 实现类似bbr的限制器.

func (*BBR) Allow

func (l *BBR) Allow(ctx context.Context, opts ...overload.AllowOption) (func(info overload.DoneInfo), error)

Allow 检查所有入站流量 一旦检测到过载,它将引发 LimitExceed 错误。

func (*BBR) Stat

func (l *BBR) Stat() Stat

Stat 状态信息

type Group

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

Group 表示BBRLimiter的类,并形成其中的命名空间

func NewGroup

func NewGroup(options ...options.Option) *Group

NewGroup 实例化限制器容器

Example
group := NewGroup()
// 如果没有就会创建
limiter := group.Get("key")
f, err := limiter.Allow(context.TODO())
if err != nil {
	// 代表已经过载了,服务不允许接入
	return
}
// Op:流量实际的操作类型回写记录指标
f(overload.DoneInfo{Op: overload.Success})
Output:

func (*Group) Get

func (g *Group) Get(key string) overload.Limiter

Get 通过指定的键获取一个限制器,如果不存在限制器,则重新创建一个限制器。

type Stat

type Stat struct {
	Cpu         int64
	InFlight    int64
	MaxInFlight int64
	MinRt       int64
	MaxPass     int64
}

Stat bbr 指标信息

Jump to

Keyboard shortcuts

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