limit

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2023 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package routerlimit provide router limit tools

Index

Constants

View Source
const (
	// NoLimit 无限制
	NoLimit int = -1
	// Block 禁止
	Block int = 0
)

Variables

This section is empty.

Functions

func DefaultRateFn added in v0.2.30

func DefaultRateFn(ctx context.Context, key string, limit int, period time.Duration,
	n int) (remaining int, reset time.Duration, allowed bool)

DefaultRateFn

func StreamServerInterceptor

func StreamServerInterceptor(limiter *Limiter) grpc.StreamServerInterceptor

StreamServerInterceptor returns a new stream server interceptor that performs rate limiting on the request.

func UnaryServerInterceptor

func UnaryServerInterceptor(limiter *Limiter) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptors that performs request rate limiting.

Types

type Default

type Default struct {
	Headers  []string
	Quota    int
	Duration time.Duration
}

Default the default limit

type Getter

type Getter interface {
	Get(key string) string
}

Getter the getter interface for map or http header

type KV

type KV struct {
	Key   string
	Value string
}

KV kv

type Limit

type Limit struct {
	Prefix   string
	Headers  []string
	Quota    int
	Duration time.Duration
}

Limit data

type LimitValue

type LimitValue struct {
	// 频次限制key
	Key string
	// 频次限制提示消息
	Message string
	// Duration 周期(向桶中放置 Token 的间隔)
	Duration time.Duration
	// Quota 配额
	Quota int
}

LimitValue the limit value

type Limiter

type Limiter struct {
	RateFn RateFn
	Config *RouterLimit
}

Limiter the limiter struct for hold fn and config

type RateFn added in v0.2.30

type RateFn func(ctx context.Context, key string,
	limit int, period time.Duration, n int) (remaining int, reset time.Duration, allowed bool)

RateFn the limit persistence fn for store limit status

type RouterLimit

type RouterLimit struct {
	// 默认限制:路由下匹配 header key 做限制
	Limit []Limit
	// 设置黑名单:当 header key value 匹配某规则时,则禁止访问
	Block []KV
	// 默认频次控制
	Default Default
	// 关闭频次限制
	Disabled bool
}

RouterLimit 路由频次限制

func (*RouterLimit) Match

func (r *RouterLimit) Match(path string, header Getter) *LimitValue

Match 匹配频次限制规则

func (*RouterLimit) MatchHeader

func (r *RouterLimit) MatchHeader(path string, data map[string][]string) *LimitValue

MatchHeader 匹配http header 类型

func (*RouterLimit) MatchMap

func (r *RouterLimit) MatchMap(path string, data map[string]string) *LimitValue

MatchMap 匹配Map类型

Jump to

Keyboard shortcuts

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