Documentation ¶
Overview ¶
Package rate implements rate limiter for Iris client requests. Example can be found at: _examples/request-ratelimit/main.go.
Index ¶
Constants ¶
const Inf = math.MaxFloat64
Inf is the infinite rate limit; it allows all events (even if burst is zero).
Variables ¶
This section is empty.
Functions ¶
func Every ¶
Every converts a minimum time interval between events to a limit. Usage: Limit(Every(1*time.Minute), 3, options...)
func Limit ¶
Limit returns a new rate limiter handler that allows requests up to rate "limit" and permits bursts of at most "burst" tokens. See `rate.SetKey(ctx, key string)` and `rate.Get` too.
E.g. Limit(1, 5) to allow 1 request per second, with a maximum burst size of 5.
See `ExceedHandler`, `ClientData` and `PurgeEvery` for the available "options".
func SetIdentifier ¶
SetIdentifier can be called manually from a handler or a middleare to change the identifier per client. The default key for a client is its Remote IP.
Types ¶
type Client ¶
type Client struct { ID string Data interface{} Limiter *rate.Limiter // contains filtered or unexported fields }
Client holds some request information and the rate limiter itself. It can be retrieved by the `Get` package-level function. It can be used to manually add RateLimit response headers.
func Get ¶
Get returns the current rate limited `Client`. Use it when you want to log or add response headers based on the current request limitation.
You can read more about X-RateLimit response headers at: https://tools.ietf.org/id/draft-polli-ratelimit-headers-00.html. A good example of that is the GitHub API itself: https://developer.github.com/v3/#rate-limiting
func (*Client) DurationFromTokens ¶
DurationFromTokens is a unit conversion function from the number of tokens to the duration of time it takes to accumulate them at a rate of limit tokens per second.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter is featured with the necessary functions to limit requests per second. It has a single exported method `Purge` which helps to manually remove old clients from the memory. Limiter is not exposed by a function, callers should use it inside an `Option` for the `Limit` package-level function.
type Option ¶
type Option func(*Limiter)
Option declares a function which can be passed on `Limit` package-level to modify its internal fields. Available Options are: * ExceedHandler * ClientData * PurgeEvery
func ClientData ¶
ClientData is an `Option` that can be passed at the `Limit` package-level function. It accepts a function which provides the Iris Context and should return custom data that will be stored to the Client and be retrieved as `Get(ctx).Client.Data` later on.
func ExceedHandler ¶
ExceedHandler is an `Option` that can be passed at the `Limit` package-level function. It accepts a handler that will be executed every time a client tries to reach a page/resource which is not accessible for that moment.
func PurgeEvery ¶
PurgeEvery is an `Option` that can be passed at the `Limit` package-level function. This function will check for old entries and remove them.
E.g. Limit(..., PurgeEvery(time.Minute, 5*time.Minute)) to check every 1 minute if a client's last visit was 5 minutes ago ("old" entry) and remove it from the memory.