rkmidlimit

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: Apache-2.0 Imports: 7 Imported by: 4

Documentation

Overview

package rkmidlimit provide options

Index

Constants

View Source
const (
	TokenBucket   = "tokenBucket"
	LeakyBucket   = "leakyBucket"
	DefaultLimit  = 1000000
	GlobalLimiter = "rk-limiter"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BeforeCtx

type BeforeCtx struct {
	Input struct {
		UrlPath string
	}
	Output struct {
		ErrResp *rkerror.ErrorResp
	}
}

BeforeCtx context for Before() function

func NewBeforeCtx

func NewBeforeCtx() *BeforeCtx

NewBeforeCtx create new BeforeCtx with fields initialized

type BootConfig

type BootConfig struct {
	Enabled   bool   `yaml:"enabled" json:"enabled"`
	Algorithm string `yaml:"algorithm" json:"algorithm"`
	ReqPerSec int    `yaml:"reqPerSec" json:"reqPerSec"`
	Paths     []struct {
		Path      string `yaml:"path" json:"path"`
		ReqPerSec int    `yaml:"reqPerSec" json:"reqPerSec"`
	} `yaml:"paths" json:"paths"`
}

BootConfig for YAML

type Limiter

type Limiter func() error

User could implement

type NoopLimiter

type NoopLimiter struct{}

NoopLimiter will do nothing

func (*NoopLimiter) Limit

func (l *NoopLimiter) Limit() error

Limit will do nothing

type Option

type Option func(*optionSet)

Option if for middleware options while creating middleware

func ToOptions

func ToOptions(config *BootConfig, entryName, entryType string) []Option

ToOptions convert BootConfig into Option list

func WithAlgorithm

func WithAlgorithm(algo string) Option

WithAlgorithm provide algorithm of rate limit. - tokenBucket - leakyBucket

func WithEntryNameAndType

func WithEntryNameAndType(entryName, entryType string) Option

WithEntryNameAndType provide entry name and entry type.

func WithGlobalLimiter

func WithGlobalLimiter(l Limiter) Option

WithGlobalLimiter provide user defined Limiter.

func WithLimiterByPath

func WithLimiterByPath(path string, l Limiter) Option

WithLimiterByPath provide user defined Limiter by method.

func WithMockOptionSet

func WithMockOptionSet(mock OptionSetInterface) Option

WithMockOptionSet provide mock OptionSetInterface

func WithReqPerSec

func WithReqPerSec(reqPerSec int) Option

WithReqPerSec Provide request per second.

func WithReqPerSecByPath

func WithReqPerSecByPath(path string, reqPerSec int) Option

WithReqPerSecByPath Provide request per second by method.

type OptionSetInterface

type OptionSetInterface interface {
	GetEntryName() string

	GetEntryType() string

	Before(*BeforeCtx)

	BeforeCtx(*http.Request) *BeforeCtx
}

OptionSetInterface mainly for testing purpose

func NewOptionSet

func NewOptionSet(opts ...Option) OptionSetInterface

NewOptionSet Create new optionSet with options.

func NewOptionSetMock

func NewOptionSetMock(before *BeforeCtx) OptionSetInterface

NewOptionSetMock for testing purpose

type ZeroRateLimiter

type ZeroRateLimiter struct{}

ZeroRateLimiter will block requests.

func (*ZeroRateLimiter) Limit

func (l *ZeroRateLimiter) Limit() error

Limit will block request and return error

Jump to

Keyboard shortcuts

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