Add a 8 Kb flush threshold to RxNettyServerHttpResponse

Issue: SPR-14991
This commit is contained in:
Sebastien Deleuze
2016-12-07 10:43:22 +01:00
parent 856cb3b964
commit 24b3614063
2 changed files with 71 additions and 20 deletions

View File

@@ -28,6 +28,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import rx.Observable;
import rx.RxReactiveStreams;
import rx.functions.Func1;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
@@ -40,6 +41,7 @@ import org.springframework.util.Assert;
*
* @author Rossen Stoyanchev
* @author Stephane Maldini
* @author Sebastien Deleuze
* @since 5.0
*/
public class RxNettyServerHttpResponse extends AbstractServerHttpResponse {
@@ -48,6 +50,9 @@ public class RxNettyServerHttpResponse extends AbstractServerHttpResponse {
private static final ByteBuf FLUSH_SIGNAL = Unpooled.buffer(0, 0);
// 8 Kb flush threshold to avoid blocking RxNetty when the send buffer has reached the high watermark
private static final long FLUSH_THRESHOLD = 8192;
public RxNettyServerHttpResponse(HttpServerResponse<ByteBuf> response,
NettyDataBufferFactory dataBufferFactory) {
super(dataBufferFactory);
@@ -74,7 +79,7 @@ public class RxNettyServerHttpResponse extends AbstractServerHttpResponse {
protected Mono<Void> writeWithInternal(Publisher<? extends DataBuffer> body) {
Observable<ByteBuf> content = RxReactiveStreams.toObservable(body)
.map(NettyDataBufferFactory::toByteBuf);
return Flux.from(RxReactiveStreams.toPublisher(this.response.write(content)))
return Flux.from(RxReactiveStreams.toPublisher(this.response.write(content, new FlushSelector(FLUSH_THRESHOLD))))
.then();
}
@@ -116,6 +121,26 @@ public class RxNettyServerHttpResponse extends AbstractServerHttpResponse {
}
}
private class FlushSelector implements Func1<ByteBuf, Boolean> {
private final long flushEvery;
private long count;
public FlushSelector(long flushEvery) {
this.flushEvery = flushEvery;
}
@Override
public Boolean call(ByteBuf byteBuf) {
this.count += byteBuf.readableBytes();
if (this.count >= this.flushEvery) {
this.count = 0;
return true;
}
return false;
}
}
/*
While the underlying implementation of {@link ZeroCopyHttpOutputMessage} seems to