Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pdk/1.5/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*** xref:policies-pdk-configure-features-libraries.adoc[]
*** xref:policies-pdk-configure-features-caching.adoc[]
*** xref:policies-pdk-configure-features-metadata.adoc[]
*** xref:policies-pdk-configure-features-rate-limiting.adoc[]
*** xref:policies-pdk-configure-features-streamproperties.adoc[]
*** xref:policies-pdk-configure-features-authentication.adoc[]
*** xref:policies-pdk-configure-features-timer.adoc[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Flex Gateway Policy Development Kit (PDK) provides the following parameters that
* `CacheBuilder`: Provides the caching features of the policy. For more information about caching, see xref:policies-pdk-configure-features-caching.adoc[].
* `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[].
* `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[].
* `RateLimitBuilder`: Provides rate limiting functionality to control request rates. For more information about rate limiting, see xref:policies-pdk-configure-features-rate-limiting.adoc[].

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.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
= Rate Limiting Requests
ifndef::env-site,env-github[]
include::_attributes.adoc[]
endif::[]
:imagesdir: ../assets/images

Flex Gateway Policy Development Kit (PDK) provides rate limiting functionality to control request rates.

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:

* xref:gateway::flex-conn-shared-storage-config.adoc[]
* xref:gateway::flex-local-shared-storage-config.adoc[]

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:

[source,Rust]
----
// buckets example configuration
let buckets = vec![
("api", vec![Tier { requests: 100, period_in_millis: 60000 }]), // 100 requests per minute
("user", vec![Tier { requests: 10, period_in_millis: 30000 }]), // 10 requests per 30 seconds
];

// timer example
let timer = clock.period(Duration::from_millis(100)); // 100ms intervals for rate limit sync

// for local mode (single-instance)
let builder = rate_limit_builder
.new(builder_id);
let rate_limiter = builder.buckets(buckets).build()?;

// for clustered mode with shared storage (multi-instance)
let builder = rate_limit_builder
.new(builder_id)
.clustered(Rc::new(timer))
.shared();
let rate_limiter = builder.buckets(buckets).build()?;
----

The `RateLimitBuilder` struct provides the following methods to configure the rate limits:

* `builder_id`: a string identifier for the rate limiter instance (required)
* `buckets`: rate limit tiers configuration with requests and time windows (optional - defaults to api instance configuration if unspecified)
* `timer`: a periodic timer for rate limit sync (required for clustered mode)
* `clustered`: Enables the rate limiter to use distributed storage backends. Without it, the rate limiter uses in-memory storage.
* `shared`: Enables the rate limiter to share state across different policy instances.

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:

[source,Rust]
----
// Check if request is allowed for a specific group and client key
match rate_limiter.is_allowed(group, &client_key, request_amount).await {
Ok(RateLimitResult::Allowed(_)) => Flow::Continue(()),
Ok(RateLimitResult::TooManyRequests(_)) => Flow::Break(Response::new(429)),
Err(e) => Flow::Break(Response::new(503)),
}
----

== Examples

Example of rate limiting configuration:
https://github.com/mulesoft/pdk-custom-policy-examples/blob/70486b80350ef0c5c47ac3ca020da16565ab1f82/multi-instance-rate-limiting/src/lib.rs#L118[]

Example of multiple rate limit request filters:
https://github.com/mulesoft/pdk-custom-policy-examples/blob/70486b80350ef0c5c47ac3ca020da16565ab1f82/multi-instance-rate-limiting/src/lib.rs#L54[]

Example of Redis setup for clustered mode:
https://github.com/mulesoft/pdk-custom-policy-examples/blob/support/1.5.x/multi-instance-rate-limiting/playground/docker-compose.yaml[]

Complete example of Rate Limiting usage:
https://github.com/mulesoft/pdk-custom-policy-examples/blob/support/1.5.x/multi-instance-rate-limiting[]

== See Also

* xref:policies-pdk-configure-features-timer.adoc[]
* xref:policies-pdk-configure-features-share-data.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ async fn response_filter(state: ResponseState, request_data: RequestData<String>
== See Also

* xref:policies-pdk-configure-features.adoc[]
* xref:policies-pdk-configure-features-rate-limiting.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,5 @@ NOTE: To view an example policy project that synchronizes asynchronous periodic

== See Also

* xref:policies-pdk-configure-features-inject-parameters.adoc[]
* xref:policies-pdk-configure-features-inject-parameters.adoc[]
* xref:policies-pdk-configure-features-rate-limiting.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ PDK provides code examples for the following policy features. Review <<policy-te
* xref:policies-pdk-configure-features-libraries.adoc[]
* xref:policies-pdk-configure-features-caching.adoc[]
* xref:policies-pdk-configure-features-metadata.adoc[]
* xref:policies-pdk-configure-features-rate-limiting.adoc[]
* xref:policies-pdk-configure-features-streamproperties.adoc[]
* xref:policies-pdk-configure-features-authentication.adoc[]
* xref:policies-pdk-configure-features-timer.adoc[]
Expand Down