redis

package
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidKeyPrefix = errors.New("invalid key prefix")

ErrInvalidKeyPrefix signals that an invalid key prefix has been provided

View Source
var ErrKeyNotExists = errors.New("key does not exist")

ErrKeyNotExists signals that key does not exist

View Source
var ErrNilRedisClient = errors.New("nil redis client")

ErrNilRedisClient signals that a nil redis client has been provided

View Source
var ErrNilRedisClientWrapper = errors.New("nil redis client wrapper")

ErrNilRedisClientWrapper signals that a nil redis client component has been provided

View Source
var ErrNoExpirationTimeForKey = errors.New("key has no expiration time")

ErrNoExpirationTimeForKey signals that key has no expiration time

View Source
var ErrRedisConnectionFailed = errors.New("error connecting to redis")

ErrRedisConnectionFailed signals that connection to redis failed

Functions

func NewRateLimiter

func NewRateLimiter(args ArgsRateLimiter) (*rateLimiter, error)

NewRateLimiter will create a new instance of rate limiter

func NewRedisClientWrapper

func NewRedisClientWrapper(client redis.UniversalClient) (*redisClientWrapper, error)

NewRedisClientWrapper will create a new redis client wrapper component

Types

type ArgsRateLimiter

type ArgsRateLimiter struct {
	OperationTimeoutInSec     uint64
	FreezeFailureConfig       FailureConfig
	SecurityModeFailureConfig FailureConfig
	Storer                    RedisStorer
}

ArgsRateLimiter defines the arguments needed for creating a rate limiter component

type FailureConfig added in v1.0.14

type FailureConfig struct {
	MaxFailures      int64
	LimitPeriodInSec uint64
}

FailureConfig defines the configuration for the rate limiter failure configuration

type Mode added in v1.0.14

type Mode int
const (
	// NormalMode is the mode for normal
	NormalMode Mode = iota
	// SecurityMode is the mode for security
	SecurityMode
)

type RateLimiter

type RateLimiter interface {
	CheckAllowedAndIncreaseTrials(key string, mode Mode) (*RateLimiterResult, error)
	Reset(key string) error
	SetSecurityModeNoExpire(key string) error
	UnsetSecurityModeNoExpire(key string) error
	DecrementSecurityFailedTrials(key string) error
	Period(mode Mode) time.Duration
	Rate(mode Mode) int
	ExtendSecurityMode(key string) error
	IsInterfaceNil() bool
}

RateLimiter defines the behaviour of a rate limiter component

func CreateRedisRateLimiter

func CreateRedisRateLimiter(cfg config.RedisConfig, twoFactorCfg config.TwoFactorConfig) (RateLimiter, error)

CreateRedisRateLimiter will create a new redis rate limiter component

type RateLimiterResult

type RateLimiterResult struct {
	// Allowed specifies if the request was allowed, 1 if allowed
	// and 0 if not allowed
	Allowed bool

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

	// ResetAfter is the time until the expiration time is reached
	// for a given key. For example, if a rate limiter
	// manages requests per minute and received one request 20s ago,
	// reset after would return 40s
	ResetAfter time.Duration
}

RateLimiterResult defines rate limiter result

type RedisStorer

type RedisStorer interface {
	Increment(ctx context.Context, key string) (int64, error)
	Decrement(ctx context.Context, key string) (int64, error)
	SetExpire(ctx context.Context, key string, ttl time.Duration) (bool, error)
	SetExpireIfNotExists(ctx context.Context, key string, ttl time.Duration) (bool, error)
	SetPersist(ctx context.Context, key string) (bool, error)
	SetGreaterExpireTTL(ctx context.Context, key string, ttl time.Duration) (bool, error)
	ResetCounterAndKeepTTL(ctx context.Context, key string) error
	ExpireTime(ctx context.Context, key string) (time.Duration, error)
	IsConnected(ctx context.Context) bool
	IsInterfaceNil() bool
}

RedisStorer defines the behaviour of a redis storer component

Jump to

Keyboard shortcuts

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