Skip to content

Commit

Permalink
default to retry when http2 closed exception occurred (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mkabaka authored Feb 2, 2021
1 parent 8771cb7 commit 2372967
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

public class ClosedConnectionException extends IOException {

public static final ClosedConnectionException INSTANCE = new ClosedConnectionException(
"Connection is inactive");

private static final long serialVersionUID = -7491330351921922628L;

public ClosedConnectionException(String msg) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2021 OPPO ESA Stack Project
*
* 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 esa.httpclient.core.exception;

import java.io.IOException;

public class ClosedStreamException extends IOException {

public static final ClosedStreamException CAUSED_BY_RST =
new ClosedStreamException("Received reset stream and current stream will be discarded");

public static final ClosedStreamException CAUSED_BY_REMOVED =
new ClosedStreamException("Stream has been removed");

private static final long serialVersionUID = -7491330351921922628L;

public ClosedStreamException(String msg) {
super(msg);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import esa.httpclient.core.HttpRequest;
import esa.httpclient.core.HttpResponse;
import esa.httpclient.core.exception.ClosedConnectionException;
import esa.httpclient.core.exception.ClosedStreamException;
import esa.httpclient.core.util.Futures;

import java.net.ConnectException;
Expand All @@ -40,7 +41,9 @@ public boolean canRetry(HttpRequest request,
}

final Throwable unwrapped = Futures.unwrapped(cause);
if (unwrapped instanceof ConnectException || unwrapped instanceof ClosedConnectionException) {
if (unwrapped instanceof ConnectException
|| unwrapped instanceof ClosedConnectionException
|| unwrapped instanceof ClosedStreamException) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import esa.commons.netty.core.BufferImpl;
import esa.commons.netty.http.Http2HeadersAdaptor;
import esa.httpclient.core.Context;
import esa.httpclient.core.exception.ClosedConnectionException;
import esa.httpclient.core.exception.ClosedStreamException;
import esa.httpclient.core.exception.ContentOverSizedException;
import esa.httpclient.core.util.HttpHeadersUtils;
import esa.httpclient.core.util.LoggerUtils;
Expand All @@ -30,7 +30,6 @@
import io.netty.handler.codec.http.HttpStatusClass;
import io.netty.handler.codec.http2.Http2CodecUtil;
import io.netty.handler.codec.http2.Http2Connection;
import io.netty.handler.codec.http2.Http2Error;
import io.netty.handler.codec.http2.Http2EventAdapter;
import io.netty.handler.codec.http2.Http2Exception;
import io.netty.handler.codec.http2.Http2Headers;
Expand Down Expand Up @@ -155,8 +154,11 @@ public void onRstStreamRead(ChannelHandlerContext ctx,
long errorCode) {
final Http2Stream stream = connection.stream(streamId);
if (stream != null) {
onError(Http2Exception.streamError(streamId, Http2Error.valueOf(errorCode),
"Received reset stream"), stream, streamId, true);
final Throwable ex = ClosedStreamException.CAUSED_BY_RST;
if (LoggerUtils.logger().isDebugEnabled()) {
LoggerUtils.logger().debug(ex.getMessage());
}
onError(ex, stream, streamId, false);
}
}

Expand All @@ -165,12 +167,12 @@ public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId,
long errorCode, ByteBuf debugData) throws Http2Exception {
final String errMsg = debugData.toString(StandardCharsets.UTF_8);

final ClosedConnectionException ex;
final ClosedStreamException ex;
if (NO_ERROR.code() == errorCode) {
ex = new ClosedConnectionException("Received goAway stream in connection: " + ctx.channel()
ex = new ClosedStreamException("Received goAway stream in connection: " + ctx.channel()
+ ", maybe server has closed the connection");
} else {
ex = new ClosedConnectionException("Received goAway stream in connection: " +
ex = new ClosedStreamException("Received goAway stream in connection: " +
ctx.channel() + ", msg: " + errMsg);
}

Expand Down Expand Up @@ -221,8 +223,8 @@ public void onStreamRemoved(Http2Stream stream) {
try {
super.onStreamRemoved(stream);
} finally {
onError(Http2Exception.streamError(stream.id(), Http2Error.STREAM_CLOSED, "Stream removed"),
stream, -1, false);
final Throwable ex = ClosedStreamException.CAUSED_BY_REMOVED;
onError(ex, stream, -1, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void onError0(Throwable cause) {

listener.onError(request, ctx, cause);
} catch (Throwable ex) {
LoggerUtils.logger().warn("Unexpected exception occurred on handle#onError0", cause);
LoggerUtils.logger().error("Unexpected exception occurred on handle#onError0", cause);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import esa.httpclient.core.HttpRequest;
import esa.httpclient.core.HttpResponse;
import esa.httpclient.core.exception.ClosedConnectionException;
import esa.httpclient.core.exception.ClosedStreamException;
import org.junit.jupiter.api.Test;

import java.net.ConnectException;
Expand All @@ -36,7 +37,9 @@ void testCanRetry() {
then(predicate.canRetry(mock(HttpRequest.class), mock(HttpResponse.class), mock(Context.class),
new ConnectException())).isTrue();
then(predicate.canRetry(mock(HttpRequest.class), mock(HttpResponse.class), mock(Context.class),
ClosedConnectionException.INSTANCE)).isTrue();
new ClosedConnectionException(""))).isTrue();
then(predicate.canRetry(mock(HttpRequest.class), mock(HttpResponse.class), mock(Context.class),
new ClosedStreamException(""))).isTrue();
then(predicate.canRetry(mock(HttpRequest.class), mock(HttpResponse.class), mock(Context.class),
new RuntimeException())).isFalse();
}
Expand Down
Loading

0 comments on commit 2372967

Please sign in to comment.