Skip to content

Commit

Permalink
fix: Avoid creating message string prematurely for streaming calls (#…
Browse files Browse the repository at this point in the history
…3622)

- Creating the string message prematurely puts a lot of strain on the
JVM and GC, switch to the format version of `Preconditions.checkState`
which only constructs the string if we throw the exception
- Issue: #3621

Thank you for opening a Pull Request! Before submitting your PR, please
read our [contributing
guidelines](https://github.com/googleapis/gapic-generator-java/blob/main/CONTRIBUTING.md).

There are a few things you can do to make sure it goes smoothly:
- [x] Make sure to open an issue as a
[bug/issue](https://github.com/googleapis/gapic-generator-java/issues/new/choose)
before writing your code! That way we can discuss the change, evaluate
designs, and agree on the general idea
- [x] Ensure the tests and linter pass
- [x] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary)

Fixes #3621 ☕️
  • Loading branch information
mina-asham authored Feb 10, 2025
1 parent c11360e commit f805e70
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class StateCheckingResponseObserver<V> implements ResponseObserv
* ensuring consistent state.
*/
public final void onStart(StreamController controller) {
Preconditions.checkState(!isStarted, getClass() + " is already started.");
Preconditions.checkState(!isStarted, "%s is already started.", getClass());
isStarted = true;

onStartImpl(controller);
Expand All @@ -56,7 +56,7 @@ public final void onStart(StreamController controller) {
* consistent state.
*/
public final void onResponse(V response) {
Preconditions.checkState(!isClosed, getClass() + " received a response after being closed.");
Preconditions.checkState(!isClosed, "%s received a response after being closed.", getClass());
onResponseImpl(response);
}

Expand All @@ -67,7 +67,7 @@ public final void onResponse(V response) {
* state.
*/
public final void onComplete() {
Preconditions.checkState(!isClosed, getClass() + " tried to double close.");
Preconditions.checkState(!isClosed, "%s tried to double close.", getClass());
isClosed = true;
onCompleteImpl();
}
Expand All @@ -79,7 +79,7 @@ public final void onComplete() {
* consistent state.
*/
public final void onError(Throwable t) {
Preconditions.checkState(!isClosed, getClass() + " received error after being closed", t);
Preconditions.checkState(!isClosed, "%s received error after being closed", t, getClass());
isClosed = true;
onErrorImpl(t);
}
Expand Down

0 comments on commit f805e70

Please sign in to comment.