limit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMaxSendPerDay       = errors.New("limit: reach the maximum send times")
	ErrResendTooFrequently = errors.New("limit: resend too frequently")
	ErrCodeRequired        = errors.New("limit: code is required")
	ErrCodeExpired         = errors.New("limit: code is expired")
	ErrCodeMaxErrorQuota   = errors.New("limit: over the maximum error quota")
	ErrCodeVerification    = errors.New("limit: code verified failed")
)

error defined for verified

View Source
var ErrLimitReturn = errors.New("limit: discarding limited token, resource pool is full, someone returned multiple times")

ErrLimitReturn indicates that the more than borrowed elements were returned.

View Source
var (
	// ErrUnknownCode is an error that represents unknown status code.
	ErrUnknownCode = errors.New("limit: unknown status code")
)

universal error

Functions

This section is empty.

Types

type Limit

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

Limit controls the concurrent requests.

func NewLimit

func NewLimit(n int) Limit

NewLimit creates a Limit that can borrow n elements from it concurrently.

func (Limit) Borrow

func (l Limit) Borrow()

Borrow borrows an element from Limit in blocking mode.

func (Limit) Return

func (l Limit) Return() error

Return returns the borrowed resource, returns error only if returned more than borrowed.

func (Limit) TryBorrow

func (l Limit) TryBorrow() bool

TryBorrow tries to borrow an element from Limit, in non-blocking mode. If success, true returned, false for otherwise.

type Option

type Option func(*VerifiedLimit)

Option VerifiedLimit 选项

func WithVerifiedAvailWindowSecond

func WithVerifiedAvailWindowSecond(sec int) Option

WithVerifiedAvailWindowSecond 验证码有效窗口时间, 默认180, 单位: 秒

func WithVerifiedKeyExpires

func WithVerifiedKeyExpires(expires time.Duration) Option

WithVerifiedKeyExpires redis存验证码key的过期时间, 默认 24小时

func WithVerifiedKeyPrefix

func WithVerifiedKeyPrefix(k string) Option

WithVerifiedKeyPrefix redis存验证码key的前缀, 默认 limit:verified:

func WithVerifiedMaxErrorQuota

func WithVerifiedMaxErrorQuota(cnt int) Option

WithVerifiedMaxErrorQuota 验证码最大验证失败次数, 默认: 3

func WithVerifiedMaxSendPerDay

func WithVerifiedMaxSendPerDay(cnt int) Option

WithVerifiedMaxSendPerDay 验证码一天最大发送次数, 默认: 10

func WithVerifiedResendIntervalSecond

func WithVerifiedResendIntervalSecond(sec int) Option

WithVerifiedResendIntervalSecond 重发验证码间隔时间, 默认60, 单位: 秒

type PeriodFailureLimit

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

A PeriodFailureLimit is used to limit requests when failure during a period of time.

func NewPeriodFailureLimit

func NewPeriodFailureLimit(store *redis.Client, opts ...PeriodLimitOption) *PeriodFailureLimit

NewPeriodFailureLimit returns a PeriodFailureLimit with given parameters.

func (*PeriodFailureLimit) Check

func (p *PeriodFailureLimit) Check(ctx context.Context, kind, key string, success bool, opts ...PeriodLimitParamOption) (PeriodFailureLimitState, error)

Check requests a permit.

func (*PeriodFailureLimit) CheckErr

CheckErr requests a permit state. same as Check

func (*PeriodFailureLimit) Del

func (p *PeriodFailureLimit) Del(ctx context.Context, kind, key string) error

Del delete a permit

func (*PeriodFailureLimit) GetInt

func (p *PeriodFailureLimit) GetInt(ctx context.Context, kind, key string) (int, bool, error)

GetInt get current failure count

func (*PeriodFailureLimit) SetQuotaFull

func (p *PeriodFailureLimit) SetQuotaFull(ctx context.Context, kind, key string, opts ...PeriodLimitParamOption) error

SetQuotaFull set a permit over quota.

func (*PeriodFailureLimit) TTL

func (p *PeriodFailureLimit) TTL(ctx context.Context, kind, key string) (time.Duration, error)

TTL get key ttl if key not exist, time = -1. if key exist, but not set expire time, t = -2

type PeriodFailureLimitState

type PeriodFailureLimitState int

PeriodFailureLimitState period failure limit state.

const (
	// PeriodFailureLimitStsUnknown means not initialized state.
	PeriodFailureLimitStsUnknown PeriodFailureLimitState = iota - 1
	// PeriodFailureLimitStsSuccess means success.
	PeriodFailureLimitStsSuccess
	// PeriodFailureLimitStsInQuota means within the quota.
	PeriodFailureLimitStsInQuota
	// PeriodFailureLimitStsOverQuota means over the quota.
	PeriodFailureLimitStsOverQuota
)

func (PeriodFailureLimitState) IsOverQuota

func (p PeriodFailureLimitState) IsOverQuota() bool

IsOverQuota means passed the quota.

func (PeriodFailureLimitState) IsSuccess

func (p PeriodFailureLimitState) IsSuccess() bool

IsSuccess means success state.

func (PeriodFailureLimitState) IsWithinQuota

func (p PeriodFailureLimitState) IsWithinQuota() bool

IsWithinQuota means within the quota.

type PeriodLimit

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

A PeriodLimit is used to limit requests during a period of time.

func NewPeriodLimit

