Documentation
¶
Index ¶
- Variables
- type Limit
- type Option
- func WithVerifiedAvailWindowSecond(sec int) Option
- func WithVerifiedKeyExpires(expires time.Duration) Option
- func WithVerifiedKeyPrefix(k string) Option
- func WithVerifiedMaxErrorQuota(cnt int) Option
- func WithVerifiedMaxSendPerDay(cnt int) Option
- func WithVerifiedResendIntervalSecond(sec int) Option
- type PeriodFailureLimit
- func (p *PeriodFailureLimit) Check(ctx context.Context, kind, key string, success bool, ...) (PeriodFailureLimitState, error)
- func (p *PeriodFailureLimit) CheckErr(ctx context.Context, kind, key string, err error, ...) (PeriodFailureLimitState, error)
- func (p *PeriodFailureLimit) Del(ctx context.Context, kind, key string) error
- func (p *PeriodFailureLimit) GetInt(ctx context.Context, kind, key string) (int, bool, error)
- func (p *PeriodFailureLimit) SetQuotaFull(ctx context.Context, kind, key string, opts ...PeriodLimitParamOption) error
- func (p *PeriodFailureLimit) TTL(ctx context.Context, kind, key string) (time.Duration, error)
- type PeriodFailureLimitState
- type PeriodLimit
- func (p *PeriodLimit) Del(ctx context.Context, kind, key string) error
- func (p *PeriodLimit) GetInt(ctx context.Context, kind, key string) (int, bool, error)
- func (p *PeriodLimit) SetQuotaFull(ctx context.Context, kind, key string, opts ...PeriodLimitParamOption) error
- func (p *PeriodLimit) TTL(ctx context.Context, kind, key string) (time.Duration, error)
- func (p *PeriodLimit) Take(ctx context.Context, kind, key string, opts ...PeriodLimitParamOption) (PeriodLimitState, error)
- type PeriodLimitOption
- type PeriodLimitOptionSetter
- type PeriodLimitParamOption
- type PeriodLimitParamSetter
- type PeriodLimitState
- type TokenLimit
- type VerifiedLimit
- type VerifiedProvider
Constants ¶
This section is empty.
Variables ¶
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
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.
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 (Limit) Borrow ¶
func (l Limit) Borrow()
Borrow borrows an element from Limit in blocking mode.
type Option ¶
type Option func(*VerifiedLimit)
Option VerifiedLimit 选项
func WithVerifiedAvailWindowSecond ¶
WithVerifiedAvailWindowSecond 验证码有效窗口时间, 默认180, 单位: 秒
func WithVerifiedKeyExpires ¶
WithVerifiedKeyExpires redis存验证码key的过期时间, 默认 24小时
func WithVerifiedKeyPrefix ¶
WithVerifiedKeyPrefix redis存验证码key的前缀, 默认 limit:verified:
func WithVerifiedMaxErrorQuota ¶
WithVerifiedMaxErrorQuota 验证码最大验证失败次数, 默认: 3
func WithVerifiedMaxSendPerDay ¶
WithVerifiedMaxSendPerDay 验证码一天最大发送次数, 默认: 10
func WithVerifiedResendIntervalSecond ¶
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 ¶
func (p *PeriodFailureLimit) CheckErr(ctx context.Context, kind, key string, err error, opts ...PeriodLimitParamOption) (PeriodFailureLimitState, error)
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) SetQuotaFull ¶
func (p *PeriodFailureLimit) SetQuotaFull(ctx context.Context, kind, key string, opts ...PeriodLimitParamOption) error
SetQuotaFull set a permit over quota.
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) SetQuotaFull ¶
func (p *PeriodLimit) SetQuotaFull(ctx context.Context, kind, key string, opts ...PeriodLimitParamOption) error
SetQuotaFull set a permit over quota.
func (*PeriodLimit) TTL ¶
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
func Period(v time.Duration) PeriodLimitOption
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).
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) 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 ¶
VerifiedProvider the provider