Skip to content

Small improvements. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
89 changes: 73 additions & 16 deletions driver-core/src/main/com/mongodb/connection/netty/NettyStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,29 @@ public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf>
readAsync(numBytes, handler, 0);
}

/**
* @param additionalTimeout Must be equal to {@link #NO_SCHEDULE_TIMEOUT} when the method is called by a Netty channel handler.
* A timeout is scheduled only by the public read methods. Taking into account that concurrent pending readers
* are not allowed, there must not be a situation when threads attempt to schedule a timeout
* before the previous one is removed or completed.
*/
private void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler, final int additionalTimeout) {
scheduleReadTimeout(additionalTimeout);
readAsync(numBytes, handler, additionalTimeout, null);
}

private void readAsync(final PendingReader pendingReader) {
readAsync(pendingReader.numBytes, pendingReader.handler, 0, pendingReader);
}

private void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler, final int additionalTimeout,
final PendingReader pendingReaderToUse) {
ByteBuf buffer = null;
Throwable exceptionResult = null;
TimeoutHandle preparedTimeoutHandle = null;
synchronized (this) {
exceptionResult = pendingException;
if (exceptionResult == null) {
if (!hasBytesAvailable(numBytes)) {
pendingReader = new PendingReader(numBytes, handler);
if (pendingReaderToUse == null) {
preparedTimeoutHandle = prepareTimeout(additionalTimeout);
pendingReader = new PendingReader(numBytes, handler, preparedTimeoutHandle);
} else {
pendingReader = pendingReaderToUse;
}
} else {
CompositeByteBuf composite = allocator.compositeBuffer(pendingInboundBuffers.size());
int bytesNeeded = numBytes;
Expand All @@ -284,12 +292,21 @@ private void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf>
}
}
}

if (preparedTimeoutHandle != null) {
preparedTimeoutHandle.scheduleTimeout();
}

if (exceptionResult != null) {
disableReadTimeout();
if (pendingReaderToUse != null) {
pendingReaderToUse.timeoutHandle.cancel();
}
handler.failed(exceptionResult);
}
if (buffer != null) {
disableReadTimeout();
if (pendingReaderToUse != null) {
pendingReaderToUse.timeoutHandle.cancel();
}
handler.completed(buffer);
}
}
Expand Down Expand Up @@ -320,8 +337,7 @@ private void handleReadResponse(final io.netty.buffer.ByteBuf buffer, final Thro
}

if (localPendingReader != null) {
//if there is a pending reader, then the reader has scheduled a timeout and we should not attempt to schedule another one
readAsync(localPendingReader.numBytes, localPendingReader.handler, NO_SCHEDULE_TIMEOUT);
readAsync(localPendingReader);
}
}

Expand Down Expand Up @@ -397,10 +413,12 @@ public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable t)
private static final class PendingReader {
private final int numBytes;
private final AsyncCompletionHandler<ByteBuf> handler;
private final TimeoutHandle timeoutHandle;

private PendingReader(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) {
private PendingReader(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler, final TimeoutHandle timeoutHandle) {
this.numBytes = numBytes;
this.handler = handler;
this.timeoutHandle = timeoutHandle;
}
}

Expand Down Expand Up @@ -485,9 +503,6 @@ public void operationComplete(final ChannelFuture future) {
}

private void scheduleReadTimeout(final int additionalTimeout) {
if (additionalTimeout == NO_SCHEDULE_TIMEOUT) {
return;
}
adjustTimeout(false, additionalTimeout);
}

Expand All @@ -509,4 +524,46 @@ private void adjustTimeout(final boolean disable, final int additionalTimeout) {
}
}
}

private TimeoutHandle prepareTimeout(final int additionalTimeout) {
return new SimpleTimeoutHandle(additionalTimeout);
}

private final class SimpleTimeoutHandle implements TimeoutHandle {
private final int additionalTimeout;

private boolean cancelled = false;
private boolean scheduled = false;

private SimpleTimeoutHandle(final int additionalTimeout) {
this.additionalTimeout = additionalTimeout;
}

@Override
public synchronized void scheduleTimeout() {
assert !scheduled : "Attempted to schedule an already scheduled timeout";

if (!cancelled) {
scheduleReadTimeout(additionalTimeout);
scheduled = true;
}
}

@Override
public synchronized void cancel() {
assert !cancelled : "Attempted to cancel an already cancelled timeout";

if (scheduled) {
disableReadTimeout();
scheduled = false;
}
cancelled = true;
}
}

private interface TimeoutHandle {
void scheduleTimeout();

void cancel();
}
}