func NewPeriodLimit(store *redis.Client, opts ...PeriodLimitOption) *PeriodLimit

NewPeriodLimit returns a PeriodLimit with given parameters.

func (*PeriodLimit) Del

func (p *PeriodLimit) Del(ctx context.Context, kind, key string) error

Del delete a permit

func (*PeriodLimit) GetInt

func (p *PeriodLimit) GetInt(ctx context.Context, kind, key string) (int, bool, error)

GetInt get current count

func (*PeriodLimit) SetQuotaFull

func (p *PeriodLimit) SetQuotaFull(ctx context.Context, kind, key string, opts ...PeriodLimitParamOption) error

SetQuotaFull set a permit over quota.

func (*PeriodLimit) TTL

func (p *PeriodLimit) TTL(ctx context.Context, kind, key string) (time.Duration, error)

TTL get key ttl if key not exist, time = -1. if key exist, but not set expire time, t = -2

func (*PeriodLimit) Take

func (p *PeriodLimit) Take(ctx context.Context, kind, key string, opts ...PeriodLimitParamOption) (PeriodLimitState, error)

Take requests a permit with context, it returns the permit state.

type PeriodLimitOption

type PeriodLimitOption func(l PeriodLimitOptionSetter)

PeriodLimitOption defines the method to customize a PeriodLimit and PeriodFailureLimit.

func Align

func Align() PeriodLimitOption

Align returns a func to customize a PeriodLimit and PeriodFailureLimit with alignment. For example, if we want to limit end users with 5 sms verification messages every day, we need to align with the local timezone and the start of the day.

func KeyPrefix added in v0.1.0

func KeyPrefix(k string) PeriodLimitOption

KeyPrefix set key prefix

func Period added in v0.1.0

Period a period of time, must greater than a second

func Quota added in v0.1.0

func Quota(v int) PeriodLimitOption

Quota limit quota requests during a period seconds of time.

type PeriodLimitOptionSetter added in v0.1.0

type PeriodLimitOptionSetter interface {
	// contains filtered or unexported methods
}

PeriodLimitOptionSetter period limit interface for PeriodLimit and PeriodFailureLimit

type PeriodLimitParamOption added in v0.1.0

type PeriodLimitParamOption func(PeriodLimitParamSetter)

PeriodLimitParamOption period limit param option

func WithPeriodLimitParamPeriod added in v0.1.0

func WithPeriodLimitParamPeriod(t time.Duration) PeriodLimitParamOption

WithPeriodLimitParamPeriod a period of time, must greater than a second

func WithPeriodLimitParamQuota added in v0.1.0

func WithPeriodLimitParamQuota(v int) PeriodLimitParamOption

WithPeriodLimitParamQuota limit quota requests during a period seconds of time.

type PeriodLimitParamSetter added in v0.1.0

type PeriodLimitParamSetter interface {
	// contains filtered or unexported methods
}

GenerateOptionSetter generate option setter

type PeriodLimitState

type PeriodLimitState int

PeriodLimitState period limit state.

const (
	// PeriodLimitStsUnknown means not initialized state.
	PeriodLimitStsUnknown PeriodLimitState = iota - 1
	// PeriodLimitStsAllowed means allowed.
	PeriodLimitStsAllowed
	// PeriodLimitStsHitQuota means hit the quota.
	PeriodLimitStsHitQuota
	// PeriodLimitStsOverQuota means passed the quota.
	PeriodLimitStsOverQuota
)

func (PeriodLimitState) IsAllowed

func (p PeriodLimitState) IsAllowed() bool

IsAllowed means allowed state.

func (PeriodLimitState) IsHitQuota

func (p PeriodLimitState) IsHitQuota() bool

IsHitQuota means this request exactly hit the quota.

func (PeriodLimitState) IsOverQuota

func (p PeriodLimitState) IsOverQuota() bool

IsOverQuota means passed the quota.

type TokenLimit

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

TokenLimit controls how frequently events are allowed to happen with in one second.

func NewTokenLimit

func NewTokenLimit(rate, burst int, key string, store *redis.Client) *TokenLimit

NewTokenLimit returns a new TokenLimit that allows events up to rate and permits bursts of at most burst tokens.

func (*TokenLimit) Allow

func (t *TokenLimit) Allow() bool

Allow is shorthand for AllowN(time.Now(), 1).

func (*TokenLimit) AllowN

func (t *TokenLimit) AllowN(now time.Time, n int) bool

AllowN reports whether n events may happen at time now. Use this method if you intend to drop / skip events that exceed the rate. Otherwise, use Reserve or Wait.

type VerifiedLimit

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

VerifiedLimit verified code limit

func NewVerified

func NewVerified(p VerifiedProvider, store *redis.Client, opts ...Option) *VerifiedLimit

NewVerified new a verified limit

func (*VerifiedLimit) Name

func (v *VerifiedLimit) Name() string

Name the provider name

func (*VerifiedLimit) SendCode

func (v *VerifiedLimit) SendCode(target, code string) error

SendCode send code and store in redis cache.

func (*VerifiedLimit) VerifyCode

func (v *VerifiedLimit) VerifyCode(target, code string) error

VerifyCode verify code from redis cache.

type VerifiedProvider

type VerifiedProvider interface {
	Name() string
	SendCode(target, code string) error
}

VerifiedProvider the provider

Jump to

Keyboard shortcuts

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