spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

[SpringCloudGateway]: Post filters are not executed after endpoint redirection timeout

Open deepak-chhetri opened this issue 1 year ago • 4 comments

Spring cloud Gateway version: 4.1.0

We have implemented couple of custom Ordered Global Pre and Post filters in spring cloud gateway application. These pre global filters are getting executed for any incoming requests before redirecting to configured endpoint. And all post global filters are getting executed after endpoint call is completed successfully and response is received.

We have observed that these post filters are getting executed for other 4xx, like 404 (Not Found), 401(Unauthorized), and 5xx, like 503(Service Unavailable), 500 (Internal Server error), error response from endpoint call.

However, we have observed that these post global filters are not getting executed when the endpoint call is getting timeout, that is 504 Gateway Timeout response is received. Global response timeout is configured as 30sec as follows and endpoint call is taking more than 30 sec to process the request:

spring:
  cloud:
    gateway:
      httpclient:
        connect-timeout: 30000
        response-timeout: 30000

All the three global post filters are implemented in following pattern:

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    return chain.filter(exchange).then(Mono.fromRunnable(() -> {
        <list of statements that modify the response in exchange object>
   }));
}

@Override
public int getOrder() {
   return POST_FILTER_BASE_ORDER - 3;  // POST_FILTER_BASE_ORDER = 1000
}

Is it an expected behavior from Spring Cloud Gateway framework or defect? Any help to resolve the issue is much appreciated.

deepak-chhetri avatar Feb 14 '24 16:02 deepak-chhetri

@deepak-chhetri can you share the sample post-filter logic you have implemented?

shashikanth8639 avatar Feb 15 '24 05:02 shashikanth8639

@deepak-chhetri can you share the sample post-filter logic you have implemented?

@shashikanth8639 Post filter is implemented in following way:

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    return chain.filter(exchange).then(Mono.fromRunnable(() -> {
        final ResponseCookie  resCookie = ResponseCookie.from(this.cookieName, "invalidated").httpOnly(true).secure(true).path("/").maxAge(0).build();
        exchange.getResponse().addCookie(resCookie);
   }));
}

This post filter executes for 404 and 503 response status from endpoint, however fails to execute when API gateway times-out the endpoint call (endpoint take more than 30sec to respond).

deepak-chhetri avatar Feb 15 '24 07:02 deepak-chhetri

@deepak-chhetri have you tried using the onError method?

shashikanth8639 avatar Feb 15 '24 14:02 shashikanth8639

@deepak-chhetri have you tried using the onError method?

Thanks @shashikanth8639 for sharing the pointer. Do you mean Mono.onError()(publisher) method?

However I need to figure out why post filters are not executing when endpoint call gets timeout so that better error handling mechanism can be implemented. Since for other 4xx and 5xx status code, post filters are getting executed.

deepak-chhetri avatar Feb 15 '24 15:02 deepak-chhetri

400 and 500 errors are responses from the server. A timeout is a local exception. Have you tried https://github.com/spring-cloud/spring-cloud-gateway/issues/3264#issuecomment-1946246543

spencergibb avatar Mar 11 '24 16:03 spencergibb

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues avatar Mar 18 '24 16:03 spring-cloud-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-cloud-issues avatar Mar 25 '24 16:03 spring-cloud-issues

400 and 500 errors are responses from the server. A timeout is a local exception. Have you tried #3264 (comment)

@spencergibb Spring Cloud Gateway is timeout the downstream endpoint request. Downstream endpoint request is triggered by framework class NettyRoutingFilter class (read-only byte code). Where should I try adding onError() operator for timeout request by gateway?

deepak-chhetri avatar Apr 07 '24 12:04 deepak-chhetri