Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ywangd committed Oct 28, 2024
1 parent 90c0f50 commit 885433d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,8 @@ static class ServerRequestHandler implements BaseRestHandler.RequestBodyChunkCon
public void handleChunk(RestChannel channel, ReleasableBytesReference chunk, boolean isLast) {
Transports.assertTransportThread();
if (shouldThrowInsideHandleChunk) {
if (randomBoolean()) {
chunk.close();
}
// Must close the chunk. This is the contract of this method.
chunk.close();
throw new RuntimeException("simulated exception inside handleChunk");
}
recvChunks.add(new Chunk(chunk, isLast));
Expand Down
19 changes: 10 additions & 9 deletions server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,7 @@ public final void handleRequest(RestRequest request, RestChannel channel, NodeCl
request.contentStream().setHandler(new HttpBody.ChunkHandler() {
@Override
public void onNext(ReleasableBytesReference chunk, boolean isLast) {
try {
chunkConsumer.handleChunk(channel, chunk, isLast);
} catch (Exception e) {
// Release the chunk if the handler fails before releasing it in exceptional cases
if (chunk.hasReferences()) {
chunk.decRef();
}
throw e;
}
chunkConsumer.handleChunk(channel, chunk, isLast);
}

@Override
Expand Down Expand Up @@ -218,6 +210,15 @@ default void close() {}
}

public interface RequestBodyChunkConsumer extends RestChannelConsumer {

/**
* Handle one chunk of the request body. The handler <b>must</b> close the chunk once it is no longer
* needed to avoid leaking.
*
* @param channel The rest channel associated to the request
* @param chunk The chunk of request body that is ready for processing
* @param isLast Whether the chunk is the last one of the request
*/
void handleChunk(RestChannel channel, ReleasableBytesReference chunk, boolean isLast);

/**
Expand Down

0 comments on commit 885433d

Please sign in to comment.