Skip to content

Commit 3c79d53

Browse files
authored
Propagate timeout errors to callback. (#1761)
JAVA-5906
1 parent b75f8c2 commit 3c79d53

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

driver-core/src/main/com/mongodb/internal/connection/AsynchronousChannelStream.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.concurrent.atomic.AtomicReference;
3838

3939
import static com.mongodb.assertions.Assertions.assertTrue;
40+
import static com.mongodb.internal.async.AsyncRunnable.beginAsync;
4041
import static com.mongodb.internal.thread.InterruptionUtil.interruptAndCreateMongoInterruptedException;
4142
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4243

@@ -88,7 +89,7 @@ protected void setChannel(final ExtendedAsynchronousByteChannel channel) {
8889

8990
@Override
9091
public void writeAsync(final List<ByteBuf> buffers, final OperationContext operationContext,
91-
final AsyncCompletionHandler<Void> handler) {
92+
final AsyncCompletionHandler<Void> handler) {
9293
AsyncWritableByteChannelAdapter byteChannel = new AsyncWritableByteChannelAdapter();
9394
Iterator<ByteBuf> iter = buffers.iterator();
9495
pipeOneBuffer(byteChannel, iter.next(), operationContext, new AsyncCompletionHandler<Void>() {
@@ -189,8 +190,11 @@ public void failed(final Throwable t) {
189190

190191
private class AsyncWritableByteChannelAdapter {
191192
void write(final ByteBuffer src, final OperationContext operationContext, final AsyncCompletionHandler<Void> handler) {
192-
getChannel().write(src, operationContext.getTimeoutContext().getWriteTimeoutMS(), MILLISECONDS, null,
193-
new AsyncWritableByteChannelAdapter.WriteCompletionHandler(handler));
193+
beginAsync().thenRun((c) -> {
194+
long writeTimeoutMS = operationContext.getTimeoutContext().getWriteTimeoutMS();
195+
getChannel().write(src, writeTimeoutMS, MILLISECONDS, null,
196+
new AsyncWritableByteChannelAdapter.WriteCompletionHandler(c.asHandler()));
197+
}).finish(handler.asCallback());
194198
}
195199

196200
private class WriteCompletionHandler extends BaseCompletionHandler<Void, Integer, Object> {
@@ -222,7 +226,7 @@ private final class BasicCompletionHandler extends BaseCompletionHandler<ByteBuf
222226
private final OperationContext operationContext;
223227

224228
private BasicCompletionHandler(final ByteBuf dst, final OperationContext operationContext,
225-
final AsyncCompletionHandler<ByteBuf> handler) {
229+
final AsyncCompletionHandler<ByteBuf> handler) {
226230
super(handler);
227231
this.byteBufReference = new AtomicReference<>(dst);
228232
this.operationContext = operationContext;
@@ -231,17 +235,20 @@ private BasicCompletionHandler(final ByteBuf dst, final OperationContext operati
231235
@Override
232236
public void completed(final Integer result, final Void attachment) {
233237
AsyncCompletionHandler<ByteBuf> localHandler = getHandlerAndClear();
234-
ByteBuf localByteBuf = byteBufReference.getAndSet(null);
235-
if (result == -1) {
236-
localByteBuf.release();
237-
localHandler.failed(new MongoSocketReadException("Prematurely reached end of stream", serverAddress));
238-
} else if (!localByteBuf.hasRemaining()) {
239-
localByteBuf.flip();
240-
localHandler.completed(localByteBuf);
241-
} else {
242-
getChannel().read(localByteBuf.asNIO(), operationContext.getTimeoutContext().getReadTimeoutMS(), MILLISECONDS, null,
243-
new BasicCompletionHandler(localByteBuf, operationContext, localHandler));
244-
}
238+
beginAsync().<ByteBuf>thenSupply((c) -> {
239+
ByteBuf localByteBuf = byteBufReference.getAndSet(null);
240+
if (result == -1) {
241+
localByteBuf.release();
242+
throw new MongoSocketReadException("Prematurely reached end of stream", serverAddress);
243+
} else if (!localByteBuf.hasRemaining()) {
244+
localByteBuf.flip();
245+
c.complete(localByteBuf);
246+
} else {
247+
long readTimeoutMS = operationContext.getTimeoutContext().getReadTimeoutMS();
248+
getChannel().read(localByteBuf.asNIO(), readTimeoutMS, MILLISECONDS, null,
249+
new BasicCompletionHandler(localByteBuf, operationContext, c.asHandler()));
250+
}
251+
}).finish(localHandler.asCallback());
245252
}
246253

247254
@Override

0 commit comments

Comments
 (0)