Skip to content

Commit aa7cc51

Browse files
rieskesnowp
authored andcommitted
server: ensure errors not logged when client closes connection (#84)
Errors were being logged when client Envoy closes connection while being shut down/restarted. This change ensures that errors are not logged when the error code is CANCELLED. Fixes #83 Signed-off-by: Vaidotas Valuckas <[email protected]>
1 parent e1666f2 commit aa7cc51

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

server/src/main/java/io/envoyproxy/controlplane/server/DiscoveryServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public void onNext(DiscoveryRequest request) {
221221

222222
@Override
223223
public void onError(Throwable t) {
224-
if (!Status.fromThrowable(t).equals(Status.CANCELLED)) {
224+
if (!Status.fromThrowable(t).getCode().equals(Status.CANCELLED.getCode())) {
225225
LOGGER.error("[{}] stream closed with error", streamId, t);
226226
}
227227

server/src/test/java/io/envoyproxy/controlplane/server/DiscoveryServerTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@
3737
import io.envoyproxy.controlplane.cache.Watch;
3838
import io.envoyproxy.controlplane.cache.WatchCancelledException;
3939
import io.grpc.Status;
40+
import io.grpc.StatusRuntimeException;
4041
import io.grpc.stub.StreamObserver;
4142
import io.grpc.testing.GrpcServerRule;
43+
44+
import java.io.ByteArrayOutputStream;
45+
import java.io.PrintStream;
4246
import java.util.Collection;
4347
import java.util.HashMap;
4448
import java.util.LinkedList;
@@ -748,6 +752,58 @@ public void onStreamCloseWithError(long streamId, String typeUrl, Throwable erro
748752
assertThat(callbacks.streamResponseCount).hasValue(0);
749753
}
750754

755+
@Test
756+
public void callbackOnError_logsError_onException() {
757+
MockConfigWatcher configWatcher = new MockConfigWatcher(false, createResponses());
758+
DiscoveryServer server = new DiscoveryServer(configWatcher);
759+
760+
AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase service =
761+
server.getAggregatedDiscoveryServiceImpl();
762+
763+
MockDiscoveryResponseObserver responseObserver = new MockDiscoveryResponseObserver();
764+
StreamObserver<DiscoveryRequest> requestObserver = service.streamAggregatedResources(responseObserver);
765+
766+
try {
767+
ByteArrayOutputStream stdErr = new ByteArrayOutputStream();
768+
System.setErr(new PrintStream(stdErr));
769+
770+
requestObserver.onError(new StatusRuntimeException(Status.INTERNAL
771+
.withDescription("internal error")
772+
.withCause(new RuntimeException("some error"))));
773+
774+
assertThat(stdErr.toString()).contains("ERROR ");
775+
assertThat(stdErr.toString()).contains("io.grpc.StatusRuntimeException: INTERNAL: internal error");
776+
} finally {
777+
System.setErr(System.err);
778+
}
779+
}
780+
781+
@Test
782+
public void callbackOnError_doesNotLogError_whenCancelled() {
783+
MockConfigWatcher configWatcher = new MockConfigWatcher(false, createResponses());
784+
DiscoveryServer server = new DiscoveryServer(configWatcher);
785+
786+
AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase service =
787+
server.getAggregatedDiscoveryServiceImpl();
788+
789+
MockDiscoveryResponseObserver responseObserver = new MockDiscoveryResponseObserver();
790+
StreamObserver<DiscoveryRequest> requestObserver = service.streamAggregatedResources(responseObserver);
791+
792+
try {
793+
ByteArrayOutputStream stdErr = new ByteArrayOutputStream();
794+
System.setErr(new PrintStream(stdErr));
795+
796+
requestObserver.onError(new StatusRuntimeException(Status.CANCELLED
797+
.withDescription("internal error")
798+
.withCause(new RuntimeException("some error"))));
799+
800+
assertThat(stdErr.toString()).doesNotContain("ERROR ");
801+
assertThat(stdErr.toString()).doesNotContain("io.grpc.StatusRuntimeException: CANCELLED:");
802+
} finally {
803+
System.setErr(System.err);
804+
}
805+
}
806+
751807
private static Table<String, String, Collection<? extends Message>> createResponses() {
752808
return ImmutableTable.<String, String, Collection<? extends Message>>builder()
753809
.put(Resources.CLUSTER_TYPE_URL, VERSION, ImmutableList.of(CLUSTER))

0 commit comments

Comments
 (0)