2019-02-26 13:50:07 +00:00
# TODO -- RateLimit
Protection from Too Many Calls
{: .subtitle }
![RateLimit ](../assets/img/middleware/ratelimit.png )
The RateLimit middleware ensures that services will receive a _fair_ number of requests, and allows you define what is fair.
## Configuration Example
??? example "Limit to 100 requests every 10 seconds (with a possible burst of 200)"
2019-03-29 11:34:05 +00:00
2019-02-26 13:50:07 +00:00
```toml
2019-03-14 08:30:04 +00:00
[http.middlewares]
2019-03-29 11:34:05 +00:00
[http.middlewares.fair-ratelimit.ratelimit]
extractorfunc = "client.ip"
[http.middlewares.fair-ratelimit.ratelimit.rateset1]
period = "10s"
average = 100
burst = 200
2019-02-26 13:50:07 +00:00
```
??? example "Combine multiple limits"
2019-03-29 11:34:05 +00:00
2019-02-26 13:50:07 +00:00
```toml
2019-03-14 08:30:04 +00:00
[http.middlewares]
2019-03-29 11:34:05 +00:00
[http.middlewares.fair-ratelimit.ratelimit]
extractorfunc = "client.ip"
2019-02-26 13:50:07 +00:00
2019-03-29 11:34:05 +00:00
[http.middlewares.fair-ratelimit.ratelimit.rateset1]
period = "10s"
average = 100
burst = 200
[http.middlewares.fair-ratelimit.ratelimit.rateset2]
period = "3s"
average = 5
burst = 10
2019-02-26 13:50:07 +00:00
```
Here, an average of 5 requests every 3 seconds is allowed and an average of 100 requests every 10 seconds. These can "burst" up to 10 and 200 in each period, respectively.
## Configuration Options
2019-04-03 12:32:04 +00:00
### `extractorfunc`
2019-02-26 13:50:07 +00:00
The `extractorfunc` option defines the strategy used to categorize requests.
The possible values are:
- `request.host` categorizes requests based on the request host.
- `client.ip` categorizes requests based on the client ip.
- `request.header.ANY_HEADER` categorizes requests based on the provided `ANY_HEADER` value.
2019-04-03 12:32:04 +00:00
### `ratelimit` (multiple values)
2019-02-26 13:50:07 +00:00
2019-04-03 12:32:04 +00:00
You can combine multiple rate limits.
The rate limit will trigger with the first reached limit.
2019-02-26 13:50:07 +00:00
2019-04-03 12:32:04 +00:00
Each rate limit has 3 options, `period` , `average` , and `burst` .
2019-02-26 13:50:07 +00:00
The rate limit will allow an average of `average` requests every `period` , with a maximum of `burst` request on that period.
!!! note "Period Format"
Period is to be given in a format understood by [time.ParseDuration ](https://golang.org/pkg/time/#ParseDuration ).