Skip to content

Commit ca0124d

Browse files
authored
refactor: remove some redundancies in execute_operator (#2488)
NODE-2894
1 parent a2d78b2 commit ca0124d

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

src/operations/execute_operation.ts

+32-30
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,17 @@ function executeWithServerSelection(topology: Topology, operation: any, callback
150150
return callback(undefined, result);
151151
}
152152

153-
if (
154-
(operation.hasAspect(Aspect.READ_OPERATION) && !isRetryableError(err)) ||
155-
(operation.hasAspect(Aspect.WRITE_OPERATION) && !shouldRetryWrite(err))
156-
) {
153+
const hasReadAspect = operation.hasAspect(Aspect.READ_OPERATION);
154+
const hasWriteAspect = operation.hasAspect(Aspect.WRITE_OPERATION);
155+
const itShouldRetryWrite = shouldRetryWrite(err);
156+
157+
if ((hasReadAspect && !isRetryableError(err)) || (hasWriteAspect && !itShouldRetryWrite)) {
157158
return callback(err);
158159
}
159160

160161
if (
161-
operation.hasAspect(Aspect.WRITE_OPERATION) &&
162-
shouldRetryWrite(err) &&
162+
hasWriteAspect &&
163+
itShouldRetryWrite &&
163164
err.code === MMAPv1_RETRY_WRITES_ERROR_CODE &&
164165
err.errmsg.match(/Transaction numbers/)
165166
) {
@@ -196,32 +197,33 @@ function executeWithServerSelection(topology: Topology, operation: any, callback
196197
return;
197198
}
198199

199-
const willRetryRead =
200-
topology.s.options.retryReads !== false &&
201-
session &&
202-
!inTransaction &&
203-
supportsRetryableReads(server) &&
204-
operation.canRetryRead;
205-
206-
const willRetryWrite =
207-
topology.s.options.retryWrites === true &&
208-
session &&
209-
!inTransaction &&
210-
supportsRetryableWrites(server) &&
211-
operation.canRetryWrite;
200+
if (operation.hasAspect(Aspect.RETRYABLE)) {
201+
const willRetryRead =
202+
topology.s.options.retryReads !== false &&
203+
operation.session &&
204+
!inTransaction &&
205+
supportsRetryableReads(server) &&
206+
operation.canRetryRead;
207+
208+
const willRetryWrite =
209+
topology.s.options.retryWrites === true &&
210+
operation.session &&
211+
!inTransaction &&
212+
supportsRetryableWrites(server) &&
213+
operation.canRetryWrite;
214+
215+
const hasReadAspect = operation.hasAspect(Aspect.READ_OPERATION);
216+
const hasWriteAspect = operation.hasAspect(Aspect.WRITE_OPERATION);
217+
218+
if ((hasReadAspect && willRetryRead) || (hasWriteAspect && willRetryWrite)) {
219+
if (hasWriteAspect && willRetryWrite) {
220+
operation.options.willRetryWrite = true;
221+
operation.session.incrementTransactionNumber();
222+
}
212223

213-
if (
214-
operation.hasAspect(Aspect.RETRYABLE) &&
215-
((operation.hasAspect(Aspect.READ_OPERATION) && willRetryRead) ||
216-
(operation.hasAspect(Aspect.WRITE_OPERATION) && willRetryWrite))
217-
) {
218-
if (operation.hasAspect(Aspect.WRITE_OPERATION) && willRetryWrite) {
219-
operation.options.willRetryWrite = true;
220-
session.incrementTransactionNumber();
224+
operation.execute(server, callbackWithRetry);
225+
return;
221226
}
222-
223-
operation.execute(server, callbackWithRetry);
224-
return;
225227
}
226228

227229
operation.execute(server, callback);

0 commit comments

Comments
 (0)