Skip to content

Commit 0a6bbfc

Browse files
committed
Only schedule a timeout if one doesn't already exist
JAVA-1934
1 parent f3b0cf5 commit 0a6bbfc

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

driver-core/src/main/com/mongodb/connection/netty/ReadTimeoutHandler.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.concurrent.ScheduledFuture;
2525
import java.util.concurrent.TimeUnit;
2626

27+
import static com.mongodb.assertions.Assertions.isTrue;
2728
import static com.mongodb.assertions.Assertions.isTrueArgument;
2829

2930
/**
@@ -40,14 +41,17 @@ public ReadTimeoutHandler(final long readTimeout) {
4041
}
4142

4243
void scheduleTimeout(final ChannelHandlerContext ctx) {
43-
timeout = ctx.executor().schedule(new ReadTimeoutTask(ctx), readTimeout, TimeUnit.MILLISECONDS);
44+
isTrue("Handler called from the eventLoop", ctx.channel().eventLoop().inEventLoop());
45+
if (timeout == null) {
46+
timeout = ctx.executor().schedule(new ReadTimeoutTask(ctx), readTimeout, TimeUnit.MILLISECONDS);
47+
}
4448
}
4549

4650
void removeTimeout(final ChannelHandlerContext ctx) {
47-
if (ctx.channel().eventLoop().inEventLoop()) {
48-
if (timeout != null) {
49-
timeout.cancel(false);
50-
}
51+
isTrue("Handler called from the eventLoop", ctx.channel().eventLoop().inEventLoop());
52+
if (timeout != null) {
53+
timeout.cancel(false);
54+
timeout = null;
5155
}
5256
}
5357

0 commit comments

Comments
 (0)