Skip to content
Merged
Show file tree
Hide file tree
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 @@ -22,7 +22,7 @@
import co.elastic.clients.util.NoCopyByteArrayOutputStream;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
import org.apache.hc.core5.http.nio.AsyncDataProducer;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.DataStreamChannel;

import java.io.IOException;
Expand All @@ -32,20 +32,24 @@
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicReference;

/**
* An HTTP entity based on a sequence of byte buffers.
*/
class MultiBufferEntity extends AbstractHttpEntity implements AsyncDataProducer {
class MultiBufferEntity extends AbstractHttpEntity implements AsyncEntityProducer {

private final Iterable<ByteBuffer> buffers;

private Iterator<ByteBuffer> iterator;
private volatile ByteBuffer currentBuffer;

private final AtomicReference<Exception> exceptionRef;

MultiBufferEntity(Iterable<ByteBuffer> buffers, ContentType contentType) {
super(contentType,null,true);
this.buffers = buffers;
this.exceptionRef = new AtomicReference<>();
init();
}

Expand All @@ -69,6 +73,13 @@ public boolean isRepeatable() {
return true;
}

@Override
public void failed(Exception cause) {
if (exceptionRef.compareAndSet(null, cause)) {
releaseResources();
}
}

@Override
public long getContentLength() {
// Use chunked encoding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.message.RequestLine;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
Expand Down Expand Up @@ -409,7 +410,7 @@ private void performRequestAsync(
final RequestContext context;
context = request.createContextForNextAttempt(nodes.next());
Future<ClassicHttpResponse> futureRef = client.execute(context.requestProducer, context.asyncResponseConsumer, context.context,
new FutureCallback<ClassicHttpResponse>() {
new FutureCallback<>() {
@Override
public void completed(ClassicHttpResponse httpResponse) {
try {
Expand Down Expand Up @@ -877,7 +878,13 @@ private static class RequestContext {
.setHeaders(request.httpRequest.getHeaders());

if (request.httpRequest.getEntity() != null) {
builder.setEntity(new BasicAsyncEntityProducer(request.httpRequest.getEntity()));
HttpEntity entity = request.httpRequest.getEntity();
if (entity instanceof AsyncEntityProducer) {
builder.setEntity((AsyncEntityProducer) request.httpRequest.getEntity());
}
else {
builder.setEntity(new BasicAsyncEntityProducer(entity));
}
}

this.requestProducer = builder.build();
Expand Down