Skip to content

Return 503 for unavailable downstream service - #4230

Open
king-407 wants to merge 1 commit into
spring-cloud:mainfrom
king-407:issue-4208-unavailable-downstream-503
Open

Return 503 for unavailable downstream service#4230
king-407 wants to merge 1 commit into
spring-cloud:mainfrom
king-407:issue-4208-unavailable-downstream-503

Conversation

@king-407

Copy link
Copy Markdown

Fixes #4208.

This updates the WebFlux NettyRoutingFilter so downstream connection failures are reported as 503 Service Unavailable instead of 500 Internal Server Error.

Previously, when Gateway could not connect to the downstream service, for example because the target port was closed, Reactor Netty raised a connection exception and the request resulted in a generic 500 response.

The change maps connection-related failures such as ConnectException and UnknownHostException to ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE).

Validation:

  • ./mvnw -pl spring-cloud-gateway-server-webflux -Dtest=NettyRoutingFilterIntegrationTests#shouldApplyConnectTimeoutPerRoute test
  • ./mvnw -pl spring-cloud-gateway-server-webflux -Dtest=NettyRoutingFilterIntegrationTests test
  • git diff --check

Signed-off-by: king-407 <95581750+king-407@users.noreply.github.com>

@anshulgoel22 anshulgoel22 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling #4208 — returning 503 for unreachable downstream services is the right HTTP semantics here.

What looks good

  • Mapping ConnectException and UnknownHostException to ResponseStatusException(SERVICE_UNAVAILABLE) aligns with how clients, proxies, and load balancers distinguish "upstream temporarily unavailable" from an internal gateway error (500).
  • The recursive isUnavailable() cause walk is important for Netty, which often wraps connection failures (e.g. AnnotatedConnectException around ConnectException, as reported in the issue).
  • The change follows the existing pattern in this filter for timeout handling (TimeoutException → 504), so the error-mapping approach is consistent.
  • The integration test update validates both status code and response body fields, not just the status integer.

Suggestion (non-blocking)
Issue #4208 also mentions DNS resolution failures in Kubernetes (service not yet published). UnknownHostException is covered by the mapper, but a dedicated integration test for an unresolvable host would make that scenario explicit and guard against regressions.

Overall this is a focused, well-scoped fix. Nice work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unavailable downstream service results in HTTP 500

3 participants