limiter

package
v0.0.0-...-8406b6b Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package limiter 针对 LimiterIface 接口实现了我们的 MethodLimiter 限流器

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

Limiter 存储令牌桶与键值对名称的映射关系

type LimiterBucketRule

type LimiterBucketRule struct {
	// 自定义键值对名称
	Key string
	// 间隔多久时间放 N 个令牌
	FillInterval time.Duration
	// 令牌桶的容量
	Capacity int64
	// 每次到达间隔时间后所放的具体令牌数量
	Quantum int64
}

LimiterBucketRule 存储令牌桶的一些相应规则属性

type LimiterIface

type LimiterIface interface {
	// Key 获取对应的限流器的键值对名称
	Key(c *gin.Context) string
	// GetBucket 获取令牌桶
	GetBucket(key string) (*ratelimit.Bucket, bool)
	// AddBuckets 新增多个令牌桶
	AddBuckets(rules ...LimiterBucketRule) LimiterIface
}

LimiterIface 用于定义当前限流器所必须要的方法 go 语言中的面向对象 不同的接口可能要不同的限流器,但要统一限流器内要实现的方法

func NewMethodLimiter

func NewMethodLimiter() LimiterIface

type MethodLimiter

type MethodLimiter struct {
	*Limiter
}

func (MethodLimiter) AddBuckets

func (l MethodLimiter) AddBuckets(rules ...LimiterBucketRule) LimiterIface

func (MethodLimiter) GetBucket

func (l MethodLimiter) GetBucket(key string) (*ratelimit.Bucket, bool)

func (MethodLimiter) Key

func (l MethodLimiter) Key(c *gin.Context) string

Key 根据 RequestURI 切割出核心路由作为键值对名称

Jump to

Keyboard shortcuts

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