ratelimiter

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2022 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package ratelimiter is a generated GoMock package.

Index

Constants

View Source
const (
	DEFAULT_MAX_REQUESTS     = 10
	DEFAULT_REFRESH_INTERVAL = 1 * time.Minute
)

Variables

View Source
var (
	ErrTooManyRequests = errors.New("max requests limit reached")
)

Functions

This section is empty.

Types

type Mockstore

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

Mockstore is a mock of store interface.

func NewMockstore

func NewMockstore(ctrl *gomock.Controller) *Mockstore

NewMockstore creates a new mock instance.

func (*Mockstore) EXPECT

func (m *Mockstore) EXPECT() *MockstoreMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*Mockstore) GetCounter

func (m *Mockstore) GetCounter(ctx context.Context, requestKey string) (int, error)

GetCounter mocks base method.

func (*Mockstore) IncreaseWithTTL

func (m *Mockstore) IncreaseWithTTL(ctx context.Context, requestKey string, ttl time.Duration) error

IncreaseWithTTL mocks base method.

type MockstoreMockRecorder

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

MockstoreMockRecorder is the mock recorder for Mockstore.

func (*MockstoreMockRecorder) GetCounter

func (mr *MockstoreMockRecorder) GetCounter(ctx, requestKey interface{}) *gomock.Call

GetCounter indicates an expected call of GetCounter.

func (*MockstoreMockRecorder) IncreaseWithTTL

func (mr *MockstoreMockRecorder) IncreaseWithTTL(ctx, requestKey, ttl interface{}) *gomock.Call

IncreaseWithTTL indicates an expected call of IncreaseWithTTL.

type RateLimiter

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

RateLimiter represents a rate limiter

func NewRateLimiter

func NewRateLimiter(store store, config RateLimiterConfig) *RateLimiter

NewRateLimiter creates a new rate limiter object

func (RateLimiter) CheckRequest

func (r RateLimiter) CheckRequest(ctx context.Context, keys []string) error

CheckRequest checks if the request is allowed, returns an error when reach the max number of requests

type RateLimiterConfig

type RateLimiterConfig struct {
	MaxRequests     int
	RefreshInterval time.Duration
}

RateLimiter contains the rate limiter's configuration

func (RateLimiterConfig) IsEmpty

func (cfg RateLimiterConfig) IsEmpty() bool

IsEmpty checks if the configuration is empty

func (*RateLimiterConfig) SetDefault

func (cfg *RateLimiterConfig) SetDefault()

SetDefault sets the default configuration (bucket default, 10 reqs/minute)

type Store

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

func NewStore

func NewStore(redisClient *redis.Client) *Store

NewStore creates a new store

func (*Store) GetCounter

func (s *Store) GetCounter(ctx context.Context, requestKey string) (int, error)

GetCounter returns the counter value for the given request key

func (*Store) IncreaseWithTTL

func (s *Store) IncreaseWithTTL(ctx context.Context, requestKey string, ttl time.Duration) error

IncreaseWithTTL increases the counter value and set the TTL to the given bucket defined by the request key

Jump to

Keyboard shortcuts

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