Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to terminate iteration early in HttpModule #491

Open
Chi-Kai opened this issue Dec 12, 2024 · 1 comment
Open

Is it possible to terminate iteration early in HttpModule #491

Chi-Kai opened this issue Dec 12, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Chi-Kai
Copy link

Chi-Kai commented Dec 12, 2024

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:

impl HttpModule for CacheContext {
    async fn request_body_filter(
        &mut self,
        body: &mut Option<Bytes>,
        _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

@Noah-Kennedy Noah-Kennedy added the enhancement New feature or request label Dec 13, 2024
@taikulawo
Copy link
Contributor

We forked pingora internally, add new args Next to Pingora Filter. It works well. Maybe we can add next function like Koa/Gin framework.
But this require api breaking.

async fn request_body_filter(
      &mut self,
      body: &mut Option<Bytes>,
      end_of_stream: bool,
      next: RequestBodyFilter<'_>,
  ) -> 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);
              return Ok(())
          }
      }
      return next.call(body, end_of_stream).await;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants