ratelimit

package
v0.0.0-...-3cdf4da Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2017 License: MIT Imports: 4 Imported by: 0

README

GoDoc

Rate Limit package

Simple Rate Limiter for your http requests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rate

type Rate struct {
	// Amount of events allowed in a given window
	Limit int

	// Time window in seconds to limit
	Window int

	// Actual count
	EventCount int

	// Start time
	Start time.Time

	// Sync Mutex
	sync.RWMutex
}

Rate represents a single client count for a given event. Its used internally by RateLimit to count different keys.

func (*Rate) Count

func (r *Rate) Count() error

Count checks for the actual limit/window and resets the window (Start) and Count when corresponding. If the limit has been reached for the actual window it returns a RateLimitError error.

type RateLimit

type RateLimit struct {
	// Amount of events allowed in a given window
	Limit int

	// Time window in seconds to limit
	Window int

	// Sync Mutex
	sync.RWMutex
	// contains filtered or unexported fields
}

func New

func New(limit, window int) *RateLimit

func (*RateLimit) Count

func (rl *RateLimit) Count(key string) error

Count checks on a given key, for the actual limit/window and resets the window (Start) and Count when corresponding. If the limit has been reached for the actual window it returns a RateLimitError error.

func (*RateLimit) Get

func (rl *RateLimit) Get(key string) *Rate

type RateLimitError

type RateLimitError struct{}

RateLimitError indicates that the limit has been reached for the actual window.

func (RateLimitError) Error

func (e RateLimitError) Error() string

type RateLimiter

type RateLimiter struct {
	yarf.Middleware
	// contains filtered or unexported fields
}

RateLimiter middleware provides request rate limits per IP

func YarfMiddleware

func YarfMiddleware(limit, window int) *RateLimiter

YarfMiddleware constructor receives the requests limit and a time window (in seconds) to allow. Any IP that requests more than the limit within the time window will be blocked until the time window ends and a new one starts.

func (*RateLimiter) PreDispatch

func (m *RateLimiter) PreDispatch(c *yarf.Context) error

PreDispatch performs the requests counting and handle blocks/

type YarfError

type YarfError struct{}

YarfError is the custom error type compatible with Yarf's YError

func (*YarfError) Body

func (e *YarfError) Body() string

Body returns the error's content body, if needed, to be returned in the HTTP response.

func (*YarfError) Code

func (e *YarfError) Code() int

Code returns the error's HTTP code to be used in the response.

func (*YarfError) Error

func (e *YarfError) Error() string

Implements the error interface returning the ErrorMsg value of each error.

func (*YarfError) ID

func (e *YarfError) ID() int

ID returns the error's ID for further reference.

func (*YarfError) Msg

func (e *YarfError) Msg() string

Msg returns the error's message, used to implement the Error interface.

Jump to

Keyboard shortcuts

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