Skip to content

Commit

Permalink
clean up logic
Browse files Browse the repository at this point in the history
  • Loading branch information
W-A-James committed May 10, 2024
1 parent 5c91311 commit 72c22b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 11 additions & 3 deletions src/cmap/connection_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,20 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
// CSOT enabled
// Determine if we're using the timeout passed in or a new timeout
if (options.timeout.duration > 0 || serverSelectionTimeoutMS > 0) {
// This check determines whether or not Topology.selectServer used the configured
// `timeoutMS` or `serverSelectionTimeoutMS` value for its timeout
if (
csotMin(options.timeout.duration, serverSelectionTimeoutMS) === serverSelectionTimeoutMS
options.timeout.duration === serverSelectionTimeoutMS ||
csotMin(options.timeout.duration, serverSelectionTimeoutMS) < serverSelectionTimeoutMS
) {
timeout = Timeout.expires(serverSelectionTimeoutMS - options.timeout.timeElapsed);
} else {
// server selection used `timeoutMS`, so we should use the existing timeout as the timeout
// here
timeout = options.timeout;
} else {
// server selection used `serverSelectionTimeoutMS`, so we construct a new timeout with
// the time remaining to ensure that Topology.selectServer and ConnectionPool.checkOut
// cumulatively don't spend more than `serverSelectionTimeoutMS` blocking
timeout = Timeout.expires(serverSelectionTimeoutMS - options.timeout.timeElapsed);
}
}
} else {
Expand Down
15 changes: 9 additions & 6 deletions src/sdam/topology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { Timeout, TimeoutError } from '../timeout';
import type { Transaction } from '../transactions';
import {
type Callback,
csotMin,
type EventEmitterWithState,
HostAddress,
List,
Expand Down Expand Up @@ -563,18 +564,20 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
);
}
const serverSelectionTimeoutMS = options.serverSelectionTimeoutMS ?? 0;
let timeout: Timeout | null = null;
let timeout: Timeout | null;
if (options.timeout) {
// CSOT Enabled
if (options.timeout.duration !== 0 || serverSelectionTimeoutMS !== 0) {
if (options.timeout.duration > 0 || serverSelectionTimeoutMS > 0) {
if (
Math.min(options.timeout.remainingTime, serverSelectionTimeoutMS) ===
serverSelectionTimeoutMS
options.timeout.duration === serverSelectionTimeoutMS ||
csotMin(options.timeout.duration, serverSelectionTimeoutMS) < serverSelectionTimeoutMS
) {
timeout = Timeout.expires(serverSelectionTimeoutMS);
} else {
timeout = options.timeout;
} else {
timeout = Timeout.expires(serverSelectionTimeoutMS);
}
} else {
timeout = null;
}
} else {
timeout = Timeout.expires(serverSelectionTimeoutMS);
Expand Down

0 comments on commit 72c22b9

Please sign in to comment.