You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Proposal] Share Threshold for Wildcard Rate Limiting (#1016)
* Add share_threshold to make wild card values can share rate limit threshold
Signed-off-by: Nam Dang <xuannam230201@gmail.com>
* Implement lazy initilization based on reviews
Signed-off-by: Nam Dang <xuannam230201@gmail.com>
---------
Signed-off-by: Nam Dang <xuannam230201@gmail.com>
Copy file name to clipboardExpand all lines: README.md
+69Lines changed: 69 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,7 @@
22
22
-[ShadowMode](#shadowmode)
23
23
-[Including detailed metrics for unspecified values](#including-detailed-metrics-for-unspecified-values)
24
24
-[Including descriptor values in metrics](#including-descriptor-values-in-metrics)
25
+
-[Sharing thresholds for wildcard matches](#sharing-thresholds-for-wildcard-matches)
25
26
-[Examples](#examples)
26
27
-[Example 1](#example-1)
27
28
-[Example 2](#example-2)
@@ -33,6 +34,7 @@
33
34
-[Example 8](#example-8)
34
35
-[Example 9](#example-9)
35
36
-[Example 10](#example-10)
37
+
-[Example 11](#example-11)
36
38
-[Loading Configuration](#loading-configuration)
37
39
-[File Based Configuration Loading](#file-based-configuration-loading)
38
40
-[xDS Management Server Based Configuration Loading](#xds-management-server-based-configuration-loading)
@@ -285,6 +287,7 @@ descriptors:
285
287
shadow_mode: (optional)
286
288
detailed_metric: (optional)
287
289
value_to_metric: (optional)
290
+
share_threshold: (optional)
288
291
descriptors: (optional block)
289
292
- ... (nested repetition of above)
290
293
```
@@ -347,6 +350,20 @@ Setting `value_to_metric: true` (default: `false`) for a descriptor will include
347
350
348
351
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.
349
352
353
+
### Sharing thresholds for wildcard matches
354
+
355
+
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.
356
+
357
+
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.
358
+
359
+
**Important notes:**
360
+
361
+
- `share_threshold`can only be used with wildcard values (values ending with `*`)
362
+
- When `share_threshold: true` is enabled, all matching values share the same cache key and rate limit counter
363
+
- When `share_threshold: false` (or not set), each matching value has its own isolated threshold
364
+
- When combined with `value_to_metric: true`, the metric key includes the wildcard prefix (the part before `*`) instead of the full runtime value, to reflect that values are sharing a threshold
365
+
- When combined with `detailed_metric: true`, the metric key also includes the wildcard prefix for entries with `share_threshold` enabled
366
+
350
367
### Examples
351
368
352
369
#### Example 1
@@ -692,6 +709,58 @@ descriptors:
692
709
693
710
Note: When `detailed_metric: true` is set on a descriptor, it takes precedence and `value_to_metric` is ignored for that descriptor.
694
711
712
+
#### Example 11
713
+
714
+
Using `share_threshold: true` to share rate limits across wildcard matches:
715
+
716
+
```yaml
717
+
domain: example11
718
+
descriptors:
719
+
# With share_threshold: true, all files/* matches share the same threshold
720
+
- key: files
721
+
value: files/*
722
+
share_threshold: true
723
+
rate_limit:
724
+
unit: hour
725
+
requests_per_unit: 10
726
+
727
+
# Without share_threshold, each files_no_share/* match has its own isolated threshold
728
+
- key: files_no_share
729
+
value: files_no_share/*
730
+
share_threshold: false
731
+
rate_limit:
732
+
unit: hour
733
+
requests_per_unit: 10
734
+
```
735
+
736
+
With this configuration:
737
+
738
+
- Requests for `files/a.pdf`, `files/b.csv`, and `files/c.txt` all share the same threshold of 10 requests per hour
739
+
- If 5 requests are made for `files/a.pdf` and 5 requests for `files/b.csv`, a request for `files/c.txt` will be rate limited (OVER_LIMIT) because the shared threshold of 10 has been reached
740
+
- Requests for `files_no_share/a.pdf` and `files_no_share/b.csv` each have their own isolated threshold of 10 requests per hour
741
+
- If 10 requests are made for `files_no_share/a.pdf` (exhausting its quota), requests for `files_no_share/b.csv` will still be allowed (up to 10 requests)
742
+
743
+
Combining `share_threshold` with `value_to_metric`:
744
+
745
+
```yaml
746
+
domain: example11_metrics
747
+
descriptors:
748
+
- key: route
749
+
value: api/*
750
+
share_threshold: true
751
+
value_to_metric: true
752
+
descriptors:
753
+
- key: method
754
+
rate_limit:
755
+
unit: minute
756
+
requests_per_unit: 60
757
+
```
758
+
759
+
- Request: `route=api/v1`, `method=GET`
760
+
- Metric key: `example11_metrics.route_api.method_GET`(includes the wildcard prefix `api` instead of the full value `api/v1`)
761
+
762
+
This reflects that all `api/*` routes share the same threshold, while still providing visibility into which API routes are being accessed.
763
+
695
764
## Loading Configuration
696
765
697
766
Rate limit service supports following configuration loading methods. You can define which methods to use by configuring environment variable `CONFIG_TYPE`.
0 commit comments