Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.concurrent.FutureListener;

import org.elasticsearch.http.HttpBody;
import org.elasticsearch.transport.netty4.Netty4Utils;
Expand All @@ -33,9 +34,11 @@ public class Netty4HttpRequestBodyStream implements HttpBody.Stream {
private boolean closing = false;
private HttpBody.ChunkHandler handler;

private final FutureListener<Void> closeListener = future -> doClose();

public Netty4HttpRequestBodyStream(Channel channel) {
this.channel = channel;
channel.closeFuture().addListener((f) -> doClose());
channel.closeFuture().addListener(closeListener);
channel.config().setAutoRead(false);
}

Expand Down Expand Up @@ -71,6 +74,7 @@ public void next() {
}

public void handleNettyContent(HttpContent httpContent) {
hasLast = httpContent instanceof LastHttpContent;
if (closing) {
httpContent.release();
return;
Expand All @@ -81,10 +85,6 @@ public void handleNettyContent(HttpContent httpContent) {
} else {
chunkQueue.add(httpContent);
}
if (httpContent instanceof LastHttpContent) {
hasLast = true;
channel.config().setAutoRead(true);
}
}

// visible for test
Expand All @@ -108,6 +108,10 @@ private void sendChunk(HttpContent httpContent) {
var bytesRef = Netty4Utils.toReleasableBytesReference(httpContent.content());
var isLast = httpContent instanceof LastHttpContent;
handler.onNext(bytesRef, isLast);
if (isLast) {
channel.config().setAutoRead(true);
channel.closeFuture().removeListener(closeListener);
}
}

private void releaseQueuedChunks() {
Expand Down