| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
The rate limit service is a Go/gRPC service designed to enable generic rate limit scenarios from different types of applications. Applications request a rate limit decision based on a domain and a set of descriptors. The service reads the configuration from disk via runtime, composes a cache key, and talks to the Redis cache. A decision is then returned to the caller.
For every main commit, an image is pushed to Dockerhub. There is currently no versioning (post v1.4.0) and tags are based on commit sha.
The Docker image uses Google's distroless base image (gcr.io/distroless/static-debian12:nonroot) for enhanced security and minimal attack surface. Distroless images contain only the application and its runtime dependencies, omitting unnecessary OS components like package managers, shells, and other utilities.
The image is pinned to a specific SHA digest for deterministic builds and uses the nonroot variant to run as a non-privileged user, following security best practices.
For debugging purposes, you can use the debug variant of the distroless image:
FROM gcr.io/distroless/static-debian12:debug
COPY --from=build /go/bin/ratelimit /bin/ratelimitThis provides shell access and debugging tools while maintaining the security benefits of distroless.
v3 rls.proto is currently supported. Support for v2 rls proto is now deprecated.
Install Redis-server.
Make sure go is setup correctly and checkout rate limit service into your go path. More information about installing go here.
In order to run the integration tests using a local Redis server please run two Redis-server instances: one on port 6379 and another on port 6380
redis-server --port 6379 &
redis-server --port 6380 &To setup for the first time (only done once):
make bootstrapTo compile:
make compileEnsure you set the correct platform if running OSX host with a linux container e.g.
GOOS=linux make compileTo compile and run tests:
make testsTo run the server locally using some sensible default settings you can do this (this will setup the server to read the configuration files from the path you specify):
USE_STATSD=false LOG_LEVEL=debug REDIS_SOCKET_TYPE=tcp REDIS_URL=localhost:6379 RUNTIME_ROOT=/home/user/src/runtime/data RUNTIME_SUBDIRECTORY=ratelimit RUNTIME_APPDIRECTORY=configThe docker-compose setup uses a distroless-based container for the ratelimit service. In order to run the docker-compose setup from the root of the repo, run
docker-compose upThe ratelimit service is built using the main Dockerfile which uses Google's distroless base image for enhanced security and minimal attack surface. The distroless image contains only the application and its runtime dependencies, omitting unnecessary OS components like package managers and shells.
If you want to run with two redis instances, you will need to modify the docker-compose.yml file to run a second redis container, and change the environment variables as explained in the two redis instances section.
To run a fully configured environment to demo Envoy based rate limiting, run:
export CONFIG_TYPE=FILE
docker-compose -f docker-compose-example.yml up --build --remove-orphansThis will run ratelimit, redis, prom-statsd-exporter and two Envoy containers such that you can demo rate limiting by hitting the below endpoints.
curl localhost:8888/test
curl localhost:8888/header -H "foo: foo" # Header based
curl localhost:8888/twoheader -H "foo: foo" -H "bar: bar" # Two headers
curl localhost:8888/twoheader -H "foo: foo" -H "baz: baz" # This will be rate limited
curl localhost:8888/twoheader -H "foo: foo" -H "bar: banned" # Ban a particular header value
curl localhost:8888/twoheader -H "foo: foo" -H "baz: shady" # This will never be ratelimited since "baz" with value "shady" is in shadow_mode
curl localhost:8888/twoheader -H "foo: foo" -H "baz: not-so-shady" # This is subject to rate-limiting because the it's now in shadow_modeEdit examples/ratelimit/config/example.yaml to test different rate limit configs. Hot reloading is enabled.
The descriptors in example.yaml and the actions in examples/envoy/proxy.yaml should give you a good idea on how to configure rate limits.
To see the metrics in the example
# The metrics for the shadow_mode keys
curl http://localhost:9102/metrics | grep -i shadowTo run a fully configured environment to demo Envoy based rate limiting, run:
export CONFIG_TYPE=GRPC_XDS_SOTW
docker-compose -f docker-compose-example.yml --profile xds-config up --build --remove-orphansThis will run in xds-config docker-compose profile which will run example xDS-Server, ratelimit, redis, prom-statsd-exporter and two Envoy containers such that you can demo rate limiting by hitting the below endpoints.
curl localhost:8888/test
curl localhost:8888/header -H "foo: foo" # Header based
curl localhost:8888/twoheader -H "foo: foo" -H "bar: bar" # Two headers
curl localhost:8888/twoheader -H "foo: foo" -H "baz: baz" # This will be rate limited
curl localhost:8888/twoheader -H "foo: foo" -H "bar: banned" # Ban a particular header value
curl localhost:8888/twoheader -H "foo: foo" -H "baz: shady" # This will never be ratelimited since "baz" with value "shady" is in shadow_mode
curl localhost:8888/twoheader -H "foo: foo" -H "baz: not-so-shady" # This is subject to rate-limiting because the it's now in shadow_modeEditexamples/xds-sotw-config-server/resource.go to test different rate limit configs.
To see the metrics in the example
# The metrics for the shadow_mode keys
curl http://localhost:9102/metrics | grep -i shadowIntegration tests are coded as bash-scripts in integration-test/scripts.
The test suite will spin up a docker-compose environment from integration-test/docker-compose-integration-test.yml
If the test suite fails it will exit with code 1.
make integration_testsThe rate limit configuration file format is YAML (mainly so that comments are supported).
Each configuration contains a top level descriptor list and potentially multiple nested lists beneath that. The format is:
domain: <unique domain ID>
descriptors:
- key: <rule key: required>
value: <rule value: optional>
rate_limit: (optional block)
name: (optional)
replaces: (optional)
- name: (optional)
unit: <see below: required>
requests_per_unit: <see below: required>
shadow_mode: (optional)
detailed_metric: (optional)
value_to_metric: (optional)
share_threshold: (optional)
descriptors: (optional block)
- ... (nested repetition of above)Each descriptor in a descriptor list must have a key. It can also optionally have a value to enable a more specific match. The "rate_limit" block is optional and if present sets up an actual rate limit rule. See below for how the rule is defined. If the rate limit is not present and there are no nested descriptors, then the descriptor is effectively whitelisted. Otherwise, nested descriptors allow more complex matching and rate limiting scenarios.
rate_limit:
unit: <second, minute, hour, day>
requests_per_unit: <uint>The rate limit block specifies the actual rate limit that will be used when there is a match. Currently the service supports per second, minute, hour, and day limits. More types of limits may be added in the future based on user demand.
The replaces key indicates that this descriptor will replace the configuration set by another descriptor.
If there is a rule being evaluated, and multiple descriptors can apply, the replaces descriptor will drop evaluation of the descriptor which it is replacing.
To enable this, any descriptor which should potentially be replaced by another should have a name keyword in the rate_limit section, and any descriptor which should potentially replace the original descriptor should have a name keyword in its respective replaces section. Whenever limits match to both rules, only the rule which replaces the original will take effect, and the limit of the original will not be changed after evaluation.
For example, let's say you have a bunch of endpoints and each is classified under read or write, with read having a certain limit and write having another. Each user has a certain limit for both endpoints. However, let's say that you want to increase a user's limit to a single read endpoint. The only option without using replaces would be to increase their limit for the read category. The replaces keyword allows increasing the limit of a single endpoint in this case.
A shadow_mode key in a rule indicates that whatever the outcome of the evaluation of the rule, the end-result will always be "OK".
When a block is in ShadowMode all functions of the rate limiting service are executed as normal, with cache-lookup and statistics
An additional statistic is added to keep track of how many times a key with "shadow_mode" has overridden result.
There is also a Global Shadow Mode
Setting the detailed_metric: true for a descriptor will extend the metrics that are produced. Normally a descriptor that matches a value that is not explicitly listed in the configuration will from a metrics point-of-view be rolled-up into the base entry. This can be problematic if you want to have those details available for analysis.
NB! This should only be enabled in situations where the potentially large cardinality of metrics that this can lead to is acceptable.
Setting value_to_metric: true (default: false) for a descriptor will include the descriptor's runtime value in the metric key, even when the descriptor value is not explicitly defined in the configuration. This allows you to track metrics per descriptor value when the value comes from the runtime request, providing visibility into different rate limit scenarios without needing to pre-define every possible value.
Note: If a value is explicitly specified in a descriptor (e.g., value: "GET"), that value is always included in the metric key regardless of the value_to_metric setting. The value_to_metric flag only affects descriptors where the value is not explicitly defined in the configuration.
When combined with wildcard matching, the full runtime value is included in the metric key, not just the wildcard prefix. This feature works independently of detailed_metric - when detailed_metric is set, it takes precedence and value_to_metric is ignored.
Setting share_threshold: true (default: false) for a descriptor with a wildcard value (ending with *) allows all values matching that wildcard to share the same rate limit threshold, instead of using isolated thresholds for each matching value.
This is useful when you want to apply a single rate limit across multiple resources that match a wildcard pattern. For example, if you have a rule for files/*, both files/a.pdf and files/b.csv will share the same threshold when share_threshold: true is set.
Important notes:
Let's start with a simple example:
domain: mongo_cps
descriptors:
- key: database
value: users
rate_limit:
unit: second
requests_per_unit: 500
- key: database
value: default
rate_limit:
unit: second
requests_per_unit: 500In the configuration above the domain is "mongo_cps" and we setup 2 different rate limits in the top level descriptor list. Each of the limits have the same key ("database"). They have a different value ("users", and "default"), and each of them setup a 500 request per second rate limit.
A slightly more complex example:
domain: messaging
descriptors:
# Only allow 5 marketing messages a day
- key: message_type
value: marketing
descriptors:
- key: to_number
rate_limit:
unit: day
requests_per_unit: 5
# Only allow 100 messages a day to any unique phone number
- key: to_number
rate_limit:
unit: day
requests_per_unit: 100In the preceding example, the domain is "messaging" and we setup two different scenarios that illustrate more complex functionality. First, we want to limit on marketing messages to a specific number. To enable this, we make use of nested descriptor lists. The top level descriptor is ("message_type", "marketing"). However this descriptor does not have a limit assigned so it's just a placeholder. Contained within this entry we have another descriptor list that includes an entry with key "to_number". However, notice that no value is provided. This means that the service will match against any value supplied for "to_number" and generate a unique limit. Thus, ("message_type", "marketing"), ("to_number", "2061111111") and ("message_type", "marketing"),("to_number", "2062222222") will each get 5 requests per day.
The configuration also sets up another rule without a value. This one creates an overall limit for messages sent to any particular number during a 1 day period. Thus, ("to_number", "2061111111") and ("to_number", "2062222222") both get 100 requests per day.
When calling the rate limit service, the client can specify multiple descriptors to limit on in a single call. This limits round trips and allows limiting on aggregate rule definitions. For example, using the preceding configuration, the client could send this complete request (in pseudo IDL):
RateLimitRequest:
domain: messaging
descriptor: ("message_type", "marketing"),("to_number", "2061111111")
descriptor: ("to_number", "2061111111")
And the service will rate limit against all matching rules and return an aggregate result; a logical OR of all the individual rate limit decisions.
An example to illustrate matching order.
domain: edge_proxy_per_ip
descriptors:
- key: remote_address
rate_limit:
unit: second
requests_per_unit: 10
# Black list IP
- key: remote_address
value: 50.0.0.5
rate_limit:
unit: second
requests_per_unit: 0In the preceding example, we setup a generic rate limit for individual IP addresses. The architecture's edge proxy can be configured to make a rate limit service call with the descriptor ("remote_address", "50.0.0.1") for example. This IP would get 10 requests per second as would any other IP. However, the configuration also contains a second configuration that explicitly defines a value along with the same key. If the descriptor ("remote_address", "50.0.0.5") is received, the service will attempt the most specific match possible. This means the most specific descriptor at the same level as your request. Thus, key/value is always attempted as a match before just key.
The Ratelimit service matches requests to configuration entries with the same level, i.e same number of tuples in the request's descriptor as nested levels of descriptors in the configuration file. For instance, the following request:
RateLimitRequest:
domain: example4
descriptor: ("key", "value"),("subkey", "subvalue")
Would not match the following configuration. Even though the first descriptor in the request matches the 1st level descriptor in the configuration, the request has two tuples in the descriptor.
domain: example4
descriptors:
- key: key
value: value
rate_limit:
requests_per_unit: 300
unit: secondHowever, it would match the following configuration:
domain: example4
descriptors:
- key: key
value: value
descriptors:
- key: subkey
rate_limit:
requests_per_unit: 300
unit: secondWe can also define unlimited rate limit descriptors:
domain: internal
descriptors:
- key: ldap
rate_limit:
unlimited: true
- key: azure
rate_limit:
unit: minute
requests_per_unit: 100For an unlimited descriptor, the request will not be sent to the underlying cache (Redis/Memcached), but will be quickly returned locally by the ratelimit instance. This can be useful for collecting statistics, or if one wants to define a descriptor that has no limit but the client wants to distinguish between such descriptor and one that does not exist.
The return value for unlimited descriptors will be an OK status code with the LimitRemaining field set to MaxUint32 value.
A rule using shadow_mode is useful for soft-launching rate limiting. In this example
RateLimitRequest:
domain: example6
descriptor: ("service", "auth-service"),("user", "user-a")
user-a of the auth-service would not get rate-limited regardless of the rate of requests, there would however be statistics related to the breach of the configured limit of 10 req / sec.
user-b would be limited to 20 req / sec however.
domain: example6
descriptors:
- key: service
descriptors:
- key: user
value: user-a
rate_limit:
requests_per_unit: 10
unit: second
shadow_mode: true
- key: user
value: user-b
rate_limit:
requests_per_unit: 20
unit: secondWhen the replaces keyword is used, that limit will replace any limit which has the name being replaced as its name, and the original descriptor's limit will not be affected.
In the example below, the following limits will apply:
(key_1, value_1), (user, bkthomps): 5 / sec (key_2, value_2), (user, bkthomps): 10 / sec (key_1, value_1), (key_2, value_2), (user, bkthomps): 10 / sec since the (key_1, value_1), (user, bkthomps) rule was replaced and this will not affect the 5 / sec limit that would take effect with (key_2, value_2), (user, bkthomps)
domain: example7
descriptors:
- key: key_1
value: value_1
descriptors:
- key: user
value: bkthomps
rate_limit:
name: specific_limit
requests_per_unit: 5
unit: second
- key: key_2
value: value_2
descriptors:
- key: user
value: bkthomps
rate_limit:
replaces:
- name: specific_limit
requests_per_unit: 10
unit: secondIn this example we demonstrate how a descriptor without a specified value is configured to override the default behavior and include the matched-value in the metrics.
Rate limiting configuration and tracking works as normally
(key_1, unspecified_value): 10 / sec (key_1, unspecified_value2): 10 / sec (key_1, value_1): 20 / sec
domain: example8
descriptors:
- key: key1
detailed_metric: true
rate_limit:
unit: minute
requests_per_unit: 10
- key: key1
value: value1
rate_limit:
unit: minute
requests_per_unit: 20The metrics keys will be the following:
"key1_unspecified_value" "key1_unspecified_value2" "key1_value1"
rather than the normal "key1" "key1_value1"
Value supports wildcard matching using *, which can appear at any position — trailing, middle, or multiple times. Each * matches zero or more characters.
Trailing wildcard — matches any value starting with the given prefix:
domain: example9
descriptors:
- key: key1
value: value*
rate_limit:
unit: minute
requests_per_unit: 20Matches value1, value2, valueXYZ, etc.
Middle wildcard — matches values with a fixed prefix and suffix:
domain: example9
descriptors:
- key: path
value: /api/*/action
rate_limit:
unit: minute
requests_per_unit: 20Matches /api/123/action, /api/user-id/action. Does not match /api/123/other.
Multiple wildcards — each * matches an independent segment, in order:
domain: example9
descriptors:
- key: route
value: /api/*/resource/*/action
rate_limit:
unit: minute
requests_per_unit: 20Matches /api/v1/resource/123/action, /api/v2/resource/456/action.
Using value_to_metric: true to include descriptor values in metrics when values are not explicitly defined in the configuration:
domain: example10
descriptors:
- key: route
value_to_metric: true
descriptors:
- key: http_method
value_to_metric: true
descriptors:
- key: subject_id
rate_limit:
unit: minute
requests_per_unit: 60With this configuration, requests with different runtime values for route and http_method will generate separate metrics:
Request: route=api, http_method=GET, subject_id=123
Metric key: example10.route_api.http_method_GET.subject_id
Request: route=web, http_method=POST, subject_id=456
Metric key: example10.route_web.http_method_POST.subject_id
Without value_to_metric: true, both requests would use the same metric key: example10.route.http_method.subject_id.
When combined with wildcard matching, the full runtime value is included:
domain: example10_wildcard
descriptors:
- key: user
value_to_metric: true
descriptors:
- key: action
value: read*
value_to_metric: true
descriptors:
- key: resource
rate_limit:
unit: minute
requests_per_unit: 100Note: When detailed_metric: true is set on a descriptor, it takes precedence and value_to_metric is ignored for that descriptor.
Using share_threshold: true to share rate limits across wildcard matches:
domain: example11
descriptors:
# With share_threshold: true, all files/* matches share the same threshold
- key: files
value: files/*
share_threshold: true
rate_limit:
unit: hour
requests_per_unit: 10
# Without share_threshold, each files_no_share/* match has its own isolated threshold
- key: files_no_share
value: files_no_share/*
share_threshold: false
rate_limit:
unit: hour
requests_per_unit: 10With this configuration:
Combining share_threshold with value_to_metric:
domain: example11_metrics
descriptors:
- key: route
value: api/*
share_threshold: true
value_to_metric: true
descriptors:
- key: method
rate_limit:
unit: minute
requests_per_unit: 60This reflects that all api/* routes share the same threshold, while still providing visibility into which API routes are being accessed.
Rate limit service supports following configuration loading methods. You can define which methods to use by configuring environment variable CONFIG_TYPE.
| Config Loading Method | Value for Environment Variable CONFIG_TYPE |
|---|---|
| File Based Configuration Loading | FILE (Default) |
| xDS Server Based Configuration Loading | GRPC_XDS_SOTW |
When the environment variable FORCE_START_WITHOUT_INITIAL_CONFIG set to false, the Rate limit service will wait for initial rate limit configuration before starting the server (gRPC, Rest server endpoints). When set to true the server will start even without initial configuration.
The Ratelimit service uses a library written by Lyft called goruntime to do configuration loading. Goruntime monitors a designated path, and watches for symlink swaps to files in the directory tree to reload configuration files.
The path to watch can be configured via the settings package with the following environment variables:
RUNTIME_ROOT default:"/srv/runtime_data/current" RUNTIME_SUBDIRECTORY RUNTIME_APPDIRECTORY default:"config" RUNTIME_IGNOREDOTFILES default:"false"
Configuration files are loaded from RUNTIME_ROOT/RUNTIME_SUBDIRECTORY/RUNTIME_APPDIRECTORY/*.yaml
There are two methods for triggering a configuration reload:
The former is the default behavior. To use the latter method, set the RUNTIME_WATCH_ROOT environment variable to false.
The following filesystem operations on configuration files inside RUNTIME_ROOT/RUNTIME_SUBDIRECTORY/RUNTIME_APPDIRECTORY/ will force a reload of all config files:
For more information on how runtime works you can read its README.
By default it is not possible to define multiple configuration files within RUNTIME_SUBDIRECTORY referencing the same domain. To enable this behavior set MERGE_DOMAIN_CONFIG to true.
xDS Management Server is a gRPC server which implements the Aggregated Discovery Service (ADS). The xDS Management server serves Discovery Response with Ratelimit Configuration Resources and with Type URL "type.googleapis.com/ratelimit.config.ratelimit.v3.RateLimitConfig".
The xDS client in the Rate limit service configure Rate limit service with the provided configuration. In case of connection failures, the xDS Client retries the connection to the xDS server with exponential backoff and the backoff parameters are configurable.
The followings are the gRPC connection options.
For more information on xDS protocol please refer to the envoy proxy documentation.
You can refer to the sample xDS configuration management server.
The xDS server for listening for configuration can be set via settings package with the following environment variables:
CONFIG_GRPC_XDS_NODE_ID default:"default" CONFIG_GRPC_XDS_SERVER_URL default:"localhost:18000" CONFIG_GRPC_XDS_SERVER_CONNECT_RETRY_INTERVAL default:"3s"
As well Ratelimit supports TLS connections, these can be configured using the following environment variables:
When using xDS you can configure extra headers that will be added to GRPC requests to the xDS Management server. Extra headers can be useful for providing additional authorization information. This can be configured using the following environment variable:
CONFIG_GRPC_XDS_CLIENT_ADDITIONAL_HEADERS - set to "<k1:v1>,<k2:v2>" to add multiple headers to GRPC requests.
A centralized log collection system works better with logs in json format. JSON format avoids the need for custom parsing rules. The Ratelimit service produces logs in a text format by default. For Example:
time="2020-09-10T17:22:35Z" level=debug msg="loading domain: messaging"
time="2020-09-10T17:22:35Z" level=debug msg="loading descriptor: key=messaging.message_type_marketing"
time="2020-09-10T17:22:35Z" level=debug msg="loading descriptor: key=messaging.message_type_marketing.to_number ratelimit={requests_per_unit=5, unit=DAY}"
time="2020-09-10T17:22:35Z" level=debug msg="loading descriptor: key=messaging.to_number ratelimit={requests_per_unit=100, unit=DAY}"
time="2020-09-10T17:21:55Z" level=warning msg="Listening for debug on ':6070'"
time="2020-09-10T17:21:55Z" level=warning msg="Listening for HTTP on ':8080'"
time="2020-09-10T17:21:55Z" level=debug msg="waiting for runtime update"
time="2020-09-10T17:21:55Z" level=warning msg="Listening for gRPC on ':8081'"
JSON Log format can be configured using the following environment variables:
LOG_FORMAT=json
Output example:
{"@message":"loading domain: messaging","@timestamp":"2020-09-10T17:22:44.926010192Z","level":"debug"}
{"@message":"loading descriptor: key=messaging.message_type_marketing","@timestamp":"2020-09-10T17:22:44.926019315Z","level":"debug"}
{"@message":"loading descriptor: key=messaging.message_type_marketing.to_number ratelimit={requests_per_unit=5, unit=DAY}","@timestamp":"2020-09-10T17:22:44.926037174Z","level":"debug"}
{"@message":"loading descriptor: key=messaging.to_number ratelimit={requests_per_unit=100, unit=DAY}","@timestamp":"2020-09-10T17:22:44.926048993Z","level":"debug"}
{"@message":"Listening for debug on ':6070'","@timestamp":"2020-09-10T17:22:44.926113905Z","level":"warning"}
{"@message":"Listening for gRPC on ':8081'","@timestamp":"2020-09-10T17:22:44.926182006Z","level":"warning"}
{"@message":"Listening for HTTP on ':8080'","@timestamp":"2020-09-10T17:22:44.926227031Z","level":"warning"}
{"@message":"waiting for runtime update","@timestamp":"2020-09-10T17:22:44.926267808Z","level":"debug"}
Client-side GRPC DNS re-resolution in scenarios with auto scaling enabled might not work as expected and the current workaround is to configure connection keepalive on server-side. The behavior can be fixed by configuring the following env variables for the ratelimit server:
Health check status is determined internally by individual components. Currently, we have three components that determine the overall health status of the rate limit service. Each of the individual component's health needs to be healthy for the overall to report healthy. Some components may be turned OFF via configurations so overall health is not effected by that component's health status.
Health check can be configured to check if rate-limit configurations are loaded using the following environment variable.
HEALTHY_WITH_AT_LEAST_ONE_CONFIG_LOADED default:"false"`
If HEALTHY_WITH_AT_LEAST_ONE_CONFIG_LOADED is enabled then health check will start as unhealthy and becomes healthy if it detects at least one domain is loaded with the config. If it detects no config again then it will change to unhealthy.
By default the ratelimit gRPC server binds to 0.0.0.0:8081. To change this set GRPC_HOST and/or GRPC_PORT. If you want to run the server on a unix domain socket then set GRPC_UDS, e.g. GRPC_UDS=/<dir>/ratelimit.sock and leave GRPC_HOST and GRPC_PORT unmodified.
For information on the fields of a Ratelimit gRPC request please read the information on the RateLimitRequest message type in the Ratelimit proto file.
Each descriptor entry may set the is_negative_hits field. When it is true, the descriptor's hits_addend is subtracted from the rate limit counter instead of being added to it, effectively refunding previously consumed capacity.
Negative-hit behavior:
The gRPC client will interact with ratelimit server and tell you if the requests are over limit.
go run main.go -domain test \ -descriptors name=foo,age=14 -descriptors name=bar,age=18
There is a global shadow-mode which can make it easier to introduce rate limiting into an existing service landscape. It will override whatever result is returned by the regular rate limiting process.
The global shadow mode is configured with an environment variable
Setting environment variable SHADOW_MODE to true will enable the feature.
There is an additional service-level statistics generated that will increment whenever the global shadow mode has overridden a rate limiting result.
The rate limit service generates various statistics for each configured rate limit rule that will be useful for end users both for visibility and for setting alarms. Ratelimit uses gostats as its statistics library. Please refer to gostats' documentation for more information on the library.
Statistics default to using StatsD and configured via the env vars from gostats.
To output statistics to stdout instead, set env var USE_STATSD to false
Configure statistics output frequency with STATS_FLUSH_INTERVAL, where the type is time.Duration, e.g. 10s is the default value.
To disable statistics entirely, set env var DISABLE_STATS to true
To sanitize . characters in descriptor keys and values before they are published as metrics, set env var SANITIZE_DESCRIPTOR_METRIC_DOTS to true (default false). When enabled, each . in a descriptor key or value is replaced with _ in the emitted metric name. Because . is the statsd metric-hierarchy separator, a dotted value (e.g. a gRPC path like /helloworld.Greeter/SayHello) would otherwise inject unintended extra hierarchy levels — which breaks the Prometheus statsd_exporter metric-name-to-label mapping. This only affects metric names; rate limit matching (which keys off the raw descriptor) is unchanged. It is disabled by default for backward compatibility.
Rate Limit Statistic Path:
ratelimit.service.rate_limit.DOMAIN.KEY_VALUE.STAT
DOMAIN:
KEY_VALUE:
The default mode is that the value-part is omitted if the rule that matches is a descriptor without a value. Specifying the detailed_metric configuration parameter changes this behavior and creates a unique metric even in this situation.
STAT:
To use a custom near_limit ratio threshold, you can specify with NEAR_LIMIT_RATIO environment variable. It defaults to 0.8 (0-1 scale). These are examples of generated stats for some configured rate limit rules from the above examples:
ratelimit.service.rate_limit.mongo_cps.database_default.over_limit: 0 ratelimit.service.rate_limit.mongo_cps.database_default.total_hits: 2846 ratelimit.service.rate_limit.mongo_cps.database_users.over_limit: 0 ratelimit.service.rate_limit.mongo_cps.database_users.total_hits: 2939 ratelimit.service.rate_limit.messaging.message_type_marketing.to_number.over_limit: 0 ratelimit.service.rate_limit.messaging.message_type_marketing.to_number.total_hits: 0 ratelimit.service.rate_limit.messaging.auth-service.over_limit.total_hits: 1 ratelimit.service.rate_limit.messaging.auth-service.over_limit.over_limit: 1 ratelimit.service.rate_limit.messaging.auth-service.over_limit.shadow_mode: 1
To enable dogstatsd integration set:
dogstatsd also enables so called mogrifiers which can convert from traditional stats tags into a combination of stat name and tags.
To enable mogrifiers, set a comma-separated list of them in DOG_STATSD_MOGRIFIERS.
e.g. USE_DOG_STATSD_MOGRIFIERS: FOO,BAR
For each mogrifier, define variables that declare the mogrification
Variables within mogrifiers are strings such as $1, $2, $3 which can be used to reference a match group from the regex pattern.
In the example below we will set mogrifier DOMAIN to adjust some.original.metric.TAG to some.original.metric with tag domain:TAG
First enable a single mogrifier:
Then, declare the rules for the DOMAIN modifier:
Let's also set another mogrifier which outputs the hits metrics with a domain and descriptor tag
First, enable an extra mogrifier:
Then, declare additional rules for the DESCRIPTOR mogrifier
To enable Prometheus integration set:
Define the mapping from statsd to prometheus metrics in a YAML file. Find more information about the mapping in the Metric Mapping and Configuration. The default setting is:
mappings: # Requires statsd exporter >= v0.6.0 since it uses the "drop" action.
- match: "ratelimit.service.rate_limit.*.*.near_limit"
name: "ratelimit_service_rate_limit_near_limit"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
- match: "ratelimit.service.rate_limit.*.*.over_limit"
name: "ratelimit_service_rate_limit_over_limit"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
- match: "ratelimit.service.rate_limit.*.*.total_hits"
name: "ratelimit_service_rate_limit_total_hits"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
- match: "ratelimit.service.rate_limit.*.*.within_limit"
name: "ratelimit_service_rate_limit_within_limit"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
- match: "ratelimit.service.rate_limit.*.*.*.near_limit"
name: "ratelimit_service_rate_limit_near_limit"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"
- match: "ratelimit.service.rate_limit.*.*.*.over_limit"
name: "ratelimit_service_rate_limit_over_limit"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"
- match: "ratelimit.service.rate_limit.*.*.*.total_hits"
name: "ratelimit_service_rate_limit_total_hits"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"
- match: "ratelimit.service.rate_limit.*.*.*.within_limit"
name: "ratelimit_service_rate_limit_within_limit"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"
- match: "ratelimit.service.call.should_rate_limit.*"
name: "ratelimit_service_should_rate_limit_error"
match_metric_type: counter
labels:
err_type: "$1"
- match: "ratelimit_server.*.total_requests"
name: "ratelimit_service_total_requests"
match_metric_type: counter
labels:
grpc_method: "$1"
- match: "ratelimit_server.*.response_time"
name: "ratelimit_service_response_time_seconds"
timer_type: histogram
scale: 0.001
labels:
grpc_method: "$1"
- match: "ratelimit.service.config_load_success"
name: "ratelimit_service_config_load_success"
match_metric_type: counter
- match: "ratelimit.service.config_load_error"
name: "ratelimit_service_config_load_error"
match_metric_type: counter
- match: "ratelimit.service.rate_limit.*.*.*.shadow_mode"
name: "ratelimit_service_rate_limit_shadow_mode"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"The ratelimit service listens to HTTP 1.1 (by default on port 8080) with two endpoints:
Takes an HTTP POST with a JSON body of the form e.g.
{
"domain": "dummy",
"descriptors": [
{ "entries": [{ "key": "one_per_day", "value": "something" }] }
]
}The service will return an http 200 if this request is allowed (if no ratelimits exceeded) or 429 if one or more ratelimits were exceeded.
The response is a RateLimitResponse encoded with proto3-to-json mapping:
{
"overallCode": "OVER_LIMIT",
"statuses": [
{
"code": "OVER_LIMIT",
"currentLimit": {
"requestsPerUnit": 1,
"unit": "MINUTE"
}
},
{
"code": "OK",
"currentLimit": {
"requestsPerUnit": 2,
"unit": "MINUTE"
},
"limitRemaining": 1
}
]
}The debug port can be used to interact with the running process.
$ curl 0:6070/ /debug/pprof/: root of various pprof endpoints. hit for help. /rlconfig: print out the currently loaded configuration for debugging /stats: print out stats
You can specify the debug server address with the DEBUG_HOST and DEBUG_PORT environment variables. They currently default to 0.0.0.0 and 6070 respectively.
Ratelimit optionally uses freecache as its local caching layer, which stores the over-the-limit cache keys, and thus avoids reading the redis cache again for the already over-the-limit keys. The local cache size can be configured via LocalCacheSizeInBytes in the settings. If LocalCacheSizeInBytes is 0, local cache is disabled.
Ratelimit uses Redis as its caching layer. Ratelimit supports two operation modes:
As well Ratelimit supports TLS connections and authentication. These can be configured using the following environment variables:
For controlling the behavior of cache key incrementation when any of them is already over the limit, you can use the following configuration:
STOP_CACHE_KEY_INCREMENT_WHEN_OVERLIMIT is useful when multiple descriptors are included in a single request. Setting this to true can prevent the incrementation of other descriptors' counters if any of the descriptors is already over the limit.
Ratelimit supports different types of redis deployments:
The deployment type can be specified with the REDIS_TYPE / REDIS_PERSECOND_TYPE environment variables. Depending on the type defined, the REDIS_URL and REDIS_PERSECOND_URL are expected to have the following formats:
Controls the maximum duration for Redis connection establishment, read operations, and write operations.
Controls what happens when all connections in the pool are in use and a new request arrives.
By default, for each request, ratelimit will pick up a connection from pool, write multiple redis commands in a single write then reads their responses in a single read. This reduces network delay.
For high throughput scenarios, ratelimit supports write buffering via radix v4's WriteFlushInterval. It can be configured using the following environment variables:
Write buffering is disabled by default (window = 0). For optimal performance, set REDIS_PIPELINE_WINDOW to 150us-500us depending on your latency requirements and load patterns.
To configure one Redis instance use the following environment variables:
This setup will use the same Redis server for all limits.
To configure two Redis instances use the following environment variables:
This setup will use the Redis server configured with the _PERSECOND_ vars for per second limits, and the other Redis server for all other limits.
To configure whether to return health check failure if there is no active redis connection
When a Redis master is failed over by repointing an address at the new master (a Kubernetes Service, DNS, or a proxy — common with Redis-compatible servers like Dragonfly, or Redis deployments without Sentinel), the demoted master keeps already-established connections open. Pooled connections are only discarded on IO errors, so every write on those stale connections keeps failing with READONLY You can't write against a read only replica. until the process restarts.
Setting REDIS_CLOSE_CONNECTION_ON_READONLY_ERROR to "true" closes a pooled connection whenever a command on it fails with a READONLY error reply, so the pool reconnects through the configured address and reaches the current master. The failing command still returns its error to the caller; only the connection handling changes. Applies to both the main and the per-second Redis clients.
By default, a unit: month rate limit uses a fixed 30-day window counted from the Unix epoch, which does not line up with real calendar months (it drifts, and treats every month as 30 days regardless of its actual length).
Setting USE_CALENDAR_MONTH_RATE_LIMIT to "true" switches MONTH limits to a true calendar month window instead: the cache key bucket, TTL/expiration, and reported reset time all cover the 1st through the last day of the month (UTC). This is opt-in because it changes when existing MONTH limits reset and is therefore not enabled by default.
Experimental Memcache support has been added as an alternative to Redis in v1.5.
To configure a Memcache instance use the following environment variables instead of the Redis variables:
With memcache mode increments will happen asynchronously, so it's technically possible for a client to exceed quota briefly if multiple requests happen at exactly the same time.
Note that Memcache has a max key length of 250 characters, so operations referencing very long descriptors will fail. Descriptors sent to Memcache should not contain whitespaces or control characters.
When using multiple memcache nodes in MEMCACHE_HOST_PORT=, one should provide the identical list of memcache nodes to all ratelimiter instances to ensure that a particular cache key is always hashed to the same memcache node.
Ratelimit service can be configured to return custom headers with the ratelimit information. It will populate the response_headers_to_add as part of the RateLimitResponse.
The following environment variables control the custom response feature:
The following environment variables control the custom request header feature. When enabled, Envoy injects these headers into the forwarded request before it reaches the upstream service, allowing upstream services to inspect rate limit state and make per-request routing decisions (e.g. skip a hot path when quota is exhausted) without the request being blocked.
Ratelimit service supports exporting spans in OLTP format. See OpenTelemetry for more information.
The following environment variables control the tracing feature:
You may use the following commands to quickly setup a openTelemetry collector together with a Jaeger all-in-one binary for quickstart:
docker run --name otlp -d -p 4318 -p 4317 -v examples/otlp-collector:/tmp/otlp-collector otel/opentelemetry-collector:0.48.0 -- --config /tmp/otlp-collector/config.yaml
otelcol-contrib --config examples/otlp-collector/config.yaml
docker run -d --name jaeger -p 16686:16686 -p 14250:14250 jaegertracing/all-in-one:1.33Ratelimit supports TLS for it's gRPC endpoint.
The following environment variables control the TLS feature:
Ratelimit uses goruntime to watch the TLS certificate and key and will hot reload them on changes.
Ratelimit supports mTLS when Envoy sends requests to the service.
TLS must be enabled on the gRPC endpoint in order for mTLS to work see TLS.
The following variables can be set to enable mTLS on the Ratelimit service.
In the envoy config use, add the transport_socket section to the ratelimit service cluster config
"name": "ratelimit"
"transport_socket":
"name": "envoy.transport_sockets.tls"
"typed_config":
"@type": "type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext"
"common_tls_context":
"tls_certificates":
- "certificate_chain":
"filename": "/opt/envoy/tls/ratelimit-client-cert.pem"
"private_key":
"filename": "/opt/envoy/tls/ratelimit-client-key.pem"
"validation_context":
"match_subject_alt_names":
- "exact": "ratelimit.server.dnsname"
"trusted_ca":
"filename": "/opt/envoy/tls/ratelimit-server-ca.pem"| Back | FazBrowse Home | New Git URL |