limit

package
v0.0.0-...-b2e13bb Latest Latest
Warning

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

Go to latest
Published: May 19, 2024 License: MIT Imports: 5 Imported by: 0

README

limit

限制器

  • PeriodLimitManager 周期限制管理器, 限制周期次数. 比如 一天限制某个操作多少次.
  • PeriodFailureLimitManager 周期失败限制管理器, 限制周期内失败次数. 比如密码错误次数限制

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 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 LimitToken

type LimitToken interface {
	AllowN(now time.Time, n int) bool
	Allow() bool
}

type PeriodFailureLimit

type PeriodFailureLimit[S PeriodFailureStorage] struct {
	// contains filtered or unexported fields
}

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

func NewPeriodFailureLimit

func NewPeriodFailureLimit[S PeriodFailureStorage](store S, opts ...PeriodLimitOption) *PeriodFailureLimit[S]

NewPeriodFailureLimit returns a PeriodFailureLimit with given parameters.

func (*PeriodFailureLimit[S]) Check

func (p *PeriodFailureLimit[S]) Check(ctx context.Context, key string, success bool) (PeriodFailureLimitState, error)

Check requests a permit.

func (*PeriodFailureLimit[S]) CheckErr

func (p *PeriodFailureLimit[S]) CheckErr(ctx context.Context, key string, err error) (PeriodFailureLimitState, error)

CheckErr requests a permit state. same as Check

func (*PeriodFailureLimit[S]) Del

func (p *PeriodFailureLimit[S]) Del(ctx context.Context, key string) error

Del delete a permit

func (*PeriodFailureLimit[S]) GetRunValue

func (p *PeriodFailureLimit[S]) GetRunValue(ctx context.Context, key string) (*RunValue, error)

GetRunValue get run value Exist: false if key not exist. Count: current failure count TTL: not set expire time, t = -1

func (*PeriodFailureLimit[S]) SetQuotaFull

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

SetQuotaFull set a permit over quota.

type PeriodFailureLimitDriver

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
	// GetRunValue get run value
	// Exist: false if key not exist.
	// Count: current failure count
	// TTL: not set expire time, t = -1.
	GetRunValue(ctx context.Context, key string) (*RunValue, error)
}

PeriodFailureLimitDriver driver interface

type PeriodFailureLimitManager

type PeriodFailureLimitManager[T comparable] struct {
	// contains filtered or unexported fields
}

PeriodFailureLimitManager manage limit period failure

func NewPeriodFailureLimitManager

func NewPeriodFailureLimitManager[T comparable]() *PeriodFailureLimitManager[T]

NewPeriodFailureLimitManager new a instance

func NewPeriodFailureLimitManagerWithDriver

func NewPeriodFailureLimitManagerWithDriver[T comparable](drivers map[T]PeriodFailureLimitDriver) *PeriodFailureLimitManager[T]

NewPeriodFailureLimitManagerWithDriver new a instance with driver

func (*PeriodFailureLimitManager[T]) Acquire

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

func (*PeriodFailureLimitManager[T]) Register

func (p *PeriodFailureLimitManager[T]) Register(kind T, d PeriodFailureLimitDriver) error

Register 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 PeriodFailureStorage

type PeriodFailureStorage interface {
	Check(ctx context.Context, key string, quota, expireSec int, success bool) (int64, error)
	SetQuotaFull(ctx context.Context, key string, quota, expireSec int) error
	Del(ctx context.Context, key string) error
	GetRunValue(ctx context.Context, key string) ([]int64, error)
}

type PeriodLimit

type PeriodLimit[S PeriodStorage] struct {
	// contains filtered or unexported fields
}

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

func NewPeriodLimit

func NewPeriodLimit[S PeriodStorage](store S, opts ...PeriodLimitOption) *PeriodLimit[S]

NewPeriodLimit returns a PeriodLimit with given parameters.

func (*PeriodLimit[S]) Del

func (p *PeriodLimit[S]) Del(ctx context.Context, key string) error

Del delete a permit

func (*PeriodLimit[S]) GetRunValue

func (p *PeriodLimit[S]) GetRunValue(ctx context.Context, key string) (*RunValue, error)

GetRunValue get run value Exist: false if key not exist. Count: current count TTL: not set expire time, t = -1

func (*PeriodLimit[S]) SetQuotaFull

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

SetQuotaFull set a permit over quota.

func (*PeriodLimit[S]) Take

func (p *PeriodLimit[S]) Take(ctx context.Context, key string) (PeriodLimitState, error)

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

type PeriodLimitDriver

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
	// GetRunValue get run value
	// Exist: false if key not exist.
	// Count: current count
	// TTL: not set expire time, t = -1.
	GetRunValue(ctx context.Context, key string) (*RunValue, error)
}

PeriodLimitDriver driver interface

type PeriodLimitManager

type PeriodLimitManager[T comparable] struct {
	// contains filtered or unexported fields
}

PeriodLimitManager manage limit period

func NewPeriodLimitManager

func NewPeriodLimitManager[T comparable]() *PeriodLimitManager[T]

NewPeriodLimitManager new a instance

func NewPeriodLimitManagerWithDriver

func NewPeriodLimitManagerWithDriver[T comparable](drivers map[T]PeriodLimitDriver) *PeriodLimitManager[T]

NewPeriodLimitManagerWithDriver new a instance with driver

func (*PeriodLimitManager[T]) Acquire

func (p *PeriodLimitManager[T]) Acquire(kind T) PeriodLimitDriver

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

func (*PeriodLimitManager[T]) Register

func (p *PeriodLimitManager[T]) Register(kind T, 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

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

func WithKeyPrefix(k string) PeriodLimitOption

WithKeyPrefix set key prefix

func WithPeriod

func WithPeriod(v time.Duration) PeriodLimitOption

WithPeriod a period of time, must greater than a second

func WithQuota

func WithQuota(v int) PeriodLimitOption

WithQuota limit quota requests during a period seconds of time.

type PeriodLimitOptionSetter

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 PeriodStorage

type PeriodStorage interface {
	Take(ctx context.Context, key string, quota, expireSec int) (int64, error)
	SetQuotaFull(ctx context.Context, key string, quota, expireSec int) error
	Del(ctx context.Context, key string) error
	GetRunValue(ctx context.Context, key string) ([]int64, error)
}

type RunValue

type RunValue struct {
	Exist bool          //  false if key not exist.
	Count int64         // count value
	TTL   time.Duration // TTL in seconds
}

RunValue run value int redis

type UnsupportedPeriodFailureLimitDriver

type UnsupportedPeriodFailureLimitDriver struct{}

UnsupportedPeriodFailureLimitDriver unsupported limit period failure driver

func (UnsupportedPeriodFailureLimitDriver) Check

func (UnsupportedPeriodFailureLimitDriver) CheckErr

func (UnsupportedPeriodFailureLimitDriver) Del

func (UnsupportedPeriodFailureLimitDriver) GetRunValue

func (UnsupportedPeriodFailureLimitDriver) SetQuotaFull

type UnsupportedPeriodLimitDriver

type UnsupportedPeriodLimitDriver struct{}

UnsupportedPeriodLimitDriver unsupported limit period driver

func (UnsupportedPeriodLimitDriver) Del

func (UnsupportedPeriodLimitDriver) GetRunValue

func (u UnsupportedPeriodLimitDriver) GetRunValue(ctx context.Context, key string) (*RunValue, error)

func (UnsupportedPeriodLimitDriver) SetQuotaFull

func (UnsupportedPeriodLimitDriver) Take

Directories

Path Synopsis
v8
v9

Jump to

Keyboard shortcuts

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