@@ -155,8 +155,9 @@ export class IdempotencyHandler<Func extends AnyFunction> {
155
155
let e ;
156
156
for ( let retryNo = 0 ; retryNo <= MAX_RETRIES ; retryNo ++ ) {
157
157
try {
158
- const result = await this . #saveInProgressOrReturnExistingResult( ) ;
159
- if ( result ) return result as ReturnType < Func > ;
158
+ const { isIdempotent, result } =
159
+ await this . #saveInProgressOrReturnExistingResult( ) ;
160
+ if ( isIdempotent ) return result as ReturnType < Func > ;
160
161
161
162
return await this . getFunctionResult ( ) ;
162
163
} catch ( error ) {
@@ -215,8 +216,9 @@ export class IdempotencyHandler<Func extends AnyFunction> {
215
216
) : Promise < ReturnType < Func > | void > {
216
217
for ( let retryNo = 0 ; retryNo <= MAX_RETRIES ; retryNo ++ ) {
217
218
try {
218
- const result = await this . #saveInProgressOrReturnExistingResult( ) ;
219
- if ( result ) {
219
+ const { isIdempotent, result } =
220
+ await this . #saveInProgressOrReturnExistingResult( ) ;
221
+ if ( isIdempotent ) {
220
222
await callback ( request ) ;
221
223
222
224
return result as ReturnType < Func > ;
@@ -310,43 +312,58 @@ export class IdempotencyHandler<Func extends AnyFunction> {
310
312
* Before returning a result, we might neede to look up the idempotency record
311
313
* and validate it to ensure that it is consistent with the payload to be hashed.
312
314
*/
313
- #saveInProgressOrReturnExistingResult =
314
- async ( ) : Promise < JSONValue | void > => {
315
- try {
316
- await this . #persistenceStore. saveInProgress (
317
- this . #functionPayloadToBeHashed,
318
- this . #idempotencyConfig. lambdaContext ?. getRemainingTimeInMillis ( )
319
- ) ;
320
- } catch ( e ) {
321
- if ( e instanceof IdempotencyItemAlreadyExistsError ) {
322
- let idempotencyRecord = e . existingRecord ;
323
- if ( idempotencyRecord !== undefined ) {
324
- // If the error includes the existing record, we can use it to validate
325
- // the record being processed and cache it in memory.
326
- idempotencyRecord = this . #persistenceStore. processExistingRecord (
327
- idempotencyRecord ,
328
- this . #functionPayloadToBeHashed
329
- ) ;
330
- // If the error doesn't include the existing record, we need to fetch
331
- // it from the persistence layer. In doing so, we also call the processExistingRecord
332
- // method to validate the record and cache it in memory.
333
- } else {
334
- idempotencyRecord = await this . #persistenceStore. getRecord (
335
- this . #functionPayloadToBeHashed
336
- ) ;
337
- }
315
+ #saveInProgressOrReturnExistingResult = async ( ) : Promise < {
316
+ isIdempotent : boolean ;
317
+ result : JSONValue ;
318
+ } > => {
319
+ const returnValue : {
320
+ isIdempotent : boolean ;
321
+ result : JSONValue ;
322
+ } = {
323
+ isIdempotent : false ,
324
+ result : undefined ,
325
+ } ;
326
+ try {
327
+ await this . #persistenceStore. saveInProgress (
328
+ this . #functionPayloadToBeHashed,
329
+ this . #idempotencyConfig. lambdaContext ?. getRemainingTimeInMillis ( )
330
+ ) ;
338
331
339
- return IdempotencyHandler . determineResultFromIdempotencyRecord (
340
- idempotencyRecord
332
+ return returnValue ;
333
+ } catch ( e ) {
334
+ if ( e instanceof IdempotencyItemAlreadyExistsError ) {
335
+ let idempotencyRecord = e . existingRecord ;
336
+ if ( idempotencyRecord !== undefined ) {
337
+ // If the error includes the existing record, we can use it to validate
338
+ // the record being processed and cache it in memory.
339
+ idempotencyRecord = this . #persistenceStore. processExistingRecord (
340
+ idempotencyRecord ,
341
+ this . #functionPayloadToBeHashed
341
342
) ;
343
+ // If the error doesn't include the existing record, we need to fetch
344
+ // it from the persistence layer. In doing so, we also call the processExistingRecord
345
+ // method to validate the record and cache it in memory.
342
346
} else {
343
- throw new IdempotencyPersistenceLayerError (
344
- 'Failed to save in progress record to idempotency store' ,
345
- e as Error
347
+ idempotencyRecord = await this . #persistenceStore. getRecord (
348
+ this . #functionPayloadToBeHashed
346
349
) ;
347
350
}
351
+
352
+ returnValue . isIdempotent = true ;
353
+ returnValue . result =
354
+ IdempotencyHandler . determineResultFromIdempotencyRecord (
355
+ idempotencyRecord
356
+ ) ;
357
+
358
+ return returnValue ;
359
+ } else {
360
+ throw new IdempotencyPersistenceLayerError (
361
+ 'Failed to save in progress record to idempotency store' ,
362
+ e as Error
363
+ ) ;
348
364
}
349
- } ;
365
+ }
366
+ } ;
350
367
351
368
/**
352
369
* Save a successful result to the idempotency store.
0 commit comments