@@ -265,10 +265,11 @@ class ClientSession extends EventEmitter {
265265 */
266266 withTransaction ( fn , options ) {
267267 const startTime = Date . now ( ) ;
268- return retryTransaction ( this , startTime , fn , options ) ;
268+ return attemptTransaction ( this , startTime , fn , options ) ;
269269 }
270270}
271271
272+ const MAX_WITH_TRANSACTION_TIMEOUT = 120000 ;
272273const WRITE_CONCERN_FAILED_CODE = 64 ;
273274const UNSATISFIABLE_WRITE_CONCERN_CODE = 100 ;
274275const UNKNOWN_REPL_WRITE_CONCERN_CODE = 79 ;
@@ -278,6 +279,10 @@ const NON_DETERMINISTIC_WRITE_CONCERN_ERRORS = [
278279 'UnsatisfiableWriteConcern'
279280] ;
280281
282+ function hasNotTimedOut ( startTime , max ) {
283+ return Date . now ( ) - startTime < max ;
284+ }
285+
281286function isWriteConcernTimeoutError ( err ) {
282287 return err . code === WRITE_CONCERN_FAILED_CODE && ! ! ( err . errInfo && err . errInfo . wtimeout === true ) ;
283288}
@@ -290,54 +295,55 @@ function isUnknownTransactionCommitResult(err) {
290295 ) ;
291296}
292297
293- function retryCommit ( session , startTime , fn , options ) {
298+ function attemptTransactionCommit ( session , startTime , fn , options ) {
294299 return session . commitTransaction ( ) . catch ( err => {
295300 if (
296301 ! isWriteConcernTimeoutError ( err ) &&
297302 ( err instanceof MongoError && err . hasErrorLabel ( 'UnknownTransactionCommitResult' ) ) &&
298- Date . now ( ) - startTime < 120000
303+ hasNotTimedOut ( startTime , MAX_WITH_TRANSACTION_TIMEOUT )
299304 ) {
300- return retryCommit ( session , startTime , fn , options ) ;
305+ return attemptTransactionCommit ( session , startTime , fn , options ) ;
301306 }
302307
303308 if (
304309 err instanceof MongoError &&
305310 err . hasErrorLabel ( 'TransientTransactionError' ) &&
306- Date . now ( ) - startTime < 120000
311+ hasNotTimedOut ( startTime , MAX_WITH_TRANSACTION_TIMEOUT )
307312 ) {
308- return retryTransaction ( session , startTime , fn , options ) ;
313+ return attemptTransaction ( session , startTime , fn , options ) ;
309314 }
310315
311316 throw err ;
312317 } ) ;
313318}
314319
315- function retryTransaction ( session , startTime , fn , options ) {
320+ function userExplicitlyEndedTransaction ( session ) {
321+ return (
322+ [ TxnState . NO_TRANSACTION , TxnState . TRANSACTION_COMMITTED , TxnState . TRANSACTION_ABORTED ] . indexOf (
323+ session . transaction . state
324+ ) !== - 1
325+ ) ;
326+ }
327+
328+ function attemptTransaction ( session , startTime , fn , options ) {
316329 session . startTransaction ( options ) ;
317330
318331 return fn ( session )
319332 . then ( ( ) => {
320- if (
321- [
322- TxnState . NO_TRANSACTION ,
323- TxnState . TRANSACTION_COMMITTED ,
324- TxnState . TRANSACTION_ABORTED
325- ] . indexOf ( session . transaction . state ) !== - 1
326- ) {
327- // Assume user provided function intentionally ended the transaction
333+ if ( userExplicitlyEndedTransaction ( session ) ) {
328334 return ;
329335 }
330336
331- return retryCommit ( session , startTime , fn , options ) ;
337+ return attemptTransactionCommit ( session , startTime , fn , options ) ;
332338 } )
333339 . catch ( err => {
334340 function maybeRetryOrThrow ( err ) {
335341 if (
336342 err instanceof MongoError &&
337343 err . hasErrorLabel ( 'TransientTransactionError' ) &&
338- Date . now ( ) - startTime < 120000
344+ hasNotTimedOut ( startTime , MAX_WITH_TRANSACTION_TIMEOUT )
339345 ) {
340- return retryTransaction ( session , startTime , fn , options ) ;
346+ return attemptTransaction ( session , startTime , fn , options ) ;
341347 }
342348
343349 throw err ;
0 commit comments