You are browsing documentation for an outdated plugin version.
You can set the rate limit based on peak or non-peak time by using the Pre-function and the Rate Limiting Advanced plugins together.
This example creates two Kong Gateway routes: one to handle peak traffic, and one to handle off-peak traffic. Each route has a different rate limit, which gets applied by the Rate Limiting Advanced plugin. The Pre-function plugin runs a Lua function in the rewrite phase, sending traffic to one of these Kong Gateway routes based on your defined peak and off-peak settings.
Configure rate limit for peak and non-peak times
Set up two routes and apply separate rate limits to each one using the Rate Limiting Advanced plugin.
The names and values of any entities and attributes (service, route, header name, header value, and so on) in the following examples are entirely up to you.
-
Create a service named
httpbin
:curl -i -X POST http://localhost:8001/services \ --data "name=httpbin" \ --data "url=https://httpbin.konghq.com/anything"
-
Create a route to handle peak traffic. Name the route
peak
, attach it to the servicehttpbin
, and set a header with the nameX-Peak
and the valuetrue
:curl -i -X POST http://localhost:8001/services/httpbin/routes \ --data "name=peak" \ --data "paths=/httpbin" \ --data "headers.X-Peak=true"
-
Apply a rate limit to the
peak
route by enabling the Rate Limiting Advanced plugin. This example sets the limit to 10 requests per 30 second window:curl -i -X POST http://localhost:8001/routes/peak/plugins/ \ --data "name=rate-limiting-advanced" \ --data "config.limit=10" \ --data "config.window_size=30"
-
Create another route to handle off-peak traffic. Name the route
off-peak
, attach it to the servicehttpbin
, and set a header with the nameX-Off-Peak
and the valuetrue
:curl -i -X POST http://localhost:8001/services/httpbin/routes \ --data "name=off-peak" \ --data "paths=/httpbin" \ --data "headers.X-Off-Peak=true"
-
Apply a rate limit to the
off-peak
route by enabling the Rate Limiting Advanced plugin. This example sets the limit to 5 requests per 30 second window:curl -i -X POST http://localhost:8001/routes/off-peak/plugins/ \ --data "name=rate-limiting-advanced" \ --data "config.limit=5" \ --data "config.window_size=30"
Apply the Pre-function plugin to route peak and off-peak traffic
-
Create a
ratelimit.lua
file with the following code:local hour = os.date("*t").hour if hour >= 8 and hour <= 17 then kong.service.request.set_header("X-Peak","true") else kong.service.request.set_header("X-Off-Peak","true") end
This function determines the time of day based on the operating system’s time, then sets a request header based on the determined time.
Set the hours based on your own preferred peak times.
-
Apply the Pre-function plugin globally and run it in the rewrite phase:
curl -i -X POST http://localhost:8001/plugins \ --form "name=pre-function" \ --form "config.rewrite[1]=@ratelimit.lua"
This plugin will now pass each request to the correct route based on the time of
day defined in the ratelimit.lua
function.