Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Prevent watch stream from emitting events after close. (#1471)
* Prevent watch stream from emitting events after close. * Cleanup * Copyright * Make classes final and hide visibility. * Add comments * Hand roll spy * Add unit test, rename class, add comments. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Copyright * Comment * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Comments --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c1f478f
commit ee3f8c0
Showing
8 changed files
with
542 additions
and
45 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
125 changes: 125 additions & 0 deletions
125
google-cloud-firestore/src/main/java/com/google/cloud/firestore/SilenceableBidiStream.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,125 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed 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 | ||
* | ||
* http://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.google.cloud.firestore; | ||
|
||
import com.google.api.gax.rpc.BidiStreamObserver; | ||
import com.google.api.gax.rpc.ClientStream; | ||
import com.google.api.gax.rpc.StreamController; | ||
import java.util.function.Function; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Conditionally pass through callbacks to wrapped `BidiStreamObserver`. | ||
* | ||
* <p>Due to the asynchronous nature of a stream, there can be a delay between closing a stream and | ||
* the upstream no longer sending responses. Receiving callbacks after closing upstream can have | ||
* undesirable consequences. | ||
* | ||
* <p>The underlying `ClientStream` can be called through the `SilenceableBidiStream`. Methods such | ||
* as `send()` and `closeSend()` are exposed. | ||
* | ||
* <p>The `SilenceableBidiStream` wraps a `BidiStreamObserver`. This is helpful for situations where | ||
* the observer should be detached from a stream. Instead of calling the `closeSend()` method, the | ||
* `closeSendAndSilence()` method will silence the stream by preventing further callbacks including | ||
* `onError` and `onComplete`. | ||
* | ||
* <p>If silenced, the observer could be safely attached to a new stream. This is useful for error | ||
* handling where upstream must be stopped, but a new stream can continue to service the observer. | ||
* In these cases, the old stream cannot be allowed to send more responses, and especially cannot be | ||
* allowed to send `onError` or `onComplete` since that would signal the downstream that the stream | ||
* is finished. | ||
*/ | ||
final class SilenceableBidiStream<RequestT, ResponseT> | ||
implements BidiStreamObserver<RequestT, ResponseT> { | ||
|
||
private final ClientStream<RequestT> stream; | ||
private final BidiStreamObserver<RequestT, ResponseT> delegate; | ||
private boolean silence = false; | ||
private static final Logger LOGGER = Logger.getLogger(Watch.class.getName()); | ||
|
||
SilenceableBidiStream( | ||
BidiStreamObserver<RequestT, ResponseT> responseObserverT, | ||
Function<BidiStreamObserver<RequestT, ResponseT>, ClientStream<RequestT>> streamSupplier) { | ||
this.delegate = responseObserverT; | ||
stream = streamSupplier.apply(this); | ||
} | ||
|
||
public boolean isSilenced() { | ||
return silence; | ||
} | ||
|
||
public void send(RequestT request) { | ||
LOGGER.info(stream.toString()); | ||
stream.send(request); | ||
} | ||
|
||
public void closeSend() { | ||
LOGGER.info(stream::toString); | ||
stream.closeSend(); | ||
} | ||
|
||
public void closeSendAndSilence() { | ||
LOGGER.info(stream::toString); | ||
silence = true; | ||
stream.closeSend(); | ||
} | ||
|
||
@Override | ||
public void onReady(ClientStream<RequestT> stream) { | ||
if (silence) { | ||
LOGGER.info(() -> String.format("Silenced: %s", stream)); | ||
} else { | ||
delegate.onReady(stream); | ||
} | ||
} | ||
|
||
@Override | ||
public void onStart(StreamController controller) { | ||
if (silence) { | ||
LOGGER.info(() -> String.format("Silenced: %s", stream)); | ||
} else { | ||
delegate.onStart(controller); | ||
} | ||
} | ||
|
||
@Override | ||
public void onResponse(ResponseT response) { | ||
if (silence) { | ||
LOGGER.info(() -> String.format("Silenced: %s", stream)); | ||
} else { | ||
delegate.onResponse(response); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
if (silence) { | ||
LOGGER.info(() -> String.format("Silenced: %s", stream)); | ||
} else { | ||
delegate.onError(t); | ||
} | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
if (silence) { | ||
LOGGER.info(() -> String.format("Silenced: %s", stream)); | ||
} else { | ||
delegate.onComplete(); | ||
} | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
google-cloud-firestore/src/test/java/com/google/cloud/firestore/FirestoreSpy.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,40 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed 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 | ||
* | ||
* http://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.google.cloud.firestore; | ||
|
||
import com.google.api.gax.rpc.BidiStreamObserver; | ||
import com.google.api.gax.rpc.BidiStreamingCallable; | ||
import com.google.api.gax.rpc.ClientStream; | ||
|
||
public final class FirestoreSpy { | ||
|
||
public final FirestoreImpl spy; | ||
public BidiStreamObserver streamRequestBidiStreamObserver; | ||
|
||
public FirestoreSpy(FirestoreOptions firestoreOptions) { | ||
spy = | ||
new FirestoreImpl(firestoreOptions) { | ||
@Override | ||
public <RequestT, ResponseT> ClientStream<RequestT> streamRequest( | ||
BidiStreamObserver<RequestT, ResponseT> responseObserverT, | ||
BidiStreamingCallable<RequestT, ResponseT> callable) { | ||
streamRequestBidiStreamObserver = responseObserverT; | ||
return super.streamRequest(responseObserverT, callable); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.