rkmidlimit

package
v2.2.22 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 6 Imported by: 16

Documentation

Overview

Package rkmidlimit provide options

Index

Constants

View Source
const (
	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.ErrorInterface
	}
}

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"`
	Ignore    []string `yaml:"ignore" json:"ignore"`
	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

Limiter User could implement it

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. - 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 WithPathToIgnore

func WithPathToIgnore(paths ...string) Option

WithPathToIgnore provide paths prefix that will ignore.

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

	ShouldIgnore(string) bool
}

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