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.
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 }
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 ¶
Body returns the error's content body, if needed, to be returned in the HTTP response.
func (*YarfError) Error ¶
Implements the error interface returning the ErrorMsg value of each error.