Skip to content

Commit ef147d8

Browse files
committed
Remove usages of hasExpired
1 parent be5a60d commit ef147d8

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

driver-core/src/main/com/mongodb/internal/TimeoutContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ public boolean hasExpired() {
144144
return Timeout.nullAsInfinite(timeout).call(NANOSECONDS, () -> false, (ns) -> false, () -> true);
145145
}
146146

147+
public void onExpired(final Runnable onExpired) {
148+
Timeout.nullAsInfinite(timeout).onExpired(onExpired);
149+
}
150+
147151
/**
148152
* Sets the recent min round trip time
149153
* @param minRoundTripTimeMS the min round trip time
@@ -370,6 +374,7 @@ public Timeout startWaitQueueTimeout(final StartTime checkoutStart) {
370374
return checkoutStart.timeoutAfterOrInfiniteIfNegative(ms, MILLISECONDS);
371375
}
372376

377+
// TODO (CSOT) method not used in production;
373378
@Nullable
374379
public Timeout getTimeout() {
375380
return timeout;

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -771,24 +771,22 @@ private void updateSessionContext(final SessionContext sessionContext, final Res
771771
}
772772

773773
private void throwTranslatedWriteException(final Throwable e, final OperationContext operationContext) {
774-
throw translateWriteException(e, operationContext);
775-
}
776-
777-
private MongoException translateWriteException(final Throwable e, final OperationContext operationContext) {
778-
if (e instanceof MongoSocketWriteTimeoutException && operationContext.getTimeoutContext().hasExpired()) {
779-
return createMongoTimeoutException(e);
774+
if (e instanceof MongoSocketWriteTimeoutException) {
775+
operationContext.getTimeoutContext().onExpired(() -> {
776+
throw createMongoTimeoutException(e);
777+
});
780778
}
781779

782780
if (e instanceof MongoException) {
783-
return (MongoException) e;
781+
throw (MongoException) e;
784782
}
785783
Optional<MongoInterruptedException> interruptedException = translateInterruptedException(e, "Interrupted while sending message");
786784
if (interruptedException.isPresent()) {
787-
return interruptedException.get();
785+
throw interruptedException.get();
788786
} else if (e instanceof IOException) {
789-
return new MongoSocketWriteException("Exception sending message", getServerAddress(), e);
787+
throw new MongoSocketWriteException("Exception sending message", getServerAddress(), e);
790788
} else {
791-
return new MongoInternalException("Unexpected exception", e);
789+
throw new MongoInternalException("Unexpected exception", e);
792790
}
793791
}
794792

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ public ByteBuf getBuffer(final int size) {
163163
public void write(final List<ByteBuf> buffers, final OperationContext operationContext) throws IOException {
164164
for (final ByteBuf cur : buffers) {
165165
outputStream.write(cur.array(), 0, cur.limit());
166-
if (operationContext.getTimeoutContext().hasExpired()) {
166+
operationContext.getTimeoutContext().onExpired(() -> {
167167
throwMongoTimeoutException("Socket write exceeded the timeout limit.");
168-
}
168+
});
169169
}
170170
}
171171

0 commit comments

Comments
 (0)