ratelimit

package
v6.0.0-beta.13 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ratelimit API 限流中间件

这是以用户或是客户端为单位的限流中间件,并不能按 API 进行细化的限流。

提供了对以下报头的支持: - X-Rate-Limit-Limit: 同一个时间段所允许的请求的最大数目; - X-Rate-Limit-Remaining: 在当前时间段内剩余的请求的数量; - X-Rate-Limit-Reset: 为了得到最大请求数所需等待的 UNIX 时间。

所有数据保存在 web.Cache 之中,缓存服务重启后数据也将重置。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenIP

func GenIP(ctx *web.Context) (string, error)

GenIP 用于生成区分令牌桶的 IP 地址

Types

type GenFunc

type GenFunc func(*web.Context) (string, error)

GenFunc 用于生成用户唯一 ID 的函数

用于区分令牌桶所属的用户

type Ratelimit

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

Ratelimit 提供操作 Bucket 的一系列服务

func New

func New(s *web.Server, prefix string, capacity uint64, rate time.Duration, fn GenFunc) *Ratelimit

New 声明一个新的 Ratelimit

rate 拿令牌的频率; fn 为令牌桶名称的产生方法,默认为用户的 IP;

func (*Ratelimit) Middleware

func (rate *Ratelimit) Middleware(next web.HandlerFunc) web.HandlerFunc

Middleware 将当前中间件应用于 next

func (*Ratelimit) Transfer

func (rate *Ratelimit) Transfer(oldName, newName string) error

Transfer 将 oldName 的数据传送给 newName

Jump to

Keyboard shortcuts

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