Add a rate limiter

Allow one request to the reports route every 30 seconds and verify the 429 response.

Limit an expensive route

Save this profile as m4w.json in the application directory.

JSON
{
  "listen": { "port": 9000 },
  "protection": {
    "endpoints": [
      {
        "paths": [
          "/reports/*"
        ],
        "methods": [
          "GET"
        ],
        "rate": {
          "requests": 1,
          "period_seconds": 30,
          "burst": 1
        }
      }
    ]
  }
}

Start the server

Shell
php vendor/mini4work/webserver/examples/server.php m4w.json

Send a request

Shell
curl -i http://127.0.0.1:9000/reports/daily
curl -i http://127.0.0.1:9000/reports/daily
curl -i http://127.0.0.1:9000/health

With an initially full bucket, expect 200, then 429 with Retry-After. The health request still reaches the handler and returns 200.

Endpoint limits