cloudflare / cloudflare/pingora

Is it possible to terminate iteration early in HttpModule

Open
#491 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
27.4k
Forks
1.7k
Avg merge
6h 22m
Merged PRs (30d)
3

Description

## What is the problem your feature solves, or the need it fulfills?

When using the `HttpModule` , I want to terminate the iteration and immediately return response. Currently, there is no clear way to stop the execution flow within the `request_body_filter` method and directly response back.

## Describe the solution you'd like

I propose implementing a mechanism in the `HttpModule` to support immediate termination of the request flow and return a response. For example, within the `request_body_filter` method, if a cache hit is detected, the function should allow setting the response and halting further execution.

The code might look like this:
```rust
impl HttpModule for CacheContext {
async fn request_body_filter(
&mut self,
body: &mut Option,
_end_of_stream: bool,
) -> Result<()> {
if let Some(cache) = Cache::global() {
if let Ok(Some(cached_response)) = cache.try_cache(&self.query).await {
info!("HIT! cached_response: {}", cached_response);
let formatted_response =
cache.response_template.replace("%s", &cached_response);
self.cached_response = Some(formatted_response);

// Mechanism to terminate further processing and return cached response.

}
}
Ok(())
}
}
```

## Describe alternatives you've considered

## Additional context

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.