Skip to content

Commit 96daef2

Browse files
author
Stephane Maldini
committed
Turn channel to auto read on http inbound termination to detect close faster
Auto retry persistent http clients if they failed to write a newly acquired channel
1 parent 10310a3 commit 96daef2

File tree

6 files changed

+33
-30
lines changed

6 files changed

+33
-30
lines changed

src/main/java/reactor/ipc/netty/channel/PooledClientContextHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package reactor.ipc.netty.channel;
1818

19-
import java.io.IOException;
2019
import java.net.InetSocketAddress;
2120
import java.net.SocketAddress;
2221
import java.util.Objects;

src/main/java/reactor/ipc/netty/http/HttpOperations.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ protected HttpOperations(Channel ioChannel,
6666
BiFunction<? super INBOUND, ? super OUTBOUND, ? extends Publisher<Void>> handler,
6767
ContextHandler<?> context) {
6868
super(ioChannel, handler, context);
69+
//reset channel to manual read if re-used
70+
ioChannel.config().setAutoRead(false);
6971
}
7072

7173
/**

src/main/java/reactor/ipc/netty/http/client/HttpClientOperations.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import reactor.ipc.netty.NettyContext;
6767
import reactor.ipc.netty.NettyOutbound;
6868
import reactor.ipc.netty.NettyPipeline;
69+
import reactor.ipc.netty.channel.AbortedException;
6970
import reactor.ipc.netty.channel.ContextHandler;
7071
import reactor.ipc.netty.http.Cookies;
7172
import reactor.ipc.netty.http.HttpOperations;
@@ -480,6 +481,15 @@ protected void onOutboundComplete() {
480481
channel().read();
481482
}
482483

484+
@Override
485+
protected void onOutboundError(Throwable err) {
486+
if(NettyContext.isPersistent(channel()) && responseState == null){
487+
parentContext().fireContextError(err);
488+
return;
489+
}
490+
super.onOutboundError(err);
491+
}
492+
483493
@Override
484494
protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
485495
if (msg instanceof HttpResponse) {
@@ -544,6 +554,8 @@ protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
544554
if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
545555
super.onInboundNext(ctx, msg);
546556
}
557+
//force auto read to enable more accurate close selection now inbound is done
558+
channel().config().setAutoRead(true);
547559
onHandlerTerminate();
548560
return;
549561
}

src/main/java/reactor/ipc/netty/http/client/MonoHttpClientResponse.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
2+
* Copyright (c) 2011-2017 Pivotal Software Inc, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import reactor.core.publisher.Mono;
3535
import reactor.ipc.netty.NettyInbound;
3636
import reactor.ipc.netty.NettyOutbound;
37+
import reactor.ipc.netty.channel.AbortedException;
3738

3839
/**
3940
* @author Stephane Maldini
@@ -175,6 +176,10 @@ public boolean test(Throwable throwable) {
175176
redirect(re.location);
176177
return true;
177178
}
179+
if (AbortedException.isConnectionReset(throwable)) {
180+
redirect(activeURI.toString());
181+
return true;
182+
}
178183
return false;
179184
}
180185
}

src/main/java/reactor/ipc/netty/http/server/DefaultHttpServerRoutes.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
2+
* Copyright (c) 2011-2017 Pivotal Software Inc, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,7 @@
1616

1717
package reactor.ipc.netty.http.server;
1818

19-
import java.io.IOException;
2019
import java.net.URI;
21-
import java.nio.file.FileSystem;
22-
import java.nio.file.FileSystems;
2320
import java.nio.file.Files;
2421
import java.nio.file.Path;
2522
import java.util.Iterator;
@@ -33,7 +30,6 @@
3330
import org.reactivestreams.Publisher;
3431
import reactor.core.Exceptions;
3532
import reactor.core.publisher.Mono;
36-
import reactor.ipc.netty.ByteBufFlux;
3733

3834
/**
3935
* @author Stephane Maldini

src/main/java/reactor/ipc/netty/http/server/HttpServerOperations.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import io.netty.channel.Channel;
3030
import io.netty.channel.ChannelFuture;
31+
import io.netty.channel.ChannelFutureListener;
3132
import io.netty.channel.ChannelHandlerContext;
3233
import io.netty.handler.codec.http.DefaultFullHttpResponse;
3334
import io.netty.handler.codec.http.DefaultHttpResponse;
@@ -364,10 +365,11 @@ protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
364365
if (isOutboundDone()) {
365366
onHandlerTerminate();
366367
}
367-
return;
368-
}
369-
if (isOutboundDone()) {
370-
onOutboundReadMore();
368+
else {
369+
//force auto read to enable more accurate close selection now inbound is done
370+
channel().config()
371+
.setAutoRead(true);
372+
}
371373
}
372374
}
373375
else {
@@ -406,15 +408,6 @@ protected void onOutboundComplete() {
406408
});
407409
}
408410

409-
final void onOutboundReadMore() {
410-
if (isKeepAlive()) {
411-
if (log.isDebugEnabled()) {
412-
log.debug("Consuming keep-alive connection, prepare to ignore extra " + "frames");
413-
}
414-
channel().read();
415-
}
416-
}
417-
418411
@Override
419412
protected void onOutboundError(Throwable err) {
420413

@@ -430,24 +423,20 @@ protected void onOutboundError(Throwable err) {
430423
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
431424
HttpResponseStatus.INTERNAL_SERVER_ERROR);
432425
response.headers()
433-
.setInt(HttpHeaderNames.CONTENT_LENGTH, 0);
426+
.setInt(HttpHeaderNames.CONTENT_LENGTH, 0)
427+
.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
434428
channel().writeAndFlush(response)
435-
.addListener(r -> onHandlerTerminate());
436-
onOutboundReadMore();
429+
.addListener(ChannelFutureListener.CLOSE);
437430
return;
438431
}
439432

440433
if (HttpUtil.isContentLengthSet(nettyResponse)) {
441-
channel().writeAndFlush(EMPTY_BUFFER)
442-
.addListener(r -> onHandlerTerminate());
443-
onOutboundReadMore();
434+
channel().writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT)
435+
.addListener(ChannelFutureListener.CLOSE);
444436
return;
445437
}
446438
channel().writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT)
447-
.addListener(r -> onHandlerTerminate());
448-
if (isKeepAlive()) {
449-
onOutboundReadMore();
450-
}
439+
.addListener(ChannelFutureListener.CLOSE);
451440
}
452441

453442
@Override

0 commit comments

Comments
 (0)