rate

package
v0.12.14 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: AGPL-3.0, AGPL-3.0-only Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const RedisBanKeyPrefix = "chii:ban:"
View Source
const RedisRateKeyPrefix = "chii:login:rate:"

Variables

This section is empty.

Functions

This section is empty.

Types

type Limit

type Limit struct {
	Rate   int
	Burst  int
	Period time.Duration
}

func PerHour

func PerHour(rate int) Limit

func PerMinute

func PerMinute(rate int) Limit

func PerSecond

func PerSecond(rate int) Limit

func (Limit) IsZero

func (l Limit) IsZero() bool

func (Limit) String

func (l Limit) String() string

type Manager

type Manager interface {
	// Allowed 检查是否允许登录。
	Allowed(ctx context.Context, ip string) (allowed bool, remain int, err error)
	// Reset 登录成功时应该重置计数。
	Reset(ctx context.Context, ip string) error
}

func New

func New(rdb *redis.Client) Manager

type Result

type Result struct {
	// Limit is the limit that was used to obtain this result.
	Limit Limit

	// Allowed is the number of events that may happen at time now.
	Allowed int

	// Remaining is the maximum number of requests that could be
	// permitted instantaneously for this key given the current
	// state. For example, if a rate limiter allows 10 requests per
	// second and has already received 6 requests for this key this
	// second, Remaining would be 4.
	Remaining int

	// RetryAfter is the time until the next request will be permitted.
	// It should be -1 unless the rate limit has been exceeded.
	RetryAfter time.Duration

	// ResetAfter is the time until the RateLimiter returns to its
	// initial state for a given key. For example, if a rate limiter
	// manages requests per second and received one request 200ms ago,
	// Reset would return 800ms. You can also think of this as the time
	// until Limit and Remaining will be equal.
	ResetAfter time.Duration
}

Jump to

Keyboard shortcuts

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