Skip to content

Commit d7bafe8

Browse files
Merge pull request #946 from mulesoft/W-20012804-pdk-1.5-data-storage-gr
W-20012804 pdk 1.5 rate limiting gr
2 parents 5e84268 + ddb3280 commit d7bafe8

6 files changed

+89
-1
lines changed

pdk/1.5/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*** xref:policies-pdk-configure-features-libraries.adoc[]
2121
*** xref:policies-pdk-configure-features-caching.adoc[]
2222
*** xref:policies-pdk-configure-features-metadata.adoc[]
23+
*** xref:policies-pdk-configure-features-rate-limiting.adoc[]
2324
*** xref:policies-pdk-configure-features-streamproperties.adoc[]
2425
*** xref:policies-pdk-configure-features-authentication.adoc[]
2526
*** xref:policies-pdk-configure-features-timer.adoc[]

pdk/1.5/modules/ROOT/pages/policies-pdk-configure-features-inject-parameters.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Flex Gateway Policy Development Kit (PDK) provides the following parameters that
1212
* `CacheBuilder`: Provides the caching features of the policy. For more information about caching, see xref:policies-pdk-configure-features-caching.adoc[].
1313
* `StreamProperties`: Provides a structure to share properties with other policies that process the same request. For more information about sharing information between policies, see xref:policies-pdk-configure-features-streamproperties.adoc[].
1414
* `PolicyViolations``: Provides a mechanism to report policy violations for your monitoring dashboards. For more information about policy violations, see xref:policies-pdk-configure-features-violations.adoc[].
15+
* `RateLimitBuilder`: Provides rate limiting functionality to control request rates. For more information about rate limiting, see xref:policies-pdk-configure-features-rate-limiting.adoc[].
1516
1617
You can also directly inject the `HttpClient` and `StreamProperties` parameter into the `on_request` or `on_response` wrapped functions. For example, if you need to perform an HTTP call inside the `on_request` function, inject `HTTPClient` directly into that function.
1718

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
= Rate Limiting Requests
2+
ifndef::env-site,env-github[]
3+
include::_attributes.adoc[]
4+
endif::[]
5+
:imagesdir: ../assets/images
6+
7+
Flex Gateway Policy Development Kit (PDK) provides rate limiting functionality to control request rates.
8+
9+
Custom policies support both single Flex Replica and multi-replica deployments. For multi-replica deployments, you must configure shared storage. To configure shared storage, see:
10+
11+
* xref:gateway::flex-conn-shared-storage-config.adoc[]
12+
* xref:gateway::flex-local-shared-storage-config.adoc[]
13+
14+
== Inject the `RateLimitBuilder`
15+
16+
To enable rate limiting functionality, you need to inject the `RateLimitBuilder` in your custom policy's entrypoint and configure it for either local (single-replica) or clustered (multi-replica) mode:
17+
18+
[source,Rust]
19+
----
20+
// buckets example configuration
21+
let buckets = vec![
22+
("api", vec![Tier { requests: 100, period_in_millis: 60000 }]), // 100 requests per minute
23+
("user", vec![Tier { requests: 10, period_in_millis: 30000 }]), // 10 requests per 30 seconds
24+
];
25+
26+
// timer example
27+
let timer = clock.period(Duration::from_millis(100)); // 100ms intervals for rate limit sync
28+
29+
// for local mode (single-instance)
30+
let builder = rate_limit_builder
31+
.new(builder_id);
32+
let rate_limiter = builder.buckets(buckets).build()?;
33+
34+
// for clustered mode with shared storage (multi-instance)
35+
let builder = rate_limit_builder
36+
.new(builder_id)
37+
.clustered(Rc::new(timer))
38+
.shared();
39+
let rate_limiter = builder.buckets(buckets).build()?;
40+
----
41+
42+
The `RateLimitBuilder` struct provides the following methods to configure the rate limits:
43+
44+
* `builder_id`: a string identifier for the rate limiter instance (required)
45+
* `buckets`: rate limit tiers configuration with requests and time windows (optional - defaults to api instance configuration if unspecified)
46+
* `timer`: a periodic timer for rate limit sync (required for clustered mode)
47+
* `clustered`: Enables the rate limiter to use distributed storage backends. Without it, the rate limiter uses in-memory storage.
48+
* `shared`: Enables the rate limiter to share state across different policy instances.
49+
50+
== Check Requests Against Rate Limits
51+
52+
After creating the rate limiter instance, use the `is_allowed` method to check if requests are allowed with support for multiple independent rate limit configurations:
53+
54+
[source,Rust]
55+
----
56+
// Check if request is allowed for a specific group and client key
57+
match rate_limiter.is_allowed(group, &client_key, request_amount).await {
58+
Ok(RateLimitResult::Allowed(_)) => Flow::Continue(()),
59+
Ok(RateLimitResult::TooManyRequests(_)) => Flow::Break(Response::new(429)),
60+
Err(e) => Flow::Break(Response::new(503)),
61+
}
62+
----
63+
64+
== Rate Limiting Configuration Examples
65+
66+
PDK provides the https://github.com/mulesoft/pdk-custom-policy-examples/tree/{template-policies-url-ver-var}/multi-instance-rate-limiting[Multi-Instance Rate Limiting Example policy] to demonstrate how to configure rate limiting in Rust code.
67+
68+
Within the example policy, see these functions for further configuration details:
69+
70+
* https://github.com/mulesoft/pdk-custom-policy-examples/tree/{template-policies-url-ver-var}/multi-instance-rate-limiting/src/lib.rs#L118[`entrypoint` function] for how to configure the `RateLimitBuilder`.
71+
72+
* https://github.com/mulesoft/pdk-custom-policy-examples/tree/{template-policies-url-ver-var}/multi-instance-rate-limiting/src/lib.rs#L54[`request_filter` function] for how the policy applies rate limits to incoming requests.
73+
74+
== Redis Shared Storage Configuration Example
75+
76+
For an example of how to configure Redis shared storage for testing, see the example https://github.com/mulesoft/pdk-custom-policy-examples/blob/{template-policies-url-ver-var}/multi-instance-rate-limiting/playground/docker-compose.yaml[`playground/docker-compose.yaml`] file.
77+
78+
79+
80+
== See Also
81+
82+
* xref:policies-pdk-configure-features-timer.adoc[]
83+
* xref:policies-pdk-configure-features-share-data.adoc[]

pdk/1.5/modules/ROOT/pages/policies-pdk-configure-features-share-data.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ async fn response_filter(state: ResponseState, request_data: RequestData<String>
5151
== See Also
5252

5353
* xref:policies-pdk-configure-features.adoc[]
54+
* xref:policies-pdk-configure-features-rate-limiting.adoc[]

pdk/1.5/modules/ROOT/pages/policies-pdk-configure-features-timer.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,5 @@ NOTE: To view an example policy project that synchronizes asynchronous periodic
181181

182182
== See Also
183183

184-
* xref:policies-pdk-configure-features-inject-parameters.adoc[]
184+
* xref:policies-pdk-configure-features-inject-parameters.adoc[]
185+
* xref:policies-pdk-configure-features-rate-limiting.adoc[]

pdk/1.5/modules/ROOT/pages/policies-pdk-configure-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ PDK provides code examples for the following policy features. Review <<policy-te
191191
* xref:policies-pdk-configure-features-libraries.adoc[]
192192
* xref:policies-pdk-configure-features-caching.adoc[]
193193
* xref:policies-pdk-configure-features-metadata.adoc[]
194+
* xref:policies-pdk-configure-features-rate-limiting.adoc[]
194195
* xref:policies-pdk-configure-features-streamproperties.adoc[]
195196
* xref:policies-pdk-configure-features-authentication.adoc[]
196197
* xref:policies-pdk-configure-features-timer.adoc[]

0 commit comments

Comments
 (0)