@@ -218,7 +218,6 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
218
218
219
219
const encodedItem = JSON . stringify ( item ) ;
220
220
const ttl = this . #getExpirySeconds( record . expiryTimestamp ) ;
221
- const now = Date . now ( ) ;
222
221
223
222
try {
224
223
/**
@@ -229,14 +228,15 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
229
228
* | (in_progress_expiry) (expiry)
230
229
*
231
230
* Conditions to successfully save a record:
232
- * * The idempotency key does not exist:
231
+ *
232
+ * The idempotency key does not exist:
233
233
* - first time that this invocation key is used
234
234
* - previous invocation with the same key was deleted due to TTL
235
235
* - SET see https://redis.io/commands/set/
236
236
*/
237
237
238
238
console . debug (
239
- `Putting record for idempotency key: ${ record . idempotencyKey } `
239
+ `Putting record on redis for idempotency key: ${ record . idempotencyKey } `
240
240
) ;
241
241
const response = await this . #client. set (
242
242
record . idempotencyKey ,
@@ -268,9 +268,8 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
268
268
const existingRecord = await this . _getRecord ( record . idempotencyKey ) ;
269
269
270
270
/** If the status of the idempotency record is `COMPLETED` and the record has not expired
271
- * (i.e., the expiry timestamp is greater than the current timestamp), then a valid completed
272
- * record exists. We raise an error to prevent duplicate processing of a request that has already
273
- * been completed successfully.
271
+ * then a valid completed record exists. We raise an error to prevent duplicate processing
272
+ * of a request that has already been completed successfully.
274
273
*/
275
274
if (
276
275
existingRecord . getStatus ( ) === IdempotencyRecordStatus . COMPLETED &&
@@ -283,14 +282,14 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
283
282
}
284
283
285
284
/** If the idempotency record has a status of 'INPROGRESS' and has a valid `inProgressExpiryTimestamp`
286
- * (meaning the timestamp is greater than the current timestamp in milliseconds), then we have encountered
287
- * a valid in-progress record. This indicates that another process is currently handling the request, and
288
- * to maintain idempotency, we raise an error to prevent concurrent processing of the same request.
285
+ * (meaning the timestamp is greater than the current timestamp in milliseconds), then we have encountered
286
+ * a valid in-progress record. This indicates that another process is currently handling the request, and
287
+ * to maintain idempotency, we raise an error to prevent concurrent processing of the same request.
289
288
*/
290
289
if (
291
290
existingRecord . getStatus ( ) === IdempotencyRecordStatus . INPROGRESS &&
292
291
existingRecord . inProgressExpiryTimestamp &&
293
- existingRecord . inProgressExpiryTimestamp > now
292
+ existingRecord . inProgressExpiryTimestamp > Date . now ( )
294
293
) {
295
294
throw new IdempotencyItemAlreadyExistsError (
296
295
`Failed to put record for in-progress idempotency key: ${ record . idempotencyKey } ` ,
@@ -299,19 +298,19 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
299
298
}
300
299
301
300
/** Reaching this point indicates that the idempotency record found is an orphan record. An orphan record is
302
- * one that is neither completed nor in-progress within its expected time frame. It may result from a
303
- * previous invocation that has timed out or an expired record that has yet to be cleaned up by Redis.
304
- * We raise an error to handle this exceptional scenario appropriately.
301
+ * one that is neither completed nor in-progress within its expected time frame. It may result from a
302
+ * previous invocation that has timed out or an expired record that has yet to be cleaned up by Redis.
303
+ * We raise an error to handle this exceptional scenario appropriately.
305
304
*/
306
305
throw new IdempotencyPersistenceConsistencyError (
307
306
'Orphaned record detected'
308
307
) ;
309
308
} catch ( error ) {
310
309
if ( error instanceof IdempotencyPersistenceConsistencyError ) {
311
310
/** Handle an orphan record by attempting to acquire a lock, which by default lasts for 10 seconds.
312
- * The purpose of acquiring the lock is to prevent race conditions with other processes that might
313
- * also be trying to handle the same orphan record. Once the lock is acquired, we set a new value
314
- * for the idempotency record in Redis with the appropriate time-to-live (TTL).
311
+ * The purpose of acquiring the lock is to prevent race conditions with other processes that might
312
+ * also be trying to handle the same orphan record. Once the lock is acquired, we set a new value
313
+ * for the idempotency record in Redis with the appropriate time-to-live (TTL).
315
314
*/
316
315
await this . #acquireLock( record . idempotencyKey ) ;
317
316
@@ -337,9 +336,9 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
337
336
338
337
/**
339
338
* Attempt to acquire a lock for a specified resource name, with a default timeout.
340
- * This method attempts to set a lock using Redis to prevent concurrent
341
- * access to a resource identified by 'idempotencyKey'. It uses the 'NX' flag to ensure that
342
- * the lock is only set if it does not already exist, thereby enforcing mutual exclusion.
339
+ * This method attempts to set a lock using Redis to prevent concurrent access to a resource
340
+ * identified by 'idempotencyKey'. It uses the 'NX' flag to ensure that the lock is only
341
+ * set if it does not already exist, thereby enforcing mutual exclusion.
343
342
*
344
343
* @param idempotencyKey - The key to create a lock for
345
344
*/
@@ -354,9 +353,10 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
354
353
} ) ;
355
354
356
355
if ( acquired ) return ;
357
- // If the lock acquisition fails, it suggests a race condition has occurred. In this case, instead of
358
- // proceeding, we log the event and raise an error to indicate that the current operation should be
359
- // retried after the lock is released by the process that currently holds it.
356
+ /** If the lock acquisition fails, it suggests a race condition has occurred. In this case, instead of
357
+ * proceeding, we log the event and raise an error to indicate that the current operation should be
358
+ * retried after the lock is released by the process that currently holds it.
359
+ */
360
360
console . debug ( 'Lock acquisition failed, raise to retry' ) ;
361
361
throw new IdempotencyItemAlreadyExistsError (
362
362
'Lock acquisition failed, raise to retry'
0 commit comments