Repository navigation
fix: gRPC error handling — proper status codes and circuit breaker accuracy #645
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
29cd884
fix: circuit breaker and gRPC-to-HTTP error mapping
CatherineSue e2c141e
refactor: consolidate gRPC error handling helpers into utils
CatherineSue fdae13a
refactor: replace gRPC error free functions with TonicStatusExt trait
CatherineSue 315bc28
refactor: move TonicStatusExt to dedicated tonic_ext module
CatherineSue 00f6c9e
fix: address PR review comments
CatherineSue cc8d952
fix: use is_retryable_status for HTTP circuit breaker outcome
CatherineSue 85e57c1
fix: unify gRPC circuit breaker predicate with HTTP retry logic
CatherineSue 48fff6d
fix: add explicit Code::Ok arm to http_status() mapping
CatherineSue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //! Extension traits for tonic gRPC types. | ||
|
|
||
| use axum::response::Response; | ||
| use http::StatusCode; | ||
| use tonic::Code; | ||
|
|
||
| use crate::{core::is_retryable_status, routers::error}; | ||
|
|
||
| /// Extension methods for `tonic::Status`. | ||
| pub(crate) trait TonicStatusExt { | ||
| /// Map gRPC status code to the corresponding HTTP status code. | ||
| fn http_status(&self) -> StatusCode; | ||
|
|
||
| /// Returns `true` if this error should trip the circuit breaker. | ||
| /// | ||
| /// Delegates to `is_retryable_status(http_status())` so the circuit breaker | ||
| /// uses the same predicate for both HTTP and gRPC paths. This covers | ||
| /// 408, 429, 500, 502, 503, 504 (i.e. ResourceExhausted, DeadlineExceeded, | ||
| /// Internal, Unavailable, Unknown, DataLoss, etc.). | ||
| fn is_cb_failure(&self) -> bool; | ||
|
|
||
| /// Convert this gRPC error into an HTTP error response with the appropriate status code. | ||
| fn to_http_error(&self, code: &str, msg: String) -> Response; | ||
| } | ||
|
|
||
| impl TonicStatusExt for tonic::Status { | ||
| fn http_status(&self) -> StatusCode { | ||
| match self.code() { | ||
| Code::Ok => StatusCode::OK, | ||
| Code::InvalidArgument | ||
| | Code::FailedPrecondition | ||
| | Code::OutOfRange | ||
| | Code::Cancelled => StatusCode::BAD_REQUEST, | ||
| Code::Unauthenticated => StatusCode::UNAUTHORIZED, | ||
| Code::PermissionDenied => StatusCode::FORBIDDEN, | ||
| Code::NotFound => StatusCode::NOT_FOUND, | ||
| Code::AlreadyExists | Code::Aborted => StatusCode::CONFLICT, | ||
| Code::ResourceExhausted => StatusCode::TOO_MANY_REQUESTS, | ||
| Code::Unavailable => StatusCode::SERVICE_UNAVAILABLE, | ||
| Code::DeadlineExceeded => StatusCode::GATEWAY_TIMEOUT, | ||
| Code::Unimplemented => StatusCode::NOT_IMPLEMENTED, | ||
| // Internal, Unknown, DataLoss | ||
| _ => StatusCode::INTERNAL_SERVER_ERROR, | ||
| } | ||
| } | ||
|
|
||
| fn is_cb_failure(&self) -> bool { | ||
| is_retryable_status(self.http_status()) | ||
|
CatherineSue marked this conversation as resolved.
Resolved
|
||
| } | ||
|
slin1237 marked this conversation as resolved.
Resolved
|
||
|
|
||
| fn to_http_error(&self, code: &str, msg: String) -> Response { | ||
| error::create_error(self.http_status(), code, msg) | ||
| } | ||
| } | ||
|
|
||
| /// Extension for `Result<T, tonic::Status>` to check circuit breaker health. | ||
| pub(crate) trait TonicResultExt { | ||
| /// Returns `true` if the result is healthy for the circuit breaker. | ||
| /// `Ok` and client-error results are healthy; only retryable errors are failures. | ||
| fn is_healthy(&self) -> bool; | ||
| } | ||
|
|
||
| impl<T> TonicResultExt for Result<T, tonic::Status> { | ||
| fn is_healthy(&self) -> bool { | ||
| self.as_ref().map_or_else(|e| !e.is_cb_failure(), |_| true) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.