Skip to content

Commit bcee059

Browse files
committed
wip
1 parent ca577b2 commit bcee059

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export type CursorFlag = (typeof CURSOR_FLAGS)[number];
6363
/** @internal */
6464
export const CursorTimeoutMode = Object.freeze({
6565
ITERATION: 'iteration',
66-
LIFETIME: 'lifetime'
66+
LIFETIME: 'cursorLifetime'
6767
} as const);
6868

6969
/** @internal
@@ -206,20 +206,20 @@ export abstract class AbstractCursor<
206206
this.cursorOptions.timeoutMS = options.timeoutMS;
207207
if (this.cursorOptions.timeoutMS != null) {
208208
if (options.timeoutMode == null) {
209-
if (this.cursorOptions.tailable) {
209+
if (options.tailable) {
210210
this.cursorOptions.timeoutMode = CursorTimeoutMode.ITERATION;
211211
} else {
212212
this.cursorOptions.timeoutMode = CursorTimeoutMode.LIFETIME;
213213
}
214214
} else {
215-
if (
216-
this.cursorOptions.tailable &&
217-
this.cursorOptions.timeoutMode === CursorTimeoutMode.LIFETIME
218-
) {
215+
if (options.tailable && this.cursorOptions.timeoutMode === CursorTimeoutMode.LIFETIME) {
219216
throw new MongoAPIError("Cannot set tailable cursor's timeoutMode to LIFETIME");
220217
}
221218
this.cursorOptions.timeoutMode = options.timeoutMode;
222219
}
220+
} else {
221+
if (options.timeoutMode != null)
222+
throw new MongoAPIError('Cannot set timeoutMode without setting timeoutMS');
223223
}
224224

225225
const readConcern = ReadConcern.fromOptions(options);
@@ -444,19 +444,22 @@ export abstract class AbstractCursor<
444444
throw new MongoCursorExhaustedError();
445445
}
446446

447-
do {
448-
const doc = this.documents?.shift(this.cursorOptions);
449-
if (doc != null) {
450-
if (this.transform != null) return await this.transformDocument(doc);
451-
return doc;
452-
}
453-
await this.fetchBatch();
454-
} while (!this.isDead || (this.documents?.length ?? 0) !== 0);
447+
try {
448+
do {
449+
const doc = this.documents?.shift(this.cursorOptions);
450+
if (doc != null) {
451+
if (this.transform != null) return await this.transformDocument(doc);
452+
return doc;
453+
}
454+
await this.fetchBatch();
455+
} while (!this.isDead || (this.documents?.length ?? 0) !== 0);
455456

456-
if (this.cursorOptions.timeoutMode === 'iteration') {
457-
this.timeoutContext?.refresh();
457+
return null;
458+
} finally {
459+
if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION) {
460+
this.timeoutContext?.refresh();
461+
}
458462
}
459-
return null;
460463
}
461464

462465
/**
@@ -689,6 +692,8 @@ export abstract class AbstractCursor<
689692

690693
this.cursorId = null;
691694
this.documents?.clear();
695+
this.timeoutContext?.clear();
696+
this.timeoutContext = undefined;
692697
this.isClosed = false;
693698
this.isKilled = false;
694699
this.initialized = false;
@@ -761,7 +766,8 @@ export abstract class AbstractCursor<
761766
}
762767
const omitMaxTimeMS =
763768
this.cursorOptions.timeoutMS != null &&
764-
((this.cursorOptions.timeoutMode === 'iteration' && !this.cursorOptions.tailable) ||
769+
((this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION &&
770+
!this.cursorOptions.tailable) ||
765771
(this.cursorOptions.tailable && !this.cursorOptions.awaitData));
766772
try {
767773
const state = await this._initialize(this.cursorSession, {

src/cursor/run_command_cursor.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ import { ns } from '../utils';
1212
import {
1313
AbstractCursor,
1414
type CursorInitializeOptions,
15+
type CursorTimeoutMode,
1516
type InitialCursorResponse
1617
} from './abstract_cursor';
1718

1819
/** @public */
1920
export type RunCursorCommandOptions = {
2021
readPreference?: ReadPreferenceLike;
2122
session?: ClientSession;
23+
timeoutMS?: number;
24+
timeoutMode?: CursorTimeoutMode;
2225
} & BSONSerializeOptions;
2326

2427
/** @public */
@@ -131,6 +134,6 @@ export class RunCommandCursor extends AbstractCursor {
131134
...this.getMoreOptions
132135
});
133136

134-
return await executeOperation(this.client, getMoreOperation);
137+
return await executeOperation(this.client, getMoreOperation, this.timeoutContext);
135138
}
136139
}

src/timeout.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export class CSOTTimeoutContext extends TimeoutContext {
209209
}
210210

211211
get maxTimeMS(): number {
212+
console.log(this.remainingTimeMS, this.minRoundTripTime);
212213
return this.remainingTimeMS - this.minRoundTripTime;
213214
}
214215

@@ -279,6 +280,7 @@ export class CSOTTimeoutContext extends TimeoutContext {
279280

280281
refresh(): void {
281282
this.start = Math.trunc(performance.now());
283+
this.minRoundTripTime = 0;
282284
this._serverSelectionTimeout?.clear();
283285
this._connectionCheckoutTimeout?.clear();
284286
}

0 commit comments

Comments
 (0)