limit

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

universal error

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.

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

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, key string) error

Del delete a permit

func (*PeriodFailureLimit) GetInt

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

GetInt get current failure count

func (*PeriodFailureLimit) SetQuotaFull

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

SetQuotaFull set a permit over quota.

func (*PeriodFailureLimit) TTL

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

type PeriodFailureLimitDriver added in v0.1.3

type PeriodFailureLimitDriver interface {
	// CheckErr requests a permit state.
	// same as Check
	CheckErr(ctx context.Context, key string, err error) (PeriodFailureLimitState, error)
	// Check requests a permit.
	Check(ctx context.Context, key string, success bool) (PeriodFailureLimitState, error)
	// SetQuotaFull set a permit over quota.
	SetQuotaFull(ctx context.Context, key string) error
	// Del delete a permit
	Del(ctx context.Context, key string) error
	// TTL get key ttl
	// if key not exist, time = -1.
	// if key exist, but not set expire time, t = -2
	TTL(ctx context.Context, key string) (time.Duration, error)
	// GetInt get current failure count
	GetInt(ctx context.Context, key string) (int, bool, error)
}

PeriodFailureLimitDriver driver interface

type PeriodFailureLimitManager added in v0.1.4

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

PeriodFailureLimitManager manage limit period failure

func NewPeriodFailureLimitManager added in v0.1.4

func NewPeriodFailureLimitManager() *PeriodFailureLimitManager

NewPeriodFailureLimitManager new a instance

func NewPeriodFailureLimitManagerWithDriver added in v0.1.4

func NewPeriodFailureLimitManagerWithDriver(drivers map[string]PeriodFailureLimitDriver) *PeriodFailureLimitManager

NewPeriodFailureLimitManagerWithDriver new a instance with driver

func (*PeriodFailureLimitManager) Acquire added in v0.1.4

Acquire driver. if driver not exist. it will return UnsupportedPeriodFailureLimitDriver.

func (*PeriodFailureLimitManager) Register added in v0.1.4

PeriodFailureLimitManager register a PeriodFailureLimitDriver with kind.

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, key string) error

Del delete a permit

func (*PeriodLimit) GetInt

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

GetInt get current count

func (*PeriodLimit) SetQuotaFull

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

SetQuotaFull set a permit over quota.

func (*PeriodLimit) TTL

func (p *PeriodLimit) TTL(ctx context.Context, 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, key string) (PeriodLimitState, error)

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

type PeriodLimitDriver added in v0.1.3

type PeriodLimitDriver interface {
	// Take requests a permit with context, it returns the permit state.
	Take(ctx context.Context, key string) (PeriodLimitState, error)
	// SetQuotaFull set a permit over quota.
	SetQuotaFull(ctx context.Context, key string) error
	// Del delete a permit
	Del(ctx context.Context, key string) error
	// TTL get key ttl
	// if key not exist, time = -1.
	// if key exist, but not set expire time, t = -2
	TTL(ctx context.Context, key string) (time.Duration, error)
	// GetInt get current count
	GetInt(ctx context.Context, key string) (int, bool, error)
}

PeriodLimitDriver driver interface

type PeriodLimitManager added in v0.1.4

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

PeriodLimitManager manage limit period

func NewPeriodLimitManager added in v0.1.4

func NewPeriodLimitManager() *PeriodLimitManager

NewPeriodLimitManager new a instance

func NewPeriodLimitManagerWithDriver added in v0.1.4

func NewPeriodLimitManagerWithDriver(drivers map[string]PeriodLimitDriver) *PeriodLimitManager

NewPeriodLimitManagerWithDriver new a instance with driver

func (*PeriodLimitManager) Acquire added in v0.1.4

func (p *PeriodLimitManager) Acquire(kind string) PeriodLimitDriver

Acquire driver. if driver not exist. it will return UnsupportedPeriodLimitDriver.

func (*PeriodLimitManager) Register added in v0.1.4

func (p *PeriodLimitManager) Register(kind string, d PeriodLimitDriver) error

Register register a PeriodLimitDriver with kind

type PeriodLimitOption

type PeriodLimitOption func(l PeriodLimitOptionSetter)

PeriodLimitOption defines the method to customize a PeriodLimit and PeriodFailureLimit.

func WithAlign added in v0.1.3

func WithAlign() PeriodLimitOption

WithAlign 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 WithKeyPrefix added in v0.1.3

func WithKeyPrefix(k string) PeriodLimitOption

WithKeyPrefix set key prefix

func WithPeriod added in v0.1.3

func WithPeriod(v time.Duration) PeriodLimitOption

WithPeriod a period of time, must greater than a second

func WithQuota added in v0.1.3

func WithQuota(v int) PeriodLimitOption

WithQuota 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 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 UnsupportedPeriodFailureLimitDriver added in v0.1.3

type UnsupportedPeriodFailureLimitDriver struct{}

UnsupportedPeriodFailureLimitDriver unsupported limit period failure driver

func (UnsupportedPeriodFailureLimitDriver) Check added in v0.1.3

func (UnsupportedPeriodFailureLimitDriver) CheckErr added in v0.1.3

func (UnsupportedPeriodFailureLimitDriver) Del added in v0.1.3

func (UnsupportedPeriodFailureLimitDriver) GetInt added in v0.1.3

func (UnsupportedPeriodFailureLimitDriver) SetQuotaFull added in v0.1.3

func (UnsupportedPeriodFailureLimitDriver) TTL added in v0.1.3

type UnsupportedPeriodLimitDriver added in v0.1.3

type UnsupportedPeriodLimitDriver struct{}

UnsupportedPeriodLimitDriver unsupported limit period driver

func (UnsupportedPeriodLimitDriver) Del added in v0.1.3

func (UnsupportedPeriodLimitDriver) GetInt added in v0.1.3

func (UnsupportedPeriodLimitDriver) SetQuotaFull added in v0.1.3

func (UnsupportedPeriodLimitDriver) TTL added in v0.1.3

func (UnsupportedPeriodLimitDriver) Take added in v0.1.3

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