Skip to content

Commit 1f27040

Browse files
committed
apply CSOT to hasNext and tryNext
1 parent 507af55 commit 1f27040

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,18 @@ export abstract class AbstractCursor<
428428
return false;
429429
}
430430

431-
do {
432-
if ((this.documents?.length ?? 0) !== 0) {
433-
return true;
431+
try {
432+
do {
433+
if ((this.documents?.length ?? 0) !== 0) {
434+
return true;
435+
}
436+
await this.fetchBatch();
437+
} while (!this.isDead || (this.documents?.length ?? 0) !== 0);
438+
} finally {
439+
if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION && this.cursorId != null) {
440+
this.timeoutContext?.refresh();
434441
}
435-
await this.fetchBatch();
436-
} while (!this.isDead || (this.documents?.length ?? 0) !== 0);
442+
}
437443

438444
return false;
439445
}
@@ -444,18 +450,20 @@ export abstract class AbstractCursor<
444450
throw new MongoCursorExhaustedError();
445451
}
446452

447-
if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION) {
448-
this.timeoutContext?.refresh();
449-
}
450-
451-
do {
452-
const doc = this.documents?.shift(this.cursorOptions);
453-
if (doc != null) {
454-
if (this.transform != null) return await this.transformDocument(doc);
455-
return doc;
453+
try {
454+
do {
455+
const doc = this.documents?.shift(this.cursorOptions);
456+
if (doc != null) {
457+
if (this.transform != null) return await this.transformDocument(doc);
458+
return doc;
459+
}
460+
await this.fetchBatch();
461+
} while (!this.isDead || (this.documents?.length ?? 0) !== 0);
462+
} finally {
463+
if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION && this.cursorId != null) {
464+
this.timeoutContext?.refresh();
456465
}
457-
await this.fetchBatch();
458-
} while (!this.isDead || (this.documents?.length ?? 0) !== 0);
466+
}
459467

460468
return null;
461469
}
@@ -468,18 +476,24 @@ export abstract class AbstractCursor<
468476
throw new MongoCursorExhaustedError();
469477
}
470478

471-
let doc = this.documents?.shift(this.cursorOptions);
472-
if (doc != null) {
473-
if (this.transform != null) return await this.transformDocument(doc);
474-
return doc;
475-
}
479+
try {
480+
let doc = this.documents?.shift(this.cursorOptions);
481+
if (doc != null) {
482+
if (this.transform != null) return await this.transformDocument(doc);
483+
return doc;
484+
}
476485

477-
await this.fetchBatch();
486+
await this.fetchBatch();
478487

479-
doc = this.documents?.shift(this.cursorOptions);
480-
if (doc != null) {
481-
if (this.transform != null) return await this.transformDocument(doc);
482-
return doc;
488+
doc = this.documents?.shift(this.cursorOptions);
489+
if (doc != null) {
490+
if (this.transform != null) return await this.transformDocument(doc);
491+
return doc;
492+
}
493+
} finally {
494+
if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION && this.cursorId != null) {
495+
this.timeoutContext?.refresh();
496+
}
483497
}
484498

485499
return null;

src/timeout.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,28 +268,26 @@ export class CSOTTimeoutContext extends TimeoutContext {
268268
const { remainingTimeMS } = this;
269269
if (!Number.isFinite(remainingTimeMS)) return null;
270270
if (remainingTimeMS > 0) return Timeout.expires(remainingTimeMS);
271-
throw new MongoOperationTimeoutError('Timed out before socket write');
271+
throw new MongoOperationTimeoutError(`Timed out before socket write after ${this.timeoutMS}ms`);
272272
}
273273

274274
get timeoutForSocketRead(): Timeout | null {
275275
const { remainingTimeMS } = this;
276276
if (!Number.isFinite(remainingTimeMS)) return null;
277277
if (remainingTimeMS > 0) return Timeout.expires(remainingTimeMS);
278-
throw new MongoOperationTimeoutError('Timed out before socket read');
278+
throw new MongoOperationTimeoutError(`Timed out before socket read after ${this.timeoutMS}ms`);
279279
}
280280

281281
refresh(): void {
282282
this.start = Math.trunc(performance.now());
283283
this.minRoundTripTime = 0;
284284
this._serverSelectionTimeout?.clear();
285-
this._serverSelectionTimeout = undefined;
286-
this._connectionCheckoutTimeout = undefined;
285+
this._connectionCheckoutTimeout?.clear();
287286
}
288287

289288
clear(): void {
290289
this._serverSelectionTimeout?.clear();
291-
this._serverSelectionTimeout = undefined;
292-
this._connectionCheckoutTimeout = undefined;
290+
this._connectionCheckoutTimeout?.clear();
293291
}
294292
}
295293

0 commit comments

Comments
 (0)