-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use gRPC StatusRuntimeException instead of StatusException. (#4658)
Motivation: We have changed to use `StatusException` for gRPC client in this PR: https://github.com/line/armeria/pull/4636/files#diff-39112577712e2be6800f84834abc33a690eed333cd96581b777e1e1cd1ccdadaR497 However, `StatusRuntimeException` is used more by the upstream gRPC and gRPC's stubs favor it. ``` /** * {@link Status} in Exception form, for propagating Status information via exceptions. This is * semantically equivalent to {@link StatusRuntimeException}, except for usage in APIs that promote * checked exceptions. gRPC's stubs favor {@code StatusRuntimeException}. */ public class StatusException extends Exception {...} ``` They are semantically equivalent and users handle the exceptions together. grpc/grpc-java#4683 https://github.com/grpc/grpc-java/blob/master/api/src/main/java/io/grpc/Status.java#L404-L408 gRPC stub usually creates `StatusRuntimeException`: https://github.com/grpc/grpc-java/blob/master/stub/src/main/java/io/grpc/stub/ClientCalls.java#L540-L544 https://github.com/grpc/grpc-java/blob/master/stub/src/main/java/io/grpc/stub/ServerCalls.java#L291 So we'd better to use `StatusRuntimeException` instead of `StatusException`. If so, we can also reduce creating the exception multiple times which is expensive due to `fillInStackTrace` call, Modifications: - Use `StatusRuntimeException` instead of `StatusException`. - Add `StatusAndMetadata` that creates the `StatusRuntimeException` only once. Result: - `StatusRuntimeException` is used for aborting a request and logging.
- Loading branch information
Showing
13 changed files
with
180 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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 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 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
57 changes: 57 additions & 0 deletions
57
grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/StatusAndMetadata.java
This file contains 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,57 @@ | ||
/* | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.internal.common.grpc; | ||
|
||
import com.linecorp.armeria.common.annotation.Nullable; | ||
|
||
import io.grpc.Metadata; | ||
import io.grpc.Status; | ||
import io.grpc.StatusRuntimeException; | ||
|
||
/** | ||
* A class that is used to cache {@link StatusRuntimeException} created from | ||
* {@link Status#asRuntimeException(Metadata)}. | ||
*/ | ||
public final class StatusAndMetadata { | ||
|
||
private final Status status; | ||
@Nullable | ||
private final Metadata metadata; | ||
@Nullable | ||
private StatusRuntimeException statusRuntimeException; | ||
|
||
public StatusAndMetadata(Status status, @Nullable Metadata metadata) { | ||
this.status = status; | ||
this.metadata = metadata; | ||
} | ||
|
||
public Status status() { | ||
return status; | ||
} | ||
|
||
@Nullable | ||
public Metadata metadata() { | ||
return metadata; | ||
} | ||
|
||
public StatusRuntimeException asRuntimeException() { | ||
if (statusRuntimeException == null) { | ||
statusRuntimeException = status.asRuntimeException(metadata); | ||
} | ||
return statusRuntimeException; | ||
} | ||
} |
This file contains 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 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 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 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.