Skip to main content
The Rate Limit Traffic Policy action enables you to configure thresholds that restrict the throughput of traffic that successfully reaches your endpoint. You can limit traffic overall or by attributes of the incoming requests.

Configuration reference

The Traffic Policy configuration reference for this action.

Supported phases

on_http_request

Type

rate-limit

Configuration fields

Behavior

Determining the rate limit bucket

When this action runs, ngrok uses information from the incoming HTTP request to determine which rate limit bucket the request falls into. The bucket_key configuration field defines each bucket by specific criteria such as client IP, request host, or a header value. If the bucket hasn’t exceeded its capacity, the request proceeds to the next action in your policy configuration. conn.client_ip is the IP address of the connection to ngrok, not a value from the X-Forwarded-For header. A client can’t change its conn.client_ip bucket by setting X-Forwarded-For. ngrok adds the client IP after any X-Forwarded-For values the client sends rather than replacing them. For details, see Upstream headers.

Multiple buckets

If you specify multiple bucket_key values, the action creates a unique rate limit bucket for each combination of the specified keys. For example, if you have two bucket_key values, such as req.host and conn.client_ip, all incoming requests that have the exact same combination of Host header and client IP are grouped into the same rate limit bucket. To rate limit separately with two different buckets, create multiple rate-limit actions instead.

Rate limit exceeded

If the identified bucket has received more events than its capacity over the specified duration:
  1. The request is rejected with an HTTP 429—Too Many Requests status code.
  2. The retry-after header is included in the response, indicating the number of seconds after which the request may be retried.

Capacity per ingress server

Currently, the capacity for each rate limit bucket is applied per ingress server. This means that each server independently tracks the number of requests and enforces the rate limits accordingly.

Non-terminating action

This is a Non-terminating action. It does not return a response, and will allow Traffic Policy processing to continue to the next Action in the chain. All Cloud Endpoint Traffic Policies must end with a terminating action. This requirement does not apply to Agent Endpoints.

Examples

Rate limit by host header

The following Traffic Policy configuration demonstrates how to use the rate-limit action to rate limit all incoming requests by the Host header.

Example Traffic Policy document

For this example, assume that ngrok is pointing at the upstream service https://httpbin.org.

Example request

In this example, a connection attempt to httpbin.ngrok.app using the curl command returns a 429 status code with a retry-after header indicating the number of seconds to wait before retrying the request.

Action result variables

The following variables are made available for use in subsequent expressions and CEL interpolations after the action has run. Variable values will only apply to the last action execution, results are not concatenated.