limiter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2020 License: MIT Imports: 9 Imported by: 0

README

go-limiter

license

Go-Limiter is a rate limiter which can throttle up and back requests in a specified situation.
Go-Limiter* can apply to a custom function that revaluates a current limit of the requests in a specified situation but doesn't get out between min and max limit that you set.
Revaluate time happens periodically; of course, it supports a custom interval that you set.
It's based on memory or redis. If you use it in distributed applications, recommend redis.

Getting Started

package main
import (
    "time"
	"github.com/gjbae1212/go-limiter"
)

func main() {
    cache, _ := limiter.NewMemoryCache() // or limiter.NewRedisCache 
    
    rateLimiter, _ := limiter.NewRateLimiter("service-name", cache,
        limiter.WithEstimatePeriod(10 * time.Minute),
        limiter.WithMinLimit(10),
        limiter.WithMaxLimit(10000),
    )
   
    // ok is true : doesn't exceed limit.
    // ok is false : exceed limit. 
    ok, _ := rateLimiter.Acquire()    
}

LICENSE

This project is following The MIT.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidParams = errors.New("[err] invalid params")
	ErrUnknown       = errors.New("[err] unknown")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Get(key string) (interface{}, bool, error)
	MGet(keys ...string) ([]interface{}, bool, error)
	Set(key string, value interface{}, ttl time.Duration) error
	Increment(key string, n int64, ttl time.Duration) error
	Ping(ctx context.Context) bool
	Close() error
}

Cache is an interface which supports fundamental functionality for storage. This is used by RateLimiter.

func NewMemoryCache

func NewMemoryCache() (Cache, error)

NewMemoryCache returns cache for memory.

func NewRedisCache

func NewRedisCache(cli *redis.Client) (Cache, error)

NewRedisCache returns cache for redis

type ErrorHandler

type ErrorHandler func(error)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is an interface for dependency injection.

type OptionFunc

type OptionFunc func(l *limiter)

OptionFunc is a function for Option interface.

func WithErrorHandler

func WithErrorHandler(h ErrorHandler) OptionFunc

WithErrorHandler returns a function which sets handler to process error.

func WithEstimatePeriod

func WithEstimatePeriod(d time.Duration) OptionFunc

WithEstimatePeriod returns a function which sets a period to estimate threshold.

func WithMaxLimit

func WithMaxLimit(max int64) OptionFunc

WithMaxLimit returns a function which sets maximum limit per minute in limiter.

func WithMinLimit

func WithMinLimit(min int64) OptionFunc

WithMinLimit returns a function which sets minimum limit per minute in limiter.

func WithRevaluateLimit

func WithRevaluateLimit(f RevaluateLimit) OptionFunc

WithRevaluateLimit returns a function which sets function used to revaluate limit.

type RateLimiter

type RateLimiter interface {
	Acquire() (bool, error)
	CurrentLimit() (int64, error)
	HealthCheck(ctx context.Context) bool
	Close() error
}

func NewRateLimiter

func NewRateLimiter(serviceName string, cache Cache, opts ...Option) (RateLimiter, error)

NewRateLimiter returns RateLimiter interface.

type RevaluateLimit

type RevaluateLimit func(currentlyLimit, estimatedTimeWindowSize, estimatedTimeWindowUsedCount int64) int64

RevaluateLimit is to revaluate sending limit per 1 minute. currentlyLimit is currently applied limit. estimatedTimeWindowSize is the size of time-window for the duration of an estimated period. estimatedTimeWindowUsedCount is the sum of sending push count of time-window for the duration of an estimated period. Its return value is new limit per 1 minute.

Jump to

Keyboard shortcuts

